Python numpy中的ndarray和array有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15879315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between ndarray and array in numpy?
提问by flxb
What is the difference between ndarrayand arrayin Numpy? And where can I find the implementations in the numpy source code?
Numpyndarray和arrayNumpy 有什么区别?我在哪里可以找到 numpy 源代码中的实现?
采纳答案by wim
numpy.arrayis just a convenience function to create an ndarray; it is not a class itself.
numpy.array只是一个方便的函数来创建一个ndarray; 它本身不是一个类。
You can also create an array using numpy.ndarray, but it is not the recommended way. From the docstring of numpy.ndarray:
您也可以使用 来创建数组numpy.ndarray,但这不是推荐的方式。从文档字符串numpy.ndarray:
Arrays should be constructed using
array,zerosorempty... The parameters given here refer to a low-level method (ndarray(...)) for instantiating an array.
应该使用
array,zeros或empty...构造数组。这里给出的参数是指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
回答by Ramón J Romero y Vigil
numpy.arrayis a function that returns a numpy.ndarray. There is no object type numpy.array.
numpy.array是一个返回 a 的函数numpy.ndarray。没有对象类型 numpy.array。
回答by Ying
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))
回答by Sujith Rao
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.
我认为np.array()你只能创建 C 就像你提到的顺序,当你检查使用np.isfortran()它时说假。但是np.ndarrray()当您指定订单时,它会根据提供的订单创建。
回答by Mahmoud Elshahat
numpy.ndarray()is a class, while numpy.array()is a method / function to create ndarray.
numpy.ndarray()是一个类, whilenumpy.array()是一个方法/函数来创建ndarray。
In numpy docs if you want to create an array from ndarrayclass you can do it with 2 ways as quoted:
在 numpy 文档中,如果你想从ndarray类创建一个数组,你可以用引用的两种方式来完成:
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.
1- using array(), zeros()or empty()methods:
数组应使用数组、零或空来构造(请参阅下面的另请参阅部分)。此处给出的参数是指ndarray(…)用于实例化数组的低级方法 ( )。
2- from ndarrayclass 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.
2-ndarray直接来自类:
有两种使用创建数组的模式__new__:如果缓冲区为无,则仅使用形状、数据类型和顺序。如果 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
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])
>>> 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
从上面的例子我们注意到我们不能将一个列表分配给“缓冲区”,我们不得不使用 numpy.array() 来返回缓冲区的 ndarray 对象
Conclusion: use numpy.array()if you want to make a numpy.ndarray()object"
结论:numpy.array()如果你想制作一个numpy.ndarray()对象,请使用“

