Summarize the matrix operations used in the process of understanding machine learning. I will update it from time to time.
Take the sum for each element. In Python it can be calculated with the + operator.
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
+
\begin{pmatrix}
5 & 6 \\
7 & 8
\end{pmatrix}
=
\begin{pmatrix}
6 & 8 \\
10 & 12
\end{pmatrix}
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(a+b)
[[ 6 8]
[10 12]]
Take the difference for each element. In Python it can be calculated with the --operator.
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
-
\begin{pmatrix}
5 & 6 \\
7 & 8
\end{pmatrix}
=
\begin{pmatrix}
-4 & -4 \\
-4 & -4
\end{pmatrix}
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(a-b)
[[-4 -4]
[-4 -4]]
Take the product for each element. In Python it can be calculated with the * operator.
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
\odot
\begin{pmatrix}
5 & 6 \\
7 & 8
\end{pmatrix}
=
\begin{pmatrix}
5 & 12 \\
21 & 32
\end{pmatrix}
import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A*B)
[[ 5 12]
[21 32]]
Take the sum of multiplying each element of the corresponding row and column. In Python it can be calculated with the dot function. However, the dot function is a ** inner product ** function. When a matrix is taken as an argument, ** the result is a matrix product **.
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
\begin{pmatrix}
5 & 6 \\
7 & 8
\end{pmatrix}
=
\begin{pmatrix}
19 & 22 \\
43 & 50
\end{pmatrix}
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
print(np.dot(a, b))
[[19 22]
[43 50]]
Recommended Posts