Coder Perfect

What’s the difference between a numpy ndarray and an array?


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

Solution #1

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

Solution #2

numpy.array is a function that returns a numpy.ndarray. There is no object type numpy.array.

Answered by Ramón J Romero y Vigil

Solution #3

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

<class 'list'>

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

<class 'numpy.ndarray'>

This indicates that the numpy array’s type is numpy.ndarray.

You can also determine the type by using the following formula:

isinstance(a, (np.ndarray))

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

Solution #4

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:

another example is to assign array object to the buffer 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

Solution #5

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