Numpy. section5 : ndarray의 원소별 연산과 브로드캐스팅. Lec19. ndim이 1일 때의 브로드캐스팅

timekeeep·2023년 2월 25일
0

Numpy

목록 보기
18/28

[1] When ndims Are Equal(Matrices)

  • 차원이 1이여야 함
  • (3,3) + (3,1)
  • (3,3) + (1,3)
  • (3,1) + (1,3)

# When ndims Are Equal(Matrices)

#(3,3) + (1,3)
import numpy as np

A = np.arange(9).reshape(3,3)
B = 10*np.arange(3).reshape((-1,3))
C = A + B

print("A: {}/{}\n{}".format(A.ndim, A.shape, A))
print("B: {}/{}\n{}\n".format(A.ndim, B.shape, B))

#(3,3) + (3,1)

A = np.arange(9).reshape(3,3)
B = 10*np.arange(3).reshape((3,-1))
C = A + B

print("A: {}/{}\n{}".format(A.ndim, A.shape, A))
print("B: {}/{}\n{}\n".format(A.ndim, B.shape, B))

print("A + B: {}/{}\n{}".format(A.ndim, C.shape, C))

#(3,1) + (1,3) -> 둘다 1인것이 있기 때문에 브로드캐스팅이 가능함

A = np.arange(3).reshape((3,-1))
B = 10*np.arange(3).reshape((-1, 3))
C = A + B

print("A: {}/{}\n{}".format(A.ndim, A.shape, A))
print("B: {}/{}\n{}\n".format(A.ndim, B.shape, B))

print("A + B: {}/{}\n{}".format(A.ndim, C.shape, C))
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개의 댓글