[英]Numpy array dimensions
How do I get the dimensions of an array?如何获取数组的维度? For instance, this is 2x2:
例如,这是 2x2:
a = np.array([[1,2],[3,4]])
By convention, in Python world, the shortcut for numpy
is np
, so:按照惯例,在 Python 世界中,
numpy
的快捷方式是np
,所以:
In [1]: import numpy as np
In [2]: a = np.array([[1,2],[3,4]])
In Numpy, dimension , axis/axes , shape are related and sometimes similar concepts:在 Numpy 中,维度、轴/轴、形状是相关的,有时是相似的概念:
In Mathematics/Physics , dimension or dimensionality is informally defined as the minimum number of coordinates needed to specify any point within a space.在数学/物理中,维度或维度被非正式地定义为指定空间内任何点所需的最小坐标数。 But in Numpy , according to the numpy doc , it's the same as axis/axes:
但是在Numpy中,根据numpy doc ,它与轴/轴相同:
In Numpy dimensions are called axes.
在 Numpy 中,维度称为轴。 The number of axes is rank.
轴的数量是等级。
In [3]: a.ndim # num of dimensions/axes, *Mathematics definition of dimension*
Out[3]: 2
the nth coordinate to index an array
in Numpy.在 Numpy 中索引
array
的第 n 个坐标。 And multidimensional arrays can have one index per axis.多维数组每个轴可以有一个索引。
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)
describes how many data (or the range) along each available axis.描述沿每个可用轴有多少数据(或范围)。
In [5]: a.shape
Out[5]: (2, 2) # both the first and second axis have 2 (columns/rows/pages/blocks/...) data
import numpy as np
>>> np.shape(a)
(2,2)
Also works if the input is not a numpy array but a list of lists如果输入不是 numpy 数组而是列表列表,也可以使用
>>> a = [[1,2],[1,2]]
>>> np.shape(a)
(2,2)
Or a tuple of tuples或者一个元组的元组
>>> a = ((1,2),(1,2))
>>> np.shape(a)
(2,2)
Use .shape
:使用
.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
You can use .ndim
for dimension and .shape
to know the exact dimension:您可以使用
.ndim
作为尺寸,使用.shape
来了解确切的尺寸:
>>> var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]])
>>> var.ndim
2
>>> varshape
(2, 6)
You can change the dimension using .reshape
function:您可以使用
.reshape
函数更改尺寸:
>>> var_ = var.reshape(3, 4)
>>> var_.ndim
2
>>> var_.shape
(3, 4)
The shape
method requires that a
be a Numpy ndarray. shape
方法要求a
是 Numpy ndarray。 But Numpy can also calculate the shape of iterables of pure python objects:但是 Numpy 也可以计算纯 python 对象的迭代形状:
np.shape([[1,2],[1,2]])
a.shape
is just a limited version of np.info()
. a.shape
只是np.info()
的有限版本。 Check this out:看一下这个:
import numpy as np
a = np.array([[1,2],[1,2]])
np.info(a)
Out出去
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
rows = a.shape[0] # 2
cols = a.shape[1] # 2
a.shape #(2,2)
a.size # rows * cols = 4
Execute below code block in python notebook.在 python notebook 中执行下面的代码块。
import numpy as np
a = np.array([[1,2],[1,2]])
print(a.shape)
print(type(a.shape))
print(a.shape[0])
output输出
(2, 2)
(2, 2)
<class 'tuple'>
<类'元组'>
2
2
then you realized that a.shape
is a tuple.然后你意识到
a.shape
是一个元组。 so you can get any dimension's size by a.shape[index of dimention]
所以你可以通过
a.shape[index of dimention]
获得任何维度的大小
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.