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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19389910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:38:44  来源:igfitidea点击:

In Python NumPy what is a dimension and axis?

pythonnumpy

提问by davidjhp

I am coding with Pythons NumPymodule. 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?

我正在使用 PythonsNumPy模块进行编码。如果一个点在 3D 空间中的坐标被描述为[1, 2, 1],那不就是三个维度,三个轴,一个三阶吗?或者,如果那是一维,那么它不应该是点(复数),而不是点?

Here is the documentation:

这是文档:

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.

在 Numpy 中,维度被称为轴。轴的数量是等级。例如,3D 空间 [1, 2, 1] 中一个点的坐标是一个秩为 1 的数组,因为它有一个轴。该轴的长度为 3。

Source: http://wiki.scipy.org/Tentative_NumPy_Tutorial

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

采纳答案by askewchan

In numpy arrays, dimensionality refers to the number of axesneeded 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:

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

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

Which has shapeof (4, 3)and dimension 2. But it can describe 3D space because the length of each row (axis1) is three, so each row can be the x, y, and z component of a point's location. The length of axis0 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.

其中有shapeof(4, 3)和 维度2。但它可以描述 3D 空间,因为每行 ( axis1)的长度是 3,所以每行可以是点位置的 x、y 和 z 分量。axis0的长度表示点数(此处为 4)。然而,这更像是代码描述的数学应用,而不是数组本身的属性。在数学中,向量的维度是它的长度(例如,3d 向量的 x、y 和 z 分量),但在 numpy 中,任何“向量”实际上只是被认为是长度可变的一维数组。该数组不关心所描述的空间(如果有的话)的维度是什么。

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)

回答by Bálint Aradi

It is of rank one, as you need one index to index it. That one axis has the length 3, as the index indexing it can take three different values: v[i], i=0..2.

它的等级为 1,因为您需要一个索引来索引它。该轴的长度为 3,作为索引索引,它可以采用三个不同的值:v[i], i=0..2.

回答by YaOzI

Just paste part of answer from this answer:

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

In Numpy, dimension, axis/axes, shapeare related and sometimes similar concepts:

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

In [1]: import numpy as np

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

dimension

尺寸

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:

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

In Numpy dimensions are called axes. The number of axes is rank.

在 Numpy 中,维度被称为轴。轴的数量是等级。

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

axis/axes

轴/轴

the nthcoordinate to index an arrayin Numpy. And multidimensional arrays can have one index per axis.

在 Numpy 中索引 an的第 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)

shape

形状

describes how many data 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

回答by Alex Granovsky

You can also use axisparameter in group operations, in case of axis=0 Numpy performs the action on elements of each column, and if axis=1, it performs the action on rows.

您也可以在分组操作中使用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])

回答by Ludex

This is how I understand it. A point is a 1D object. You can only define its position. It has no dimensions. A line or surface is a 2D object. You can define it by both its position and length or area respectively e.g. Rectangle, Square, Circle A volume is a 3D object. You can define it by its position, surface area/lengths and volume e.g. Sphere, Cube.

我是这样理解的。点是一维对象。您只能定义其位置。它没有维度。线或面是 2D 对象。您可以分别通过其位置和长度或面积来定义它,例如矩形、正方形、圆形 体积是 3D 对象。您可以通过它的位置、表面积/长度和体积来定义它,例如球体、立方体。

From this, you will define a point in NumPy by a single axis (dimension), regardless of the number of mathematical axes you use. For x and y axes, a point is defined as [2,4], and for x, y and z axes, a point is defined as [2,4,6]. Both of these are points, thus 1D.

由此,您将通过单个轴(维度)在 NumPy 中定义一个点,而不管您使用的数学轴的数量如何。对于 x 和 y 轴,点定义为 [2,4],对于 x、y 和 z 轴,点定义为 [2,4,6]。这两个都是点,因此是 1D。

To define a line, two points will be needed. This will require some form of 'nesting' of the points to the second dimension (2D). As such, a line may be defined using x and y only as [[2,4],[6,9]] or using x, y and z as [[2,4,6],[6,9,12]]. For a surface, it will simply require more points to describe it, but still remains a 2D object. For example, a triangle will need 3 points while a rectangle/square will need 4.

要定义一条线,需要两个点。这将需要将点以某种形式“嵌套”到第二维 (2D)。因此,一条线可以仅使用 x 和 y 定义为 [[2,4],[6,9]] 或使用 x、y 和 z 定义为 [[2,4,6],[6,9,12 ]]。对于一个表面,它只需要更多的点来描述它,但仍然是一个 2D 对象。例如,三角形需要 3 个点,而矩形/正方形需要 4 个点。

A volume will require 4 (a tetrahedron)or more points to define it , but still maintaining the 'nesting' of points to the third dimension (3D).

一个体积将需要 4 个(一个四面体)或更多点来定义它,但仍然保持点到第三维 (3D) 的“嵌套”。

回答by debaonline4u

If someone need this visual description:

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

numpy axis 0 and axis 1

numpy 轴 0 和轴 1