Numpy. section4 : ndarray 바꾸기. Lec11. ndarray의 모양 바꾸기(np.reshape)

timekeeep·2023년 2월 23일
0

Numpy

목록 보기
10/28

[0] 전체

[1] Changing ndarray's shapes

  • shape은 tensor연산에서 중요한 역할을 한다.

(1) numpy.reshape

a = np.arange(6)
b = np.reshape(a, (2,3))

print("original ndarray: \n", a)
print("reshaped ndarray: \n", b)

'''
original ndarray: 
 [0 1 2 3 4 5]    
reshaped ndarray: 
 [[0 1 2]
 [3 4 5]]
'''

a = np.arange(24)
b = np.reshape(a, (2,3,4))

print("original ndarray: \n", a)
print("reshaped ndarray: \n", b)

'''
original ndarray:
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
reshaped ndarray:
 [[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
'''

(2) ndarray.reshape

  • 타깃이 되는 array.reshape해주면 됌
import numpy as np

a = np.arange(6)
b = np.reshape(a, (2,3))

print("original ndarray: \n", a)
print("reshaped ndarray: \n", b)

'''
original ndarray: 
 [0 1 2 3 4 5]    
reshaped ndarray: 
 [[0 1 2]
 [3 4 5]]
'''

a = np.arange(24)
b = np.reshape(a, (2,3,4))

print("original ndarray: \n", a)
print("reshaped ndarray: \n", b)

'''
original ndarray:
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
reshaped ndarray:
 [[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
'''

# ndarray.reshape

a = np.arange(6)
b = a.reshape((2,3))

print("original ndarray: \n", a)
print("reshaped ndarray: \n", b, '\n')


b = a.reshape((2,3,4))

print("original ndarray: \n", a)
print("reshaped ndarray: \n", b, '\n')


# np.reshape and ndarray.reshape

a = np.random.randint(0, 100, (100,)) # 20명의 학생과 5개의 과목점수

print(a.reshape((20,5)).mean(axis = 0).max())
print(np.max(np.mean(np.reshape(a,(20,5)), axis = 0)))
profile
Those who are wise will shine like the brightness of the heavens, and those who lead many to righteousness, like the stars for ever and ever

0개의 댓글