374

What is the difference between ndarray and array in NumPy? Where is their implementation in the NumPy source code?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
flxb
  • 4,235
  • 3
  • 16
  • 11

5 Answers5

332

numpy.array is just a convenience function to create an ndarray; it is not a class itself.

You can also create an array using numpy.ndarray, but it is not the recommended way. From the docstring of numpy.ndarray:

Arrays should be constructed using array, zeros or empty ... The parameters given here refer to a low-level method (ndarray(...)) for instantiating an array.

Most of the meat of the implementation is in C code, here in multiarray, but you can start looking at the ndarray interfaces here:

https://github.com/numpy/numpy/blob/master/numpy/core/numeric.py

nbro
  • 15,395
  • 32
  • 113
  • 196
wim
  • 338,267
  • 99
  • 616
  • 750
  • 1
    I think array() is implemented in [core/src/multiarray/methods.c](https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/methods.c) in array_getarray(). – flxb Apr 08 '13 at 13:14
  • 14
    This can bite you if you forget that `np.array` is not a class, as I often do. `x = np.array([1,2.1,3])` `if isinstance(x,np.array): # will give you a TypeError` – Steve L Jul 11 '17 at 14:53
  • 9
    Still have no clue why should avoid using ndarray? Coz it's low-level? – GabrielChu Sep 09 '18 at 11:18
  • @flxb: No, `array_getarray` is the implementation of `numpy.ndarray.__array__`. `numpy.array` starts at [`_array_fromobject`](https://github.com/numpy/numpy/blob/v1.15.4/numpy/core/src/multiarray/multiarraymodule.c#L1648), at least in the current implementation. – user2357112 Nov 13 '18 at 19:52
  • 5
    So why is it not recommended? – NoName Dec 05 '19 at 06:05
  • 2
    Usually when you have a type (i.e. `ndarray`) which is complicated to construct factory methods such as `array`,`zeros` etc are provided to ensure that it's constructed correctly. Also perhaps the numpy developers prefer to keep the interfaces of `array`,`zeros` unchanged from release to release but don't make the same guarantees of `ndarray()` – David Waterworth Dec 20 '20 at 02:52
82

numpy.array is a function that returns a numpy.ndarray object.

There is no object of type numpy.array.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125
52

Just a few lines of example code to show the difference between numpy.array and numpy.ndarray

Warm up step: Construct a list

a = [1,2,3]

Check the type

print(type(a))

You will get

<class 'list'>

Construct an array (from a list) using np.array

a = np.array(a)

Or, you can skip the warm up step, directly have

a = np.array([1,2,3])

Check the type

print(type(a))

You will get

<class 'numpy.ndarray'>

which tells you the type of the numpy array is numpy.ndarray

You can also check the type by

isinstance(a, (np.ndarray))

and you will get

True

Either of the following two lines will give you an error message

np.ndarray(a)                # should be np.array(a)
isinstance(a, (np.array))    # should be isinstance(a, (np.ndarray))
stason
  • 5,409
  • 4
  • 34
  • 48
Ying
  • 1,894
  • 1
  • 14
  • 20
15

numpy.ndarray() is a class, while numpy.array() is a method / function to create ndarray.

In numpy docs if you want to create an array from ndarray class you can do it with 2 ways as quoted:

1- using array(), zeros() or empty() methods: Arrays should be constructed using array, zeros or empty (refer to the See Also section below). The parameters given here refer to a low-level method (ndarray(…)) for instantiating an array.

2- from ndarray class directly: There are two modes of creating an array using __new__: If buffer is None, then only shape, dtype, and order are used. If buffer is an object exposing the buffer interface, then all keywords are interpreted.

The example below gives a random array because we didn't assign buffer value:

np.ndarray(shape=(2,2), dtype=float, order='F', buffer=None)

array([[ -1.13698227e+002,   4.25087011e-303],
       [  2.88528414e-306,   3.27025015e-309]])         #random

another example is to assign array object to the buffer example:

>>> np.ndarray((2,), buffer=np.array([1,2,3]),
...            offset=np.int_().itemsize,
...            dtype=int) # offset = 1*itemsize, i.e. skip first element
array([2, 3])

from above example we notice that we can't assign a list to "buffer" and we had to use numpy.array() to return ndarray object for the buffer

Conclusion: use numpy.array() if you want to make a numpy.ndarray() object"

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Mahmoud Elshahat
  • 1,873
  • 10
  • 24
-3

I think with np.array() you can only create C like though you mention the order, when you check using np.isfortran() it says false. but with np.ndarrray() when you specify the order it creates based on the order provided.

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
Sujith Rao
  • 17
  • 3