programing

Numpy 어레이 치수

copysource 2023. 2. 3. 23:19
반응형

Numpy 어레이 치수

어레이의 치수를 취득하려면 어떻게 해야 합니까?예를 들어, 이것은 2x2 입니다.

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

어레이 치수의 튜플을 얻기 위해 사용합니다.

>>> a.shape
(2, 2)

첫 번째:

관례상 Python world의 숏컷은numpynp, 그래서:

In [1]: import numpy as np

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

두 번째:

Numpy에서 치수, 축/축, 모양은 관련이 있으며 때로는 유사한 개념입니다.

치수

수학/물리학에서 치수 또는 치수성은 공간 내의 점을 지정하는 데 필요한 최소 좌표 수로 비공식적으로 정의됩니다.하지만 Numpy의 경우, Numpy 문서에 따르면 축/축과 같습니다.

Numpy에서는 치수를 축이라고 합니다.축의 수는 순위입니다.

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

축/축

인덱스하기 위한 n번째 좌표arrayNumpy로 표시됩니다.또한 다차원 배열은 축당 하나의 인덱스를 가질 수 있습니다.

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
import numpy as np   
>>> np.shape(a)
(2,2)

입력이 numpy 배열이 아니라 목록 목록인 경우에도 작동합니다.

>>> a = [[1,2],[1,2]]
>>> np.shape(a)
(2,2)

아니면 튜플이 더 많거나

>>> a = ((1,2),(1,2))
>>> np.shape(a)
(2,2)

사용하다.shape:

In: a = np.array([[1,2,3],[4,5,6]])
In: a.shape
Out: (2, 3)
In: a.shape[0] # x axis
Out: 2
In: a.shape[1] # y axis
Out: 3

사용할 수 있습니다..ndim치수 및.shape정확한 치수를 알 수 있습니다.

>>> var = np.array([[1,2,3,4,5,6], [1,2,3,4,5,6]])

>>> var.ndim
2

>>> varshape
(2, 6) 

다음을 사용하여 치수를 변경할 수 있습니다..reshape기능:

>>> var_ = var.reshape(3, 4)

>>> var_.ndim
2

>>> var_.shape
(3, 4)

shape방법은 다음과 같습니다.aNumpy ndarray 입니다.그러나 Numpy는 순수한 비단뱀 물체의 반복 가능한 모양도 계산할 수 있습니다.

np.shape([[1,2],[1,2]])

a.shape의 한정판일 뿐입니다.np.info()이것 좀 봐.

import numpy as np
a = np.array([[1,2],[1,2]])
np.info(a)

나가.

class:  ndarray
shape:  (2, 2)
strides:  (8, 4)
itemsize:  4
aligned:  True
contiguous:  True
fortran:  False
data pointer: 0x27509cf0560
byteorder:  little
byteswap:  False
type: int32
rows = a.shape[0] # 2 
cols = a.shape[1] # 2
a.shape #(2,2)
a.size # rows * cols = 4

python 노트북에서 아래 코드 블록을 실행합니다.

import numpy as np
a = np.array([[1,2],[1,2]])
print(a.shape)
print(type(a.shape))
print(a.shape[0])

산출량

(2, 2)

<클래스 '태플'>

2

그때 깨달았구나a.shape태플입니다.그래서 당신은 어떤 차원의 사이즈도 얻을 수 있습니다a.shape[index of dimention]

언급URL : https://stackoverflow.com/questions/3061761/numpy-array-dimensions

반응형