numpy中的ndarray和array有什么区别?

问题:numpy中的ndarray和array有什么区别?

ndarrayarrayNumpy有什么区别?我在哪里可以找到numpy源代码中的实现?

What is the difference between ndarray and array in Numpy? And where can I find the implementations in the numpy source code?


回答 0

numpy.array只是创建一个便利函数ndarray; 它本身不是类。

您也可以使用创建数组numpy.ndarray,但不建议这样做。来自的文档字符串numpy.ndarray

阵列应该使用来构造arrayzerosempty…这里给出的参数是指低级方法(ndarray(...)用于实例化阵列)。

实现的大部分内容都在C代码中(在multiarray中),但是您可以在这里开始查看ndarray接口:

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

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


回答 1

numpy.array是一个返回的函数numpy.ndarray。没有对象类型numpy.array。

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


回答 2

只需几行示例代码即可显示numpy.array和numpy.ndarray之间的区别

热身步骤:构建列表

a = [1,2,3]

检查类型

print(type(a))

你会得到

<class 'list'>

使用np.array构造一个数组(从列表中)

a = np.array(a)

或者,您可以跳过热身步骤,直接进行

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

检查类型

print(type(a))

你会得到

<class 'numpy.ndarray'>

告诉你numpy数组的类型是numpy.ndarray

您还可以通过以下方式检查类型

isinstance(a, (np.ndarray))

你会得到

True

以下两行均会给您一条错误消息

np.ndarray(a)                # should be np.array(a)
isinstance(a, (np.array))    # should be isinstance(a, (np.ndarray))

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))

回答 3

numpy.ndarray()是一个类,numpy.array()而是要创建的方法/函数ndarray

在numpy docs中,如果您想从ndarray类创建数组,则可以使用以下两种方式进行引用:

1-使用array()zeros()empty()方法: 阵列应该使用数组,零或空构造(参考也参见下文部分)。此处给出的参数指的是ndarray(…)用于实例化数组的低级方法()。

2- ndarray直接来自类: 有两种创建数组的方式__new__:使用:如果buffer为None,则仅使用shape,dtype和order。如果buffer是暴露buffer接口的对象,则将解释所有关键字。

下面的示例给出了一个随机数组,因为我们没有分配缓冲区值:

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

另一个示例是将数组对象分配给缓冲区示例:

>>> 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])

从上面的示例中,我们注意到我们无法为“缓冲区”分配列表,我们不得不使用numpy.array()返回缓冲区的ndarray对象

结论:numpy.array()如果要制造numpy.ndarray()物体,则使用“

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”


回答 4

我认为与np.array()您一起只能创建C,尽管您提到了该命令,但是当您使用np.isfortran()它检查时说是false。但是,np.ndarrray()当您指定订单时,它将根据提供的订单创建订单。

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.