[PYTHON] Numpy Memorandum_Matrix

Basic matrix creation and calculation using Numpy

(Memorandum of understanding as an e-qualification measure)

n-by-m matrix

As an example, create $ A = \ begin {pmatrix} 1 & 1 \\ 2 & 4 \\ 3 & 1 \ end {pmatrix} $ in 3 rows and 2 columns.

>>> A=np.array([[1,1],[2,4],[3,1]])
>>>
>>> A
array([[1, 1],
       [2, 4],
       [3, 1]])
Check the number of rows, columns, and elements of the matrix

In the following example, get the number of rows and columns with shape, and get the total number of elements with size.

>>> A=np.array([[1,2,3],[4,3,2]])
>>>
>>> A.shape   ##Number of rows / columns
(2, 3)
>>>
>>> A.shape[0]   ##The number of rows and columns can be output separately.
2
>>>
>>> A.shape[1]
3
>>> row, col = A.shape   ##The number of rows and columns can be set to separate variables
>>>
>>> row
2
>>>
>>> col
3
Determined as ** square matrix **

A matrix in which the number of row elements and the number of column elements match. The size is n rows and n columns and becomes a square shape

In the example below, $ A = \ begin {pmatrix} 1 & 2 & 3 \\ 4 & 3 & 2 \ end {pmatrix} $ and $ \ begin {pmatrix} 1 & 2 \\ 2 & 3 \ end {pmatrix } $ Finds the number of rows $ .shape [0] $ and the number of columns $ .shape [1] $, respectively, and compares the values to determine if it is a square matrix. $ B $ is True and a square matrix.

>>> A=np.array([[1,2,3],[4,3,2]])
>>>
>>> np.array_equal(A.shape[0],A.shape[1])
False
>>>
>>> B=np.array([[1,1],[2,4]])
>>>
>>> np.array_equal(B.shape[0],B.shape[1])
True
Matrix with random integers as elements

Example 1 creates a 1-by-3 matrix using random numbers Example 2 creates a 2-by-3 matrix using random integers up to 3-6 (not including 7)


>>> np.random.rand(3)   ###Example 1
array([0.4449089 , 0.70920233, 0.59915992])

>>> np.random.randint(3,7,size=(2,3))   ###Example 2
array([[3, 6, 4],
       [5, 3, 6]])
** zero matrix **

Create a matrix with all elements 0

As an example, create a 2-by-2 zero matrix $ W = \ begin {pmatrix} 0 & 0 \\ 0 & 0 \ end {pmatrix} $

>>> W=np.zeros((2,2,))
>>>
>>> W
array([[0., 0.],
       [0., 0.]])
** Matrix with all elements 1 **

As an example, create $ W = \ begin {pmatrix} 1 & 1 \\ 1 & 1 \ end {pmatrix} $ where all elements in 2 rows and 2 columns are 1.

>>> W=np.ones((2,2,))
>>>
>>> W
array([[1., 1.],
       [1., 1.]])
** Identity matrix **

All elements on the main diagonal are 1 and all others are 0. The symbol is indicated as $ E $.

