[Python] Matrix operation

Summarize the matrix operations used in the process of understanding machine learning. I will update it from time to time.

addition

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]]

subtraction

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]]

Hadamard product (Shua product)

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]]

product

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

[Python] Matrix operation
[python] vector operation
Python OS operation
[Python] Operation of enumerate
Implemented Matrix Factorization (python)
Python directory operation summary
Python decorator operation memo
[python] Array slice operation
Python Math Series ② Matrix Multiplication
Try matrix operation with NumPy
S3 operation with python boto3
Matrix multiplication in python numpy
Transposed matrix in Python standard
Python pywin32 (win32com) Excel operation memorandum
[Python] Operation memo of pandas DataFrame
Matrix representation with Python standard input
Draw a scatterplot matrix in python
[python] week1-3: Number type and operation
Basic Python operation 2nd: Function (argument)
[Automation with python! ] Part 2: File operation
[Python] File operation using if statement
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
kafka python
Mouse operation using Windows API in Python
Python basic operation 1st: List comprehension notation
Python basics ⑤
python + lottery 6
Python Summary
Python comprehension
Python technique
Studying python
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
[Python] Matrix multiplication processing time using NumPy
Python memo
ufo-> python (3)
Python + Selenium Frequently used operation method summary
Python comprehension
install python
Python Singleton
Bit operation
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables