简体   繁体   English

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

[英]What is the difference between ndarray and array in numpy?

What is the difference between ndarray and array in Numpy? Numpy 中的ndarrayarray什么区别? And where can I find the implementations in the numpy source code?我在哪里可以找到 numpy 源代码中的实现?

numpy.array is just a convenience function to create an ndarray ; numpy.array只是一个创建ndarray的便捷函数; it is not a class itself.它本身不是一个类。

You can also create an array using numpy.ndarray , but it is not the recommended way.您也可以使用numpy.ndarray创建一个数组,但这不是推荐的方法。 From the docstring of numpy.ndarray :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.数组应该使用arrayzerosempty构造......这里给出的参数是指用于实例化数组的低级方法( ndarray(...) )。

Most of the meat of the implementation is in C code, here in multiarray , but you can start looking at the ndarray interfaces here:实现的大部分内容都在 C 代码中, 这里是 multiarray ,但您可以从这里开始查看 ndarray 接口:

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

numpy.array is a function that returns a numpy.ndarray . numpy.array是一个返回numpy.ndarray的函数。 There is no object type numpy.array.没有对象类型 numpy.array。

Just a few lines of example code to show the difference between numpy.array and numpy.ndarray只需几行示例代码即可显示 numpy.array 和 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使用 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它告诉你numpy 数组的类型是 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))

numpy.ndarray() is a class, while numpy.array() is a method / function to create ndarray . numpy.ndarray()是一个类,而numpy.array()是一个创建ndarray的方法/函数。

In numpy docs if you want to create an array from ndarray class you can do it with 2 ways as quoted:在 numpy docs 中,如果你想从ndarray类创建一个数组,你可以用 2 种方法来实现:

1- using array() , zeros() or empty() methods: Arrays should be constructed using array, zeros or empty (refer to the See Also section below). 1- 使用array()zeros()empty()方法:应该使用数组、零或空构造数组(请参阅下面的另请参阅部分)。 The parameters given here refer to a low-level method ( ndarray(…) ) for instantiating an array.这里给出的参数是指用于实例化数组的低级方法( ndarray(…) )。

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. 2- 直接来自ndarray类:使用__new__创建数组有两种模式:如果 buffer 为 None,则仅使用 shape、dtype 和 order。 If buffer is an object exposing the buffer interface, then all keywords are interpreted.如果 buffer 是一个暴露缓冲区接口的对象,那么所有的关键字都会被解释。

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, ie 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从上面的例子我们注意到我们不能将一个列表分配给“缓冲区”,我们不得不使用 numpy.array() 来返回缓冲区的 ndarray 对象

Conclusion: use numpy.array() if you want to make a numpy.ndarray() object"结论:使用numpy.array()如果你想使一个numpy.ndarray()对象”

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.我认为使用np.array()您只能创建 C,尽管您提到了顺序,但当您使用np.isfortran()检查时,它显示为 false。 but with np.ndarrray() when you specify the order it creates based on the order provided.但是当您根据提供的顺序指定它创建的顺序时,使用np.ndarrray()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM