Numpy数组的维度

434

如何获取数组的维度大小?例如,这是一个2x2的数组:

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

38
建议:在NumPy中,您所说的“dimensions”被称为“shape”。而在您的情况中,“dimension”的值是2(即“ndim”)。了解常见的NumPy术语很有用,可以更轻松地阅读文档! - Eric O. Lebigot
10个回答

577
使用.shape方法可以获取数组的维度元组:
>>> a.shape
(2, 2)

30
注意:shape更准确地描述为属性而不是函数,因为它不是使用函数调用语法调用的。 - Brent Bradburn
19
实际上,它是一个"属性"(实际上它既是属性又是函数)。 - wim
1
更具体地说,@wim指的是[属性是一个类](https://docs.python.org/3/library/functions.html#property)。对于类属性(您放在类中的属性),它们是作为类属性公开的property类型对象。在Python中,属性是指跟在点号后面的名称。[参见链接](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces)。 - Pedro Rodrigues
2
如果你真的想挑剔的话,它是一个描述符。虽然 property 本身是一个类,但 ndarray.shape 不是一个类,它是属性类型的实例。 - wim

91

首先:

按照惯例,在Python世界中,numpy的快捷方式是np,因此:

In [1]: import numpy as np

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

第二部分:

在Numpy中,dimension(维度)axis/axes(轴)shape(形状)是相关且有时类似的概念:

dimension(维度)

数学/物理 中,维度或维数通常被定义为指定空间内任何点所需的最小坐标数。但是在 Numpy 中,根据numpy文档,它与 axis/axes 相同:

在Numpy中,维度称为轴。轴的数量称为秩。

In [3]: a.ndim  # num of dimensions/axes, *Mathematics definition of dimension*
Out[3]: 2

轴/坐标轴

在Numpy中用于索引一个数组的第n个坐标。而多维数组可以每个轴有一个索引。

In [4]: a[1,0]  # to index `a`, we specific 1 at the first axis and 0 at the second axis.
Out[4]: 3  # which results in 3 (locate at the row 1 and column 0, 0-based index)

形状

描述每个可用轴上有多少数据(或范围)。

In [5]: a.shape
Out[5]: (2, 2)  # both the first and second axis have 2 (columns/rows/pages/blocks/...) data

48
import numpy as np   
>>> np.shape(a)
(2,2)

如果输入不是 numpy 数组而是列表的列表,则仍然有效。

>>> a = [[1,2],[1,2]]
>>> np.shape(a)
(2,2)

或者是一个元组的元组

>>> a = ((1,2),(1,2))
>>> np.shape(a)
(2,2)

np.shape 首先将其参数转换为数组,如果它没有形状属性。这就是为什么它适用于列表和元组示例的原因。 - hpaulj

21

使用.shape

In: a = np.array([[1,2,3],[4,5,6]])
In: a.shape
Out: (2, 3)
In: a.shape[0] # x axis
Out: 2
In: a.shape[1] # y axis
Out: 3

17
你可以使用.ndim来获取维度信息,使用.shape来了解确切的维度:
>>> var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]])

>>> var.ndim
2

>>> var.shape
(2, 6) 

你可以使用.reshape函数来改变维度。
>>> var_ = var.reshape(3, 4)

>>> var_.ndim
2

>>> var_.shape
(3, 4)

8

shape方法要求a是Numpy ndarray。但是Numpy也可以计算纯Python对象的可迭代对象的形状:

np.shape([[1,2],[1,2]])

3

a.shape 只是 np.info() 的一个简化版本。看一下这个例子:

import numpy as np
a = np.array([[1,2],[1,2]])
np.info(a)

输出

class:  ndarray
shape:  (2, 2)
strides:  (8, 4)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  False
data pointer: 0x27509cf0560
byteorder:  little
byteswap:  False
type: int32

1
rows = a.shape[0] # 2 
cols = a.shape[1] # 2
a.shape #(2,2)
a.size # rows * cols = 4

0
在Python笔记本中执行以下代码块。
import numpy as np
a = np.array([[1,2],[1,2]])
print(a.shape)
print(type(a.shape))
print(a.shape[0])

输出

(2, 2)

<class 'tuple'>

2

然后你意识到a.shape是一个元组。 因此,您可以通过a.shape[维度的索引]获取任何维度的大小。


0
由于numpy数组的维度存储在shape属性中,因此也可以使用getattr()函数。
arr = np.arange(8).reshape(2,2,2)
getattr(arr, 'shape')   # (2, 2, 2)

如果你需要将它与其他属性的动态列表一起使用,这将非常有用。一个例子可能是:
properties = ['shape', 'ndim', 'size']
d = {prop: getattr(arr, prop) for prop in properties}
# {'shape': (2, 2, 2), 'ndim': 3, 'size': 8}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接