카테고리 없음

python - numpy(9) 전치연산

mini_world 2020. 3. 30. 18:52
목차 접기

 

2차원 배열의 방향(행과 열) 변경

 

import numpy as np

ar = np.array([[1, 2, 3], [4, 5, 6]])
print(ar)  # 2x3
print(ar.T)  # 3x2 전치

# 배열의 크기 변형 : ex) 1*15 --> 3*5, 데이터 변경 없음
a = np.arange(12)
print('print(a):\n', a)
print('print(a.reshape(4, -1)):\n', a.reshape(4, -1))
print(a.reshape(-1, 6))
print(a.reshape(2,2,-2))

b = a.reshape(3, 4)
print(b)
c = b.reshape(4, 3)
print(c)


# 다차원의 배열 펼치기
print(a.flatten())
print(a.ravel())


# ----------------
print('-'*100)
x = np.arange(5)
print(x)
print(x.reshape(1, 5))
print(x.reshape(5, 1))

 

 

[코드 실행 결과]

[[1 2 3]
 [4 5 6]]
[[1 4]
 [2 5]
 [3 6]]
print(a):
 [ 0  1  2  3  4  5  6  7  8  9 10 11]
print(a.reshape(4, -1)):
 [[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]]
[[[ 0  1  2]
  [ 3  4  5]]

 [[ 6  7  8]
  [ 9 10 11]]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
[ 0  1  2  3  4  5  6  7  8  9 10 11]
[ 0  1  2  3  4  5  6  7  8  9 10 11]
----------------------------------------------------------------------------------------------------
[0 1 2 3 4]
[[0 1 2 3 4]]
[[0]
 [1]
 [2]
 [3]
 [4]]

Process finished with exit code 0

 

728x90