在Python NumPy中,什么是维度和轴?

dav*_*jhp 76 python numpy

我正在使用Pythons NumPy模块进行编码.如果将3D空间中的点的坐标描述为[1, 2, 1],则不是三维,三轴,三等级?或者,如果这是一个维度,那么它不应该是点(复数),而不是点?

这是文档:

在Numpy中,尺寸称为轴.轴数是等级.例如,3D空间[1,2,1]中的点的坐标是等级1的数组,因为它具有一个轴.该轴的长度为3.

资料来源:http://wiki.scipy.org/Tentative_NumPy_Tutorial

ask*_*han 87

在numpy中array,维度是指axes索引它所需的数量,而不是任何几何空间的维度.例如,您可以使用2D数组描述3D空间中点的位置:

array([[0, 0, 0],
       [1, 2, 3],
       [2, 2, 2],
       [9, 9, 9]])
Run Code Online (Sandbox Code Playgroud)

它具有shape(4, 3)和尺寸2.但它可以描述3D空间,因为每行(axis1)的长度为3,因此每行可以是点位置的x,y和z分量.长度为axis0表示点数(此处为4).但是,这更像是代码所描述的数学应用程序,而不是数组本身的属性.在数学中,向量的维数将是它的长度(例如,3d向量的x,y和z分量),但是在numpy中,任何"向量"实际上只被认为是不同长度的1d数组.数组不关心所描述的空间(如果有的话)的尺寸.

您可以使用它,并查看数组的尺寸和形状的数量,如下所示:

In [262]: a = np.arange(9)

In [263]: a
Out[263]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])

In [264]: a.ndim    # number of dimensions
Out[264]: 1

In [265]: a.shape
Out[265]: (9,)

In [266]: b = np.array([[0,0,0],[1,2,3],[2,2,2],[9,9,9]])

In [267]: b
Out[267]: 
array([[0, 0, 0],
       [1, 2, 3],
       [2, 2, 2],
       [9, 9, 9]])

In [268]: b.ndim
Out[268]: 2

In [269]: b.shape
Out[269]: (4, 3)
Run Code Online (Sandbox Code Playgroud)

数组可以有很多维度,但它们很难在两三个上面可视化:

In [276]: c = np.random.rand(2,2,3,4)

In [277]: c
Out[277]: 
array([[[[ 0.33018579,  0.98074944,  0.25744133,  0.62154557],
         [ 0.70959511,  0.01784769,  0.01955593,  0.30062579],
         [ 0.83634557,  0.94636324,  0.88823617,  0.8997527 ]],

        [[ 0.4020885 ,  0.94229555,  0.309992  ,  0.7237458 ],
         [ 0.45036185,  0.51943908,  0.23432001,  0.05226692],
         [ 0.03170345,  0.91317231,  0.11720796,  0.31895275]]],


       [[[ 0.47801989,  0.02922993,  0.12118226,  0.94488471],
         [ 0.65439109,  0.77199972,  0.67024853,  0.27761443],
         [ 0.31602327,  0.42678546,  0.98878701,  0.46164756]],

        [[ 0.31585844,  0.80167337,  0.17401188,  0.61161196],
         [ 0.74908902,  0.45300247,  0.68023488,  0.79672751],
         [ 0.23597218,  0.78416727,  0.56036792,  0.55973686]]]])

In [278]: c.ndim
Out[278]: 4

In [279]: c.shape
Out[279]: (2, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)


deb*_*e4u 28

如果有人需要这种视觉描述:

numpy轴0和轴1

  • /sf/ask/1550470911/ (2认同)

Bál*_*adi 9

它是排名第一,因为你需要一个索引来索引它.一个轴的长度为3,因为索引索引它可以采用三个不同的值:v[i], i=0..2.


YaO*_*OzI 7

只需粘贴此答案的部分答案:

在Numpy中,尺寸,轴/轴,形状是相关的,有时是相似的概念:

In [1]: import numpy as np

In [2]: a = np.array([[1,2],[3,4]])
Run Code Online (Sandbox Code Playgroud)

尺寸

数学/物理学中,维度或维度被非正式地定义为指定空间中任何点所需的最小坐标数.但在Numpy中,根据numpy doc,它与轴/轴相同:

在Numpy中,尺寸称为轴.轴数是等级.

In [3]: a.ndim  # num of dimensions/axes, *Mathematics definition of dimension*
Out[3]: 2
Run Code Online (Sandbox Code Playgroud)

轴/轴

在Numpy中索引a 的第n个坐标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)
Run Code Online (Sandbox Code Playgroud)

形状

描述每个可用轴上有多少数据.

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


Ale*_*sky 5

也可以在分组操作中使用axis参数,如果axis=0 Numpy对每列的元素执行操作,如果axis=1,则对行执行操作。

test = np.arange(0,9).reshape(3,3)

Out[3]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

test.sum(axis=0)
Out[5]: array([ 9, 12, 15])

test.sum(axis=1)
Out[6]: array([ 3, 12, 21])
Run Code Online (Sandbox Code Playgroud)