NumPy で行列の足し算と引き算を行う
2023.02.18
NumPy では行列を +
で足します。行列の足し算は数の足し算と同じです。
import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.array([[1, 1],
[1, 1]])
C = A + B
print(C)
# [[2 3]
# [4 5]]
行列の引き算:
import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.array([[1, 1],
[1, 1]])
C = A - B
print(C)
# [[0 1]
# [2 3]]
単位行列を使った例:
import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.eye(2)
C = A - B
print(C)
# [[0. 2.]
# [3. 3.]]