In Numpy, what is the difference between ndarray and array? And where in the numpy source code can I locate the implementations?
Asked by flxb
Numpy.array is only a helper function for making an ndarray; it isn’t a class in and of itself.
You can alternatively use numpy.ndarray to make an array, however this is not advised. Numpy.ndarray’s docstring says:
The guts of the implementation is in C code, which you can see here in multiarray, but you may start with the ndarray interfaces:
Answered by wim
The function numpy.array returns a numpy.ndarray. Numpy.array is not an object type.
Answered by Ramón J Romero y Vigil
A few lines of code to illustrate the differences between numpy.array and numpy.ndarray.
Create a list as a warm-up step.
a = [1,2,3]
Check the type
You will get
Using np.array, create an array (from a list).
a = np.array(a)
You can also skip the warm-up and go straight to the workout.
a = np.array([1,2,3])
Check the type
You will get
This indicates that the numpy array’s type is numpy.ndarray.
You can also determine the type by using the following formula:
and you’ll receive
An error warning will appear if you type either of the following two lines.
np.ndarray(a) # should be np.array(a) isinstance(a, (np.array)) # should be isinstance(a, (np.ndarray))
Answered by Ying
Numpy.ndarray() is a class, whereas numpy.array() is a method for creating ndarray.
According to the numpy documentation, there are two ways to generate an array from the ndarray class:
1- build arrays with array(), zeros(), or empty() methods: Arrays should be built with array(), zeros(), or empty() methods (refer to the See Also section below). The parameters here correspond to a low-level array instantiation mechanism (ndarray(…)).
2- straight from the ndarray class: When using __new__ to create an array, there are two options: Only shape, dtype, and order are used if buffer is None. All keywords are processed if buffer is an object that exposes the buffer interface.
Because we didn’t assign a buffer value, the next example produces a random array:
Assigning an array object to the buffer is another example:
We can’t assign a list to “buffer” in the example above, therefore we had to use numpy.array() to return an ndarray object for the buffer.
If you wish to build a numpy.ndarray() object, use numpy.array().”
Answered by Mahmoud Elshahat
Though you indicate the order, I believe you can only build C such with np.array(), as np.isfortran() returns false. When you use np.ndarrray() and specify the order, it produces the array in that order.
Answered by Sujith Rao
Post is based on https://stackoverflow.com/questions/15879315/what-is-the-difference-between-ndarray-and-array-in-numpy