在Python NumPy中,尺寸和轴是什么? [英] In Python NumPy what is a dimension and axis?

查看:193
本文介绍了在Python NumPy中,尺寸和轴是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am coding with Pythons NumPy module. If coordinates of a point in 3D space are described as [1, 2, 1], wouldn't that be three dimensions, three axis, a rank of three? Or if that is one dimension then shouldn't it be points (plural), not point?

以下是文档:

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

In Numpy dimensions are called axes. The number of axes is rank. For example, the coordinates of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis. That axis has a length of 3.

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

推荐答案

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

In numpy arrays, dimensionality refers to the number of axes needed to index it, not the dimensionality of any geometrical space. For example, you can describe the locations of points in 3D space with a 2D array:

array([[0, 0, 0],
       [1, 2, 3],
       [2, 2, 2],
       [9, 9, 9]])

其中shape(4, 3),尺寸为2.但是它可以描述3D空间,因为每行的长度(axis 1)为3,因此每行可以是点位置的x,y和z分量.长度axis 0表示点数(此处为4).但是,更多的是代码所描述的数学应用,而不是数组本身的属性.在数学中,向量的维数就是其长度(例如3d向量的x,y和z分量),但是在numpy中,任何向量"实际上只是被视为长度可变的1d数组.数组不在乎所描述的空间的大小(如果有).

Which has shape of (4, 3) and dimension 2. But it can describe 3D space because the length of each row (axis 1) is three, so each row can be the x, y, and z component of a point's location. The length of axis 0 indicates the number of points (here, 4). However, that is more of an application to the math that the code is describing, not an attribute of the array itself. In mathematics, the dimension of a vector would be its length (e.g., x, y, and z components of a 3d vector), but in numpy, any "vector" is really just considered a 1d array of varying length. The array doesn't care what the dimension of the space (if any) being described is.

您可以使用它,并像这样查看数组的维数和形状:

You can play around with this, and see the number of dimensions and shape of an array like so:

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)

数组可以具有许多维,但是在两三个或三个以上的数组中就很难看到它们了:

Arrays can have many dimensions, but they become hard to visualize above two or three:

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)

这篇关于在Python NumPy中,尺寸和轴是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