# minte9 LearnRemember

S R Q

### Min, Max, Average

p06 We can apply operation along the axes.

""" Operations / Min, Max, Mean
We can apply operations along the axes (rows or columns).
"""

import numpy as np

M = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])

print("Max item =", np.max(M))
print("Max item =", np.min(M))
print("Average =",  np.mean(M))
print("Max in each row =", np.max(M,  axis=1))
print("Min in each row =", np.min(M,  axis=1))
print("Average in each row =", np.mean(M, axis=0))

"""
Max item = 9
Max item = 1
Average = 5.0
Max in each row = [3 6 9]
Min in each row = [1 4 7]
Average in each row = [4. 5. 6.]
"""

Reshape

### Reshape

p09 The new matrix should have the same size as original matrix.

""" Operations / Matrix Reshape

Reshape maintain the data but as different numbers of rows and columns.
The new matrix should have the same size as original matrix

The argument -1 means "as many as needed"
Flatten transform a matrix into a one-dimensional array.
"""

import numpy as np

M = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
])

A = M.reshape(2, 6)
B = M.reshape(1, -1)
C = M.flatten()

assert M.size == A.size
assert M.size == B.size
assert M.size == C.size

print("Matrix =\n", M)
print("Reshaped (2,6) =\n", A)
print("Reshaped (1,-1) =\n", B)
print("Flatten =\n", C)

"""
Matrix =
[[ 1  2  3]
[ 4  5  6]
[ 7  8  9]
[10 11 12]]
Reshaped (2,6) =
[[ 1  2  3  4  5  6]
[ 7  8  9 10 11 12]]
Reshaped (1,-1) =
[[ 1  2  3  4  5  6  7  8  9 10 11 12]]
Flatten =
[ 1  2  3  4  5  6  7  8  9 10 11 12]
"""

Transpose

### Transpose

p11 The column and row indeces of each element are swapped.

""" Operations / Matrix Transpose

Transposing is a common operation in linear algebra.
Indices of column and rows of each element are swapped.
A vector cannot be transposed.
"""

import numpy as np

M = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])

M_transposed = M.T

assert M[0, 1] == M_transposed[1, 0]
assert M[1, 0] == M_transposed[0, 1]
assert M[1, 1] == M_transposed[1, 1]

print("Matrix =\n", M)
print("Transposed =\n", M_transposed)

"""
Matrix =
[[1 2 3]
[4 5 6]
[7 8 9]]
Transposed =
[[1 4 7]
[2 5 8]
[3 6 9]]
"""

Inverse

### Inverse

p18 Calculate the inverse of a square matrix.

""" Operations / Inverse Matrix

Calculate the inverse of a square matrix.
Matrices are multiplicated using @ (not *)
The new matrix M_inverse is calculated so that
M @ M_inverse = I
"""

import numpy as np

M = np.array([
[4, 3],
[3, 2],
])

M_inverse = np.linalg.inv(M)

I = np.array([
[1, 0],
[0, 1],
])

assert (M @ M_inverse == I).all()
assert (M_inverse @ M == I).all()

print("Matrix =\n", M)
print("Inverse =\n", M_inverse)
print("M @ M_inverse = I: \n", M @ M_inverse)

"""
Matrix =
[[4 3]
[3 2]]
Inverse =
[[-2.  3.]
[ 3. -4.]]
M @ M_inverse = I:
[[1. 0.]
[0. 1.]]
"""


Questions