Numpy array VS Numpy asarray
In Python, NumPy array and NumPy asarray are used to convert the data into ndarray. If we talk about the major difference that is when we make a NumPy array using np.array, it creates a copy of the object array or the original array and does not reflect any changes made to the original array. Whereas on the other hand, when we try to use NumPy asarray, it would reflect all the changes made to the original array.
Example
Here we can see that copy of the original object is created which is actually changed and hence it’s not reflected outside. Whereas, in the next part, we can see that no copy of the original object is created and hence it’s reflected outside.
Python3
import numpy as np # creating array a = np.array([ 2 , 3 , 4 , 5 , 6 ]) print ( "Original array : " ,a) # assigning value to np.array np_array = np.array(a) a[ 3 ] = 0 print ( "np.array Array : " ,np_array) # assigning value to np.asarray np_array = np.asarray(a) print ( "np.asarray Array : " ,np_array) |
Output:
Original array : [2 3 4 5 6]
np.array Array : [2 3 4 5 6]
np.asarray Array : [2 3 4 0 6]
Difference between np.asarray() and np.array()?
NumPy is a Python library used for dealing with arrays. In Python, we use the list inplace of the array but it’s slow to process. NumPy array is a powerful N-dimensional array object and is used in linear algebra, Fourier transform, and random number capabilities. It provides an array object much faster than traditional Python lists.