[Python] 벡터의 선형변환, 표준기저 구현
[In] # 벡터 그리기 %matplotlib inline import numpy as np import matplotlib.pyplot as plt # 화살표를 그리는 함수 def arrow(start, size, color): plt.quiver(start[0], start[1], size[0], size[1], angles = 'xy', scale_units = 'xy', scale = 1, color = color) # 화살표의 시작점 s = np.array([0,0]) # 벡터 a = np.array([2,3]) # 세로 벡터 arrow(s, a, color = 'black') # 그래프 표시 plt.xlim([-3, 3]) plt.ylim([-3, 3]) plt.xlabel('x', size ..
[Python] 전치행렬와 행렬곱 구현
numpy로 배열을 생성 후, 배열명 뒤에 '.T'를 붙여주면 전치행렬이 된다. [In] import numpy as np a = np.array([[0,1,2], [1,2,3]]) # 2x3 행렬 print(a.T) # a의 전치 행렬 [Out] [[0 1] [1 2] [2 3]] 정상적으로 3x2 행렬로 변환된 것을 확인할 수 있다. 전치행렬을 이용해 행렬곱을 가능하게 만들 수 있다. [In] import numpy as np a = np.array([[0,1,2], [1,2,3]]) # 2x3 행렬 b = np.array([[0,1,2], [1,2,3]]) # 2x3 행렬 # print(np.dot(a*b)) # 전치하지 않고 행렬곱을 하면 에러가 발생! print(np.dot(a, b.T)) [O..
[Python] 행렬의 곱, 요소별 곱(아디마르 곱) 표시
numpy의 dot() 함수로 간단하게 구현할 수 있다. [In] import numpy as np a = np.array([[0,1,2], [1,2,3]]) # 2x3 행렬 b = np.array([[2,1], [2,1], [2,1]]) # 3x2 행렬 print(np.dot(a,b)) # 2x2 행렬 [Out] [[ 6 3] [12 6]] Python에서는 * 기호를 통해 요소별 곱(아디마르 곱)을 실행할 수 있다. [In] import numpy as np a = np.array([[0,1,2], [3,4,5], [6,7,8]]) # 3x3 행렬 b = np.array([[0,1,2], [2,0,1], [1,2,0]]) # 3x3 행렬 print(a*b) [Out] [[ 0 1 4] [ 6 0 5]..