Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ndarray and array in numpy?

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

like image 833
flxb Avatar asked Apr 08 '13 12:04

flxb


People also ask

Is Ndarray and array the same?

The array object in NumPy is called ndarray . We can create a NumPy ndarray object by using the array() function.

What does Ndarray mean in NumPy?

An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape , which is a tuple of N non-negative integers that specify the sizes of each dimension.

What is the difference between Ndarray and matrix?

Numpy matrices are strictly 2-dimensional, while numpy arrays (ndarrays) are N-dimensional. Matrix objects are a subclass of ndarray, so they inherit all the attributes and methods of ndarrays.

What is the difference between series and Ndarray?

Series can also be passed into most NumPy methods expecting an ndarray. A key difference between Series and ndarray is that operations between Series automatically align the data based on label. Thus, you can write computations without giving consideration to whether the Series involved have the same labels.


3 Answers

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

like image 73
wim Avatar answered Nov 07 '22 20:11

wim


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

There is no object of type numpy.array.

like image 27
Ramón J Romero y Vigil Avatar answered Nov 07 '22 20:11

Ramón J Romero y Vigil


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))
like image 34
Ying Avatar answered Nov 07 '22 20:11

Ying