IT TIP

Python NumPy에서 차원과 축이란 무엇입니까?

itqueen 2020. 10. 16. 19:17
반응형

Python NumPy에서 차원과 축이란 무엇입니까?


Pythons NumPy모듈로 코딩 중입니다 . 3D 공간에서 한 점의 좌표가로 설명 [1, 2, 1]되면 3 차원, 3 축, 3 위가 아닐까요? 아니면 그것이 하나의 차원이라면 그것은 점이 아니라 점 (복수)이어야하지 않습니까?

다음은 문서입니다.

Numpy에서 차원은 축이라고합니다. 축의 수는 순위입니다. 예를 들어, 3D 공간 [1, 2, 1]에있는 점의 좌표는 축이 하나이기 때문에 순위 1의 배열입니다. 해당 축의 길이는 3입니다.

출처 : http://wiki.scipy.org/Tentative_NumPy_Tutorial


numpy arrays에서 차원 성은 axes기하학적 공간의 차원이 아니라 인덱스 필요한 수를 나타냅니다 . 예를 들어, 2D 배열을 사용하여 3D 공간에서 점의 위치를 ​​설명 할 수 있습니다.

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

어떤이 shape(4, 3)치수 2. 그러나 각 행 ( axis1) 의 길이 가 3 이기 때문에 3D 공간을 설명 할 수 있습니다. 따라서 각 행은 점 위치의 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)

배열은 여러 차원을 가질 수 있지만 2 ~ 3 개 이상에서는 시각화하기 어렵습니다.

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)

누군가이 시각적 설명이 필요한 경우 :

numpy axis 0 and axis 1


색인화하려면 하나의 색인이 필요하므로 순위 1입니다. 인덱스 인덱싱으로 세 가지 다른 값을 사용할 수 있으므로 해당 축의 길이는 3 v[i], i=0..2입니다.


답변 에서 답변의 일부를 붙여 넣으십시오 .

Numpy에서 dimension , axis / axes , shape 는 관련이 있으며 때로는 유사한 개념입니다.

In [1]: import numpy as np

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

치수

에서는 수학 / 물리 치수 또는 비공식적 차원 공간 내의 임의의 점을 지정하기 위해 필요한 좌표의 최소 개수로 정의된다. 그러나 Numpy 에서는 numpy doc 에 따르면 축 / 축과 동일합니다.

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

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

axis/axes

the nth coordinate to index an array in Numpy. 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)

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

You can also use axis parameter 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.

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])

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.

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.

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.

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).

참고URL : https://stackoverflow.com/questions/19389910/in-python-numpy-what-is-a-dimension-and-axis

반응형