How to Initialize Numpy Array : Know various Methods

[ad_1]

Numpy is an open-source and free python package for array creation and manipulation. There are many inbuilt functions that allow you to do so. You can create 1D or 2D  NumPy array, initialize the NumPy array, do complex mathematical calculations on it very easily. But how you can initialize a Numpy array. In this entire tutorial, you will learn the various way to initialize the NumPy array.

 

Different ways to Initialize the Numpy array

Method 1: Initialize NumPy array from existing lists

You can initialize the NumPy array using the existing lists. You have to pass the existing lists to the numpy.asarray() function to create an array. The list is 1D so the NumPy array will be a one-dimensional array.

Execute the below lines of code to create a NumPy array from the list.

import numpy as np
sample_list = [1,2,3,4,5,6]
numpy_array = np.asarray(sample_list)
numpy_array

Output

Numpy array from list
Numpy array from list

Method 2:  Initialize the empty NumPy array

The second method to initialize the NumPy array is using the NumPy empty() method. You have to just provide the shape of the array you want to create.

For example, If  I want to create an empty array of one dimensional then I will pass the single scalar value like np.empty([scalar_value])

import numpy as np
numpy_array = np.empty([3])
numpy_array

Output

Empty 1D Numpy array
Empty 1D Numpy array

But if I want the array for two dimensional then you have to pass number of rows and no of columns as list. For example for 3 rows and 2 columns, I will use the following lines of code.

import numpy as np
numpy_array = np.empty([3,2])
numpy_array

Output

Empty 2D Numpy array
Empty 2D Numpy array

 

Method 3: Numpy array with zeros

You can also create NumPy array using the zeros() method. Here all the elements of the NumPy array will be zero. If you will pass a single scalar value then you will get the one-dimensional array. And if you want to create a two-dimensional array then you have to pass the number of rows and columns as a list.

1 D Array

import numpy as np
numpy_array = np.zeros([3])
numpy_array

Output

1D Numpy array with all zero elements
1D Numpy array with all zero elements

2 D Array

import numpy as np
numpy_array = np.zeros([3,4])
numpy_array

Output

2D Numpy array with all zero elements
2D Numpy array with all zero elements

Method 4: NumPy array with ones

Just like you have initialized the NumPy array with zero in each element. In the same way, you create NumPy array with one as an element. To do so you have to use the numpy.ones() function. Here also you can create one-dimensional and multi-dimensional arrays.

1D Numpy array

import numpy as np
numpy_array = np.ones([3])
numpy_array

Output

1D Numpy array with all one elements
1D Numpy array with all one elements

2D Numpy array

import numpy as np
numpy_array = np.ones([3,4])
numpy_array

Output

2D Numpy array with all one elements
2D Numpy array with all one elements

Method 5:  Sequential or evenly spaced Numpy arrays

The NumPy module has a method np.arange() and np.linspace() to create a sequential or evenly spaced NumPy array.

The numpy.arange() method allows you to create an array in a defined range.

1D Numpy array

You can create elements for the NumPy array bypassing the scalar value to the arange() function. It creates elements within the defined range.

import numpy as np
numpy_array = np.arange(6)
numpy_array

Output

1D Numpy array creation using arange() method
1D Numpy array creation using arange() method

2D Numpy array

To create a 2D array using the arange() function you have to also use the reshape() method. It will organize the elements and convert the single-dimensional array to a multi-dimensional array.

import numpy as np
numpy_array = np.arange(6).reshape(3,2)
numpy_array

Output

2D Numpy array creation using arange() method
2D Numpy array creation using arange() method

 

The numpy.linspace() method creates an array in a defined range with the number of elements within it. For example, If I want 10 elements between 0 and 1 then I will pass the start value 0, stop value 1, and num =10. It will create 10 elements between  0 and 1.

Execute the below lines of code to create an array.

import numpy as np
numpy_array = np.linspace(0,1,10)
numpy_array

Output

Numpy array creation using linspace() method
Numpy array creation using linspace() method

Conclusion

Numpy is the best python package for creating a NumPy array. It allows you to perform complex mathematical calculations in an efficient way. If you want to initialize the NumPy array then the above method will be able to do so.

There can be also other methods to initialize the NumPy array. If you have any suggestions then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.



[ad_2]

Source link