第一:
按照惯例,在 Python 世界中,numpy 的快捷方式是 np,所以:
In [1]: import numpy as np
In [2]: a = np.array([[1,2],[3,4]])
第二:
在 Numpy 中,dimension、axis/axes、shape 是相关且有时相似的概念:
尺寸
在数学/物理学中,维度或维度被非正式地定义为指定空间内任何点所需的最小坐标数。但是在Numpy中,根据numpy doc,它和axis/axes是一样的:
在 Numpy 中,维度被称为轴。轴数为rank。
In [3]: a.ndim # num of dimensions/axes, *Mathematics definition of dimension*
Out[3]: 2
轴/轴
nth 坐标在 Numpy 中索引 array。并且多维数组每个轴可以有一个索引。
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