Saturday, January 22, 2022

Creating NumPy Arrays

Depending on the type of data you need inside your NumPy array, different methods can be used to create a NumPy array. 

1. Using Array Method - To create a NumPy array, you can pass a list to the array() method of the NumPy module, as shown below:

nums_list = [10,12,14,16,20]

nums_array = np.array(nums_list)

type(nums_array)

Output:

numpy.ndarray

You can also create a multi-dimensional NumPy array. To do so, you need to create a list of lists where each internal list corresponds to the row in a two dimensional array. Here is an example of how to create a two-dimensional array using the array() method.

row1 = [10,12,13]

row2 = [45,32,16]

row3 = [45,32,16]

nums_2d = np.array([row1, row2, row3])

nums_2d.shape

Output:

(3, 3)

2. Using Arrange Method - With the arrange () method, you can create a NumPy array that contains a range of integers. The first parameter to the arrange method is the lower bound, and the second parameter is the upper bound. The lower bound is included in the array. However, the upper bound is not included. The following script creates a NumPy array with integers 5 to 10.

nums_arr = np.arange(5,11)

print(nums_arr)

Output:

[5 6 7 8 9 10]

You can also specify the step as a third parameter in the arrange() function. A step defines the distance between two consecutive points in the array. The following script creates a NumPy array from 5 to 11 with a step size of 2.

nums_arr = np.arange(5,12,2)

print(nums_arr)

Output:

[5 7 9 11]

3. Using Ones Method - The ones() method can be used to create a NumPy array of all ones. Here is an example.

ones_array = np.ones(6)

print(ones_array)

Output:

[1. 1. 1. 1. 1. 1.]

You can create a two-dimensional array of all ones by passing the number of rows and columns as the first and second parameters of the ones() method, as shown below:

ones_array = np.ones((6,4))

print(ones_array)

Output:

[[1. 1. 1. 1.]

[1. 1. 1. 1.]

[1. 1. 1. 1.]

[1. 1. 1. 1.]

[1. 1. 1. 1.]

[1. 1. 1. 1.]]

4. Using Zeros Method - The zeros() method can be used to create a NumPy array of all zeros. Here is an example.

zeros_array = np.zeros(6)

print(zeros_array)

Output:

[0. 0. 0. 0. 0. 0.]

You can create a two-dimensional array of all zeros by passing the number of rows and columns as the first and second parameters of the zeros() method, as shown below:

zeros_array = np.zeros((6,4))

print(zeros_array)

Output:

[[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]]

5. Using Eyes Method - The eye() method is used to create an identity matrix in the form of a two dimensional NumPy array. An identity matrix contains 1s along the diagonal, while the rest of the elements are 0 in the array.

eyes_array = np.eye(5)

print(eyes_array)

Output:

[[1. 0. 0. 0. 0.]

[0. 1. 0. 0. 0.]

[0. 0. 1. 0. 0.]

[0. 0. 0. 1. 0.]

[0. 0. 0. 0. 1.]]

6. Using Random Method - The random.rand() function from the NumPy module can be used to create a NumPy array with uniform distribution.

uniform_random = np.random.rand(4, 5)

print(uniform_random)

Output:

[[0.36728531 0.25376281 0.05039624 0.96432236 0.08579293]

[0.29194804 0.93016399 0.88781312 0.50209692 0.63069239]

[0.99952044 0.44384871 0.46041845 0.10246553 0.53461098]

[0.75817916 0.36505441 0.01683344 0.9887365 0.21490949]]

The random.randn() function from the NumPy module can be used to create a NumPy array with normal distribution, as shown in the following example:

normal_random = np.random.randn(4, 5)

print(uniform_random)

Output:

[[0.36728531 0.25376281 0.05039624 0.96432236 0.08579293]

[0.29194804 0.93016399 0.88781312 0.50209692 0.63069239]

[0.99952044 0.44384871 0.46041845 0.10246553 0.53461098]

[0.75817916 0.36505441 0.01683344 0.9887365 0.21490949]]

Finally, the random.randint() function from the NumPy module can be used to create a NumPy array with random integers between a certain range. The first parameter to the randint() function specifies the lower bound, the second parameter specifies the upper bound, and the last parameter specifies the number of random integers to generate between the range. The following example generates five random integers between 5 and 50. 

integer_random = np.random.randint(10, 50, 5)

print(integer_random)

Output:

[25 49 21 35 17]

In the next post we will discuss about printing NumPy arrays. 

Share:

0 comments:

Post a Comment