As an example, create $ E = \ begin {pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \ end {pmatrix} $ in 3 rows and 3 columns.

>>> E = np.eye(3)
>>>
>>> E
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
** transposed matrix **

A matrix of $ n $ row $ m $ column that can be created by swapping the elements of the $ m $ row $ n $ column matrix. The transposed matrix is shown as $ A ^ {T} $ for the matrix $ A $

For example, $ \ begin {pmatrix} 2 & 3 & 1 \\ 5 & 7 & 0 \ end {pmatrix} $ transposed matrix $ \ begin {pmatrix} 2 & 5 \\ 3 & 7 \\ 1 & Find 0 \ end {pmatrix} $

>>> A=np.array([[2,3,1],[5,7,0]])
>>>
>>> A
array([[2, 3, 1],
       [5, 7, 0]])
>>>
>>> AT=A.T
>>>
>>> AT
array([[2, 5],
       [3, 7],
       [1, 0]])
Determined as ** symmetric matrix **

A square matrix that matches its own transposed matrix For the matrix $ A $, $ A = A ^ {T} $.

The following example finds and compares $ \ begin {pmatrix} 1 & 7 & 3 \\ 7 & 4 & 5 \\ 3 & 5 & 6 \ end {pmatrix} $ and its transposed matrix. The result is True, so a symmetric matrix

>>> A=np.array([[1,7,3],[7,4,5],[3,5,6]])
>>>
>>> A
array([[1, 7, 3],
       [7, 4, 5],
       [3, 5, 6]])
>>>
>>> AT=A.T
>>>
>>> np.array_equal(A,AT)
True
** Matrix product ** (matrix product)

The product of the matrices $ A $ and $ B $ is represented by $ AB $ and can be calculated by np.dot only if the number of columns of $ A $ and the number of rows of $ B $ match. The solution of the matrix multiplication is different when it is multiplied from the right and when it is multiplied from the left.

In the example below, $ A = \ begin {pmatrix} 2 & 1 \\ 1 & 3 \ end {pmatrix} $ and $ B = \ begin {pmatrix} 2 & 1 \\ 1 & 2 \ end {pmatrix} Find the product $ AB $ of $.

>>> A=np.array([[2,1],[1,3]])
>>>
>>> B=np.array([[2,1],[1,2]])
>>>
>>> AB=np.dot(A,B)
>>>
>>> AB
array([[5, 4],
       [5, 7]])
>>>
>>> BA=np.dot(B,A)
>>>
>>> BA
array([[5, 5],
       [4, 7]])
** Inner product of matrix ** (Frobenius Product)

In np.dot, the matrices $ A $ and $ B $ shown in 1 row and n columns are regarded as n-dimensional horizontal vectors and the inner product of the vectors is returned. The inner product is the scalar value obtained by multiplying each $ n $ th element and summing the results for all the elements. Converting the row vector on the right to a vertical vector will return the inner product value as a matrix.

The following 1 is a 4-dimensional row vector $ a = \ begin {pmatrix} \ 0 & 0 & 0 & 0 \ end {pmatrix} $ and $ b = \ begin {pmatrix} \ 0 & 0 & 0 & 0 \ Find the inner product $ a \ cdot b $ of end {pmatrix} $. 2 is a matrix product because $ b $ is converted to a column vector

>>> a=np.array([2,2,2,3])
>>>
>>> b=np.array([1,2,3,4])
>>>
>>> np.dot(a,b)   ###1 The inner product of vectors a and b is returned as a scalar
24
>>> np.dot(b,a)   ###1 Since it is the inner product of vectors a and b, the result is the same regardless of the order of multiplication.
24
>>>
>>> b_col=([[2],[2],[2],[3]])   ###Make 2 b a one-dimensional, four-column matrix
>>>
>>> np.dot(a,b_col)   ###The result is returned as a 1-dimensional 4-column matrix of 2 b.
array([24])
>>>

** Inverse matrix **

A matrix whose unit matrix $ E $ is obtained by multiplying the square matrix A from the right or from the left. When the inverse matrix is indicated by $ A ^ {-1} $ for the matrix $ A $ $ AA ^ {-1} = A ^ {-1} A = E $ holds

As an example, create a 2-by-2 matrix $ A = \ begin {pmatrix} 2 & 3 \\ 2 & 4 \ end {pmatrix} $ and find its inverse matrix $ A ^ {-1} $.

>>> A=np.array([[2,3],[2,4]])
>>>
>>> AI=np.linalg.inv(A)
>>>
>>> AI
array([[ 2. , -1.5],
       [-1. ,  1. ]])
** regular matrix **

A matrix that is a square matrix and has an inverse matrix. Not all matrices have an inverse matrix.

In the example below, $ AA ^ {-1} = A ^ {-1} A = E $ can be confirmed, so both $ A $ and $ AI $ are regular matrices.

>>> A=np.array([[1,1],[2,4]])
>>>
>>> AI=np.linalg.inv(A)
>>>
>>> np.dot(A,AI)
array([[1., 0.],
       [0., 1.]])
>>>
>>> np.dot(AI,A)
array([[1., 0.],
       [0., 1.]])

Recommended Posts

Numpy Memorandum_Matrix
Numpy [Basic]
numpy part 1
numpy tips
About numpy
NumPy axis
Use Numpy
numpy part 2
numpy unit test
NumPy array manipulation (3)
list and numpy
NumPy universal functions
numpy memorandum 1 / np.pad
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
numpy index search
NumPy array manipulation (1)
[Numpy] Shuffle ndarray
Python #Numpy basics
numpy non-basic techniques
About Numpy broadcast
[PyTorch] Sample ① ~ NUMPY ~
Install Numpy + atlas
[Python] Numpy memo