ndarray和numpy中的数组有什么区别?

时间:2022-08-28 21:35:18

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

ndarray和Numpy中的数组有什么区别?在numpy源代码中我可以在哪里找到实现?

3 个解决方案

#1


114  

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

嗯,numpy。数组只是创建ndarray的一个方便函数,它本身不是类。

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

您还可以使用numpy创建一个数组。恩达雷,但这不是推荐的方法。来自numpy.ndarray的docstring:

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

数组应该使用数组、0或空来构造……这里给出的参数指的是用于实例化数组的低级方法(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

#2


30  

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

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

#3


9  

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

只需要几行代码来显示numpy的区别。数组和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

您也可以检查类型by

isinstance(a, (np.ndarray))

and you will get

你会得到

True

The following two line will give you error message

下面两行将给出错误消息

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

#1


114  

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

嗯,numpy。数组只是创建ndarray的一个方便函数,它本身不是类。

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

您还可以使用numpy创建一个数组。恩达雷,但这不是推荐的方法。来自numpy.ndarray的docstring:

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

数组应该使用数组、0或空来构造……这里给出的参数指的是用于实例化数组的低级方法(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

#2


30  

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

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

#3


9  

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

只需要几行代码来显示numpy的区别。数组和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

您也可以检查类型by

isinstance(a, (np.ndarray))

and you will get

你会得到

True

The following two line will give you error message

下面两行将给出错误消息

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