Linear Equation
ππ» μ ν λ°©μ μμ΄λ, λ³μ $x_1, x_2, .. x_n$μ΄ μκ³ , $a_1x_1 + a_2x_2 + ... + a_nx_n = b$λ‘ λνλΌ μ μλ λ°©μ μμ λ»νλ€.
(bμ κ³μ a_1, a_2, ~ a_nμ μ€μ λλ 볡μμ)
ππ» μ equationμ μ΄λ κ²λ νν κ°λ₯νλ€.
$a^Tx = b$
$$a = \begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_n \end{bmatrix}, x = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix}$$
Linear System
ππ» μ ν μμ€ν μ μμ μ€λͺ ν linear equationμ΄ 1κ° μ΄μ ꡬμ±λ μμ€ν μ λ»νλ€
ππ» μμ)
ID | A | B | C | life-span |
1 | 60 | 5.5 | 1 | 66 |
2 | 65 | 5.0 | 0 | 74 |
3 | 55 | 6.0 | 1 | 78 |
→ μ΄λ₯Ό μλμ κ°μ linear systemμΌλ‘ ννν μ μλ€.
$60x_1 + 5.5x_2 + 1*x_3 = 66$
$65x_1 + 5.0x_2 + 0*x_3 = 74$
$55x_1 + 6.0x_2 + 1*x_3 = 78$
→ μ¦, $x_1, x_2, x_3$κ° μ£Όμ΄μ‘μ λ life-spanμ μ μ μκ² λλ€.
→ linear systemμ matrix νλ ¬μ μ΄μ©ν΄μ ννν μ μλ€.
β» νλ ¬ κ΄λ ¨ ν¬μ€ν μλ μ°Έμ‘° β»
Matrix
π» μ¬μ€ μ΄κ±° λ€λ£°λ €κ³ Scalar & Vector λ°°μ΄ κ±°λΌ ν μ λλ‘ νλ ¬μ.. μ νλμνμ κ½! π» Scalar & Vector (fundamentals) βΆ Linear Algebra νλ©΄? λΉμ°ν μμμΌ ν κΈ°λ³Έμ 'Scalar(μ€μΉΌλΌ)' & 'Vector(벑ν°)' & 'Matri
sh-avid-learner.tistory.com
→ μ΄λ₯Ό inverse matrixλ₯Ό μ΄μ©ν΄ νμ΄λ³΄λ©΄
$Ax = b$
$A^{-1}Ax = A^{-1}b$
$I_nx = A^{-1}b$
$x = A^{-1}b$
→ μ¦, μ°λ¦¬λ life-span = $-0.4x_1 + 20x_2 - 20x_3$λ‘ λνλΌ μ μλ€.
ππ» λ§μ½, νλ ¬ Aκ° non-invertibleμ΄λΌλ©΄(μνλ ¬μ ꡬν μ μλ€λ©΄)?
→ ν΄κ° μκ±°λ, 무μν λ§μ κ²½μ°
→ μ μμλ‘ λ linear systemμμ mμ linear equationμ κ°μ, nμ variable(λ 립 λ³μ)μ κ°μλΌκ³ νλ©΄,
β m<n: variableμ΄ λ λ§μ κ²½μ° μ£Όλ‘ λ¬΄μν λ§μ ν΄κ° μ‘΄μ¬ (under-determined system)
(μ¬λ¬ ν΄ μ€μ μΌλΆ ν΄, νΉν μ’ μλ³μμ λ ν° variationμ 보μ΄λ κ²½μ° regularizationμ μ μ©νλ λ± μ¬λ¬ μ‘°μΉλ₯Ό μ·¨ν μ μμ)
β‘ m>n: equationμ΄ λ λ§μ κ²½μ° μ£Όλ‘ ν΄κ° μμ (over-determined system)
ππ» Ax=bλ₯Ό κΈ°ννμ μΈ μλ―Έλ‘ ν΄μνλ€λ©΄, xκ° λνλ΄λ 2μ°¨μμ μ μ΄ Aλ₯Ό ν΅ν΄μ bλΌλ μ μΌλ‘ μ΄λνλ€λ κ²μ λ»νλ€. μ¦, A λ³νμ ν΅ν΄μ bλ‘ μ΄λν μ (x)μ μ°Ύλλ€.
(μ¬κΈ°μ vector xμ bλ λͺ¨λ 2μ°¨μμ΄κ³ , μ μΌλ‘ λνλΌ μ μμΌλ©°, Aλ‘ μΈν linear transformation / κ·Έ μμ transformationμ κ·Έλ¦ΌμΌλ‘ λνλ΄λ©΄ μλμ κ°λ€.)
Rank
ππ» νλ ¬ Aμ rankλ μλ‘ μ νλ 립μΈ(linearly independent) Aμ νμ κ°―μ(λλ μ΄μ κ°―μ)λ₯Ό λ»νλ€.
ππ» μ νλ 립 - 'μ΄λ ν ν(νΉμ μ΄)μ΄ νλ ¬μ λ€λ₯Έ ν(μ΄)λ€μ μ ν μ‘°ν©μΌλ‘ ννμ΄ μλλ κ²½μ°'
ex) μλμ νλ ¬ Aλ rankκ° 2 / Bλ rankκ° 1
ππ» λ°λΌμ, full-rank matrixλ nxn A νλ ¬μ λν΄μ λͺ¨λ νμ΄ μλ‘ μ νλ 립μ΄κ±°λ / λͺ¨λ μ΄μ΄ μλ‘ μ νλ λ¦½μΈ matrixλ₯Ό λ»νκ³ , μ΄λ₯Ό non-singular νλ ¬μ΄λΌκ³ λ νλ©°, μνλ ¬μ΄ μ‘΄μ¬ν¨μ λ»νλ€.
ππ» κ·Έ λ°λλ‘, full-rank matrixκ° μλλΌλ©΄ singular νλ ¬μ΄λΌ λΆλ₯΄κ³ , μνλ ¬μ΄ μμμ λ»νλ€.
ππ» Rankλ numpyμ linalg.matrix_rank() μ¬μ©
A = np.array([[1, 2],
[2, 5]])
np.linalg.matrix_rank(A) #2
B = np.array([[1, 2],
[2, 4]])
np.linalg.matrix_rank(B) #1
np.linalg.inv(B) #LinAlgError: Singular matrix
* singular matrix
β κ° νμ΄ μλ‘ μ νλ λ¦½μ΄ μλ (λΆλ₯ - λ§μ‘±νλ ν΄κ° νλλ μλ€.)
β‘ νλμ νμ΄ λ€λ₯Έ νμ scalar λ°°λ‘ νν (λΆμ - λ§μ‘±νλ ν΄κ° 무μν λ§λ€.)
ππ» νλ ¬μ(determinant)κ° 0μΈ κ²½μ°(ad-bc=0) μνλ ¬μ΄ μλ€.
ππ» matrixμ νλ ¬μμ |A|λ‘ νν
ex) Ax=yμμ
β $\cfrac{a}{c} ≠ \cfrac{y_1}{y_2}$μ΄λ©΄ λΆλ₯
β‘ $\cfrac{a}{c} = \cfrac{y_1}{y_2}$μ΄λ©΄ λΆμ
ππ» λΆλ₯μ κΈ°ννμ μλ―Έ
→ Ax=yμμ Aμ μν΄ yλ‘ μ΄λλ μ μλ vector xκ° μ‘΄μ¬νμ§ μλλ€
ππ» λΆμ μ κΈ°ννμ μλ―Έ
→ Ax=yμμ Aμ μν΄ yλ‘ μ΄λλ μ μλ vector xκ° λ¬΄μν μ‘΄μ¬νλ€
(μ μΌμͺ½ κ·Έλ¦Όμ λΆλ₯ / μ€λ₯Έμͺ½ κ·Έλ¦Όμ λΆμ )
det(A)
ππ» |det(A)| = |ad-bc|λ‘, νλ ¬μμ μ λκ°μ νλ ¬ Aλ₯Ό ν΅ν΄ λ¨μ곡κ°μ μΌλ§λ λλ Έλμ§ / μ€μλμ§ (λμ΄λ°°)λ₯Ό μλ―Ένλ€
ππ» ex) μλμ κ°μ linear transformation κ°μ νλ©΄,
→ κ° μΆ λ³λ‘ 1μ© μ΄λ£¨μ΄μ§ λ¨μ곡κ°μ λμ΄κ° 1μμ λ³ν ν 4λ‘ μ»€μ§. μ¦, κΈ°μ‘΄ 곡κ°μμ 4λ°° 컀μ‘μΌλ―λ‘ Aμ νλ ¬μμ λμ΄λ°°
ππ» λ°λΌμ νλ ¬μμ΄ 0μΈ κ²½μ°λ λμ΄κ° μλ ν μ μΌλ‘ λ³νλμμΌλ―λ‘, μ΄λ μνλ ¬μ΄ μλ€λ κ²μ λ»ν¨
ππ» numpyμ linalg.det() μ¬μ©
A
'''
array([[1, 2],
[3, 4]])
'''
np.linalg.det(A) #-2.0000000000000004
B
'''
array([[1, 2],
[2, 4]])
'''
np.linalg.det(B) #0.0
* python
1) column vector & row vector
ππ» column vectorλ numpyμ arrayλ₯Ό μ¬μ©ν΄ []μμ μνλ elementλ₯Ό μ¬λ¬ κ° μ°λ©΄ λκ³ , row vectorλ []μμ [] bracketμ ν λ² λ μ¨μ νμ μ μΈν λ€μ, ν μμ μνλ μμλ₯Ό μ½μ νλ©΄ λλ€.
→ column vector
import numpy as np
#column vector
c=np.array([1,2,3])
print(c.shape) #1-dimension array
#(3,)
#obtaining a particular entry
print(c[0])
#1
→ row vector
#row vector
r=np.array([[1,2,3]])
print(r.shape) #(1, 3)
#obtaining a particular entry
print(r[0,1]) #2
2) matrix
→ λ€μν ννμ matrix λ§λ€κΈ°
#creating a matrix with all zeros
a=np.zeros((2,2))
print(a)
'''
[[0. 0.]
[0. 0.]]
'''
#creating a matrix with all ones
b=np.ones((2,2))
print(b)
'''
[[1. 1.]
[1. 1.]]
'''
#creating a matrix filled with the same constant
c=np.full((2,2),7)
print(c)
'''
[[7 7]
[7 7]]
'''
#creating a matrix with random values
d=np.random.random((2,2))
print(d)
'''
[[0.61959397 0.13857043]
[0.0815273 0.50410076]]
'''
ππ» μ μΉνλ ¬μ λ§λ€λ €λ©΄ A.T, μ¦ λ€μ .Tλ₯Ό λΆμ΄λ©΄ λκ³ , νλ ¬μ κ³±μ np.dot(A,B)λ₯Ό μ¬μ©νλ©΄ λλ€
(μΈ κ° μ΄μμ νλ ¬λ np.dot() μμ λ£μΌλ©΄ λλ€)
β» μ£Όμμ : νλ ¬ κ³± μ°μ°μμ np.dot()μ΄ μλλΌ, * μ°μ°μλ₯Ό μ¨μλ μλλ€)
#creating a matrix
A=np.array([[1,2],[3,4],[5,6]])
'''
[[1 2]
[3 4]
[5 6]]
'''
#creating another matrix
B=np.array([[11,12,13,14],[15,16,17,18]])
'''
array([[11, 12, 13, 14],
[15, 16, 17, 18]])
'''
#transpose a matrix
A.T
'''
array([[1, 3, 5],
[2, 4, 6]])
'''
#matrix-matrix multiplication
np.dot(A,B)
'''
array([[ 41, 44, 47, 50],
[ 93, 100, 107, 114],
[145, 156, 167, 178]])
'''
→ μ΄ λ, λΉμ°ν νλ ¬ κ³± μ°μ°(AB)μμ, Aμ μ΄κ³Ό Bμ ν κ°μκ° κ°μμΌ νλ€. (κ·Έλ μ§ μμΌλ©΄ μ€λ₯)
#matrix-matrix multiplication
#size should match!
np.dot(B,A)
#ValueError: shapes (2,4) and (3,2) not aligned: 4 (dim 1) != 3 (dim 0)
3) linear system μμ νκΈ°
ππ» inv() ν¨μλ₯Ό μ¬μ©νλ©΄ μ£Όμ΄μ§ νλ ¬μ μνλ ¬μ ꡬν μ μκ³ , μμ μΈκΈν νλ ¬ κ³± ν¨μ dot()μ μ΄μ©ν΄ μ΅μ’ ν΄λ₯Ό ꡬν μ μλ€.
#coefficient matrix A and a vector b
A=np.array([[60, 5.5, 1], [65, 5.0, 0], [55, 6.0, 1]])
b=np.array([66,70,78])
#identity matrix
eye3=np.eye(3)
'''
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
'''
#computing an inverse
from numpy.linalg import inv
A_inv = inv(A)
#solution of a linear system
x=A_inv.dot(b)
x
#answer: array([ -0.43478261, 19.65217391, -16. ])
ππ» λΉ λ₯Έ λ°©λ²μΌλ‘, matrix Aμ μ’ μλ³μκ°μ΄ λμ΄λ μ΄ λ²‘ν° bκ° μμ λ, ν΄μ§ν©μ ꡬνλ solveλΌλ μ체 ν¨μλ₯Ό μ§μν΄μ€λ€.
#a better way to solve the same linear system
from numpy.linalg import solve
x=solve(A,b)
x
#array([ -0.43478261, 19.65217391, -16. ])
* μΆμ²1) μΈκ³΅μ§λ₯μ μν μ νλμ (μ£Όμ¬κ±Έ κ΅μ)
* μΆμ²2) λνμ μ¬μ κ΅μ‘ <λ°μ΄ν°λΆμμ μν κΈ°μ΄μν>
'Math & Linear Algebra > Concepts' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
eigendecomposition (0) | 2023.02.19 |
---|---|
vector similarity (0) | 2023.02.09 |
Matrix (fundamentals) (0) | 2022.07.31 |
Odds Ratio & log(Odds Ratio) (0) | 2022.07.11 |
eigenvalue & eigenvector (0) | 2022.05.14 |
λκΈ