Numpy matrix를 만들게 되면 shape 메소드를 통해서 그 matrix의 모형을 알아낼 수 있다.

 

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

x.shape
Out[49]: (3,)

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

y.shape
Out[51]: (2, 2)

다음과 같이 1X3 행렬과, 2X2 행렬을 만들었다. 그런데 뭔가 이상하지 않은가? 다음 코드를 보고 판단해보자

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

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

x.shape
Out[55]: (3,)

x2.shape
Out[54]: (1, 3)

두 행렬의 요소는 동일할지언정, 표기법이 다르다. 이는 Numpy matrix의 표기 방식 때문이라고 추정한다.

https://stackoverflow.com/questions/22053050/difference-between-numpy-array-shape-r-1-and-r

 

Difference between numpy.array shape (R, 1) and (R,)

In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplication more tedious since explicit reshape is required. For example, given a matrix M, if...

stackoverflow.com

역시나! Stackoverflow에 비슷한 질문이 있었고, 이 중 충분한 답변이 될만한 부분을 같이 적는다.

 

The shape is a tuple. If there is only 1 dimension the shape will be one number and just blank after a comma. For 2+ dimensions, there will be a number after all the commas.

(모양은 튜플입니다. 만약 1차원이라면 숫자 하나와 콤마 뒤의 빈칸으로 나타나고, 2차원 이상에서는 콤마 뒤에 숫자가 옵니다.)

 

x.ndim
Out[56]: 1

x2.ndim
Out[57]: 2

차원 확인으로 다시 한번 확인해볼 수 있다.

'머신 러닝 및 파이썬 > Numpy 공부' 카테고리의 다른 글

Dictionary 변수 데이터 옮기기  (0) 2020.03.10
행렬 곱셈, 내적  (0) 2020.03.04
Numpy의 log  (0) 2020.02.27
Numpy의 Dimension  (0) 2020.02.27

+ Recent posts