> 文档中心 > 【机器学习入门基础】Matrix

【机器学习入门基础】Matrix

Matrix

  • 定义
    • Matrix: Pectangular array of numbers
      • dimension of matrix: the number of rows × \times ×the number of column
      • Matrix Element(entries of matrix)
    • Vector: An n ×\times × 1 matrix
    • 1-indexed vs 0-index
      • Scalar → \to 对象是单个值,不是vector or matrix.(标量)
  • Matrix Addition
  • Scalar Multiplication
  • Combination of Operands
  • Matrix-vector multiplication
    • example
    • Detail
    • example
    • house size:
  • Matrix-matrix multiplication
    • Details:
    • example
    • house size:
  • Matrix multiplication
  • Identity Matrix
    • Example of identity matrix:
  • Inverse and Transpose(逆运算以及转置矩阵)
    • Matrix invers
      • caculate the inverse matrix
        • Octave
        • Matlab
    • Matrix Transpose
      • example:
  • the end

注:这里使用英文,感觉英文解释的更清楚

定义

Matrix: Pectangular array of numbers

dimension of matrix: the number of rows × \times ×the number of column

4 × 2 4 \times 2 4×2matrix( R4×2 R^{4\times2} R4×2)
[ 1902 191 1371 821 949 1437 147 1448 ] \begin{bmatrix} {1902}&{191}\\ {1371}&{821}\\ {949}&{1437}\\ {147}&{1448}\\ \end{bmatrix} 1902137194914719182114371448
2 × 3 2 \times 3 2×3matrix( R2×3 R^{2\times3} R2×3)
[ 1 2 3 4 5 6 ] \begin{bmatrix} {1}&{2}&{3}\\ {4}&{5}&{6}\\ \end{bmatrix} [142536]

Matrix Element(entries of matrix)

A =[ 1902 191 1371 821 949 1437 147 1448 ] A=\left [ \begin{matrix} {1902}&{191}\\ {1371}&{821}\\ {949}&{1437}\\ {147}&{1448} \end{matrix} \right] A=1902137194914719182114371448
Aij A_{ij} Aij:“i, j entry” in the ith i^{th} ith row, jth j^{th} jth column
A 11 A_{11} A11=1902 A 12 A_{12} A12=191 A 32 A_{32} A32=1437 A 41 A_{41} A41=147
A43 A_{43} A43= undefined(error)(全部划掉)

Vector: An n × \times × 1 matrix

4-dimensional vector ( R 4 R^4 R4)
y =[ 460 232 315 178 ] y=\left [ \begin{matrix} {460}\\ {232}\\ {315}\\ {178} \end{matrix} \right] y=460232315178
y i y_i yi= ith i^{th} ith element
y 1 y_1 y1=460 y 2 y_2 y2=232 y 3 y_3 y3=315 y 4 y_4 y4=178

1-indexed vs 0-index

y =[ y 1 y 2 y 3 y 4 ] y=\left [ \begin{matrix} {y_1}\\ {y_2}\\ {y_3}\\ {y_4} \end{matrix} \right] y=y1y2y3y4
y =[ y 0 y 1 y 2 y 3 ] y=\left [ \begin{matrix} {y_0}\\ {y_1}\\ {y_2}\\ {y_3} \end{matrix} \right] y=y0y1y2y3
Matices → \to 一般大写字母 Vector → \to 小写

Scalar → \to 对象是单个值,不是vector or matrix.(标量)

R → \to the set of sccalar real numbers(标量实数集)
R n → R^n\to Rn n-dimensional vectors of real numbers(实数n维向量)

Matrix Addition

[ 1 0 2 5 3 1 ] +[ 4 0.5 2 5 0 2 ] =[ 5 0.5 4 10 3 2 ] \begin{bmatrix} {1}&{0}\\ {2}&{5}\\ {3}&{1}\\ \end{bmatrix}+\begin{bmatrix} {4}&{0.5}\\ {2}&{5}\\ {0}&{2}\\ \end{bmatrix}=\begin{bmatrix} {5}&{0.5}\\ {4}&{10}\\ {3}&{2}\\ \end{bmatrix} 123051+4200.552=5430.5102
3 × 2 m a t r i x + 3 × 2 m a t r i x = 3 × 2 m a t r i x 3\times2 matrix+3\times2 matrix=3\times2 matrix 3×2matrix+3×2matrix=3×2matrix
[ 1 0 2 5 3 1 ] +[ 4 0.5 2 5 ] = e r r o r ( 没 有 意 义 ) \begin{bmatrix} {1}&{0}\\ {2}&{5}\\ {3}&{1}\\ \end{bmatrix}+\begin{bmatrix} {4}&{0.5}\\ {2}&{5}\\ \end{bmatrix}=error( 没有意义) 123051+[420.55]=error()

Scalar Multiplication

3 ×[ 1 0 2 5 3 1 ] =[ 3 0 6 15 9 3 ] =[ 1 0 2 5 3 1 ] × 3 3\times\begin{bmatrix} {1}&{0}\\ {2}&{5}\\ {3}&{1}\\ \end{bmatrix}=\begin{bmatrix} {3}&{0}\\ {6}&{15}\\ {9}&{3}\\ \end{bmatrix}=\begin{bmatrix} {1}&{0}\\ {2}&{5}\\ {3}&{1}\\ \end{bmatrix}\times3 3×123051=3690153=123051×3
[ 4 0 6 3 ] / 4 =14 [ 4 0 6 3 ] =[ 1 0 3 2 3 4 ] \begin{bmatrix} {4}&{0}\\ {6}&{3}\\ \end{bmatrix}/4=\frac{1}{4}\begin{bmatrix} {4}&{0}\\ {6}&{3}\\ \end{bmatrix}=\begin{bmatrix} {1}&{0}\\ {\frac{3}{2}}&{\frac{3}{4}}\\ \end{bmatrix} [4603]/4=41[4603]=[123043]

Combination of Operands

3 ×[ 1 4 2 ] +[ 0 0 5 ] −[ 3 0 2 ] / 3 =[ 3 12 6 ] +[ 0 0 5 ] −[ 1 3 2 3 ] =[ 2 12 10 1 3 ] 3\times\begin{bmatrix} {1}\\ {4}\\ {2} \end{bmatrix}+\begin{bmatrix} {0}\\ {0}\\ {5} \end{bmatrix}-\begin{bmatrix} {3}\\ {0}\\ {2} \end{bmatrix}/3=\begin{bmatrix} {3}\\ {12}\\ {6} \end{bmatrix}+\begin{bmatrix} {0}\\ {0}\\ {5} \end{bmatrix}-\begin{bmatrix} {1}\\ {3}\\ {\frac{2}{3}} \end{bmatrix}=\begin{bmatrix} {2}\\ {12}\\ {10\frac{1}{3}} \end{bmatrix} 3×142+005302/3=3126+0051332=2121031

Matrix-vector multiplication

example

[ 1 3 4 0 2 1 ] [ 1 5 ] =[ 16 4 7 ] \begin{bmatrix} {1}&{3}\\ {4}&{0}\\ {2}&{1} \end{bmatrix}\begin{bmatrix} {1}\\ {5}\\ \end{bmatrix}=\begin{bmatrix} {16}\\ {4}\\ {7} \end{bmatrix} 142301[15]=1647
1 × 1 + 3 × 5 = 16 1\times1+3\times5=16 1×1+3×5=16
4 × 1 + 0 × 5 = 4 4\times1+0\times5=4 4×1+0×5=4
2 × 1 + 1 × 5 = 7 2\times1+1\times5=7 2×1+1×5=7
( 3 × 2 m a t r i x ) ( 2 × 1 m a t r i x ) = ( 3 × 1 m a t r i x ) (3\times2 matrix) (2\times1matrix)=(3\times1matrix) (3×2matrix)(2×1matrix)=(3×1matrix)

Detail

[ a 11 … a 1 n ⋮⋱ ⋮a m 1 … a m n ] ×[ x 1 ⋮x n ] =[ y 1 ⋮y m ] \begin{bmatrix} {a_{11}}&\ldots&{a_{1n}} \\ {\vdots}&\ddots&{\vdots} \\ {a_{m1}}&\ldots&{a_{mn}} \\ \end{bmatrix}\times \begin{bmatrix} {x_{1}}\\ \vdots\\ {x_n} \end{bmatrix}=\begin{bmatrix} {y_1}\\ {\vdots}\\ {y_m} \end{bmatrix} a11am1a1namn×x1xn=y1ym
To get y i y_i yi, multiply A’s ith i^{th} ithrow with elements of vector x,and add them up.

example

[ 1 2 1 5 0 3 4 0 − 1 − 2 0 0 ] [ 1 3 2 1 ] =[ 14 13 − 7 ] \begin{bmatrix} {1}&2&1&5\\ {0}&3&4&0\\ {-1}&{-2}&0&0 \end{bmatrix}\begin{bmatrix} 1\\3\\2\\1 \end{bmatrix}=\begin{bmatrix} 14\\13\\{-7} \end{bmatrix} 1012321405001321=14137
1 × 1 + 2 × 3 + 1 × 2 + 5 × 1 = 14 1\times1+2\times3+1\times2+5\times1=14 1×1+2×3+1×2+5×1=14
0 × 1 + 3 × 3 + 0 × 2 + 4 × 1 = 13 0\times1+3\times3+0\times2+4\times1=13 0×1+3×3+0×2+4×1=13
− 1 × 1 + − 2 × 3 + 0 × 2 + 0 × 1 = − 7 {-1}\times1+{-2}\times3+0\times2+0\times1={-7} 1×1+2×3+0×2+0×1=7

house size:

{ 2104 1416 1534 852 \begin{cases} 2104\\ 1416\\ 1534\\ 852 \end{cases} 210414161534852 h θ ( x ) = − a 0 + 0.25 x h_\theta(x)=-a0+0.25x hθ(x)=a0+0.25x
[ 1 2104 1 1416 1 1534 1 852 ] ×[ − 40 0.25 ] =[ − 40 × 1 + 0.25 × 2104 − 40 × 1 + 0.25 × 1416 − 40 × 1 + 0.25 × 1534 − 40 × 1 + 0.25 × 842 ] → h θ ( 2104 ) → h θ ( 1416 ) → h θ ( 1534 ) → h θ ( 852 ) \begin{bmatrix} {1}&{2104}\\ {1}&{1416}\\ {1}&{1534}\\ {1}&{852}\\ \end{bmatrix}\times\begin{bmatrix} {-40}\\ {0.25}\\ \end{bmatrix}=\begin{bmatrix} {{-40}\times1+0.25\times2104}\\ {{-40}\times1+0.25\times1416}\\ {{-40}\times1+0.25\times1534}\\ {{-40}\times1+0.25\times842}\\ \end{bmatrix} \begin{matrix} \to h_\theta(2104)\\ \to h_\theta(1416)\\ \to h_\theta(1534)\\ \to h_\theta(852) \end{matrix} 1111210414161534852×[400.25]=40×1+0.25×210440×1+0.25×141640×1+0.25×153440×1+0.25×842hθ(2104)hθ(1416)hθ(1534)hθ(852)
prediction=datamatrix∗ * parameters

Matrix-matrix multiplication

[ 1 3 2 4 0 1 ] ×[ 1 3 0 1 5 2 ] =[ 11 10 9 14 ] \begin{bmatrix} {1}&{3}&{2}\\ {4}&{0}&{1}\\ \end{bmatrix}\times\begin{bmatrix} {1}&{3}\\ 0&1 \\5&2 \end{bmatrix}=\begin{bmatrix} {11}&{10}\\ 9&14 \end{bmatrix} [143021]×105312=[1191014]
[ 1 3 2 4 0 1 ] ×[ 1 0 5 ] =[ 11 9 ] \begin{bmatrix} {1}&{3}&{2}\\ {4}&{0}&{1}\\ \end{bmatrix}\times\begin{bmatrix} {1}\\ 0 \\5 \end{bmatrix}=\begin{bmatrix} {11}\\ 9 \end{bmatrix} [143021]×105=[119]
[ 1 3 2 4 0 1 ] ×[ 3 1 2 ] =[ 10 14 ] \begin{bmatrix} {1}&{3}&{2}\\ {4}&{0}&{1}\\ \end{bmatrix}\times\begin{bmatrix} {3}\\ 1 \\2 \end{bmatrix}=\begin{bmatrix} {10}\\ 14 \end{bmatrix} [143021]×312=[1014]

Details:

m × n matrix + n × o m a t r i x = n × o m a t r i x A × B = C m\times n {\kern 2pt} {matrix}+n\times o{\kern 2pt}matrix=n\times o{\kern 1pt}matrix\\ A\times B=C m×nmatrix+n×omatrix=n×omatrixA×B=C
The ith i^{th} ith column of the matrix C is obtained by mutiplying A withthe ith i^{th} ith column of B(for i =1,2 … \ldots , o)

example

[ 1 3 2 5 ] ×[ 0 1 3 2 ] =[ 9 7 15 12 ] \begin{bmatrix} {1}&3\\ 2&5 \end{bmatrix}\times \begin{bmatrix} {0}&1\\ 3&2 \end{bmatrix}=\begin{bmatrix} {9}&7\\ 15&12 \end{bmatrix} [1235]×[0312]=[915712]

house size:

{ 2104 1416 1534 852 \begin{cases} 2104\\ 1416\\ 1534\\ 852 \end{cases} 210414161534852
Have 3 competing hypothesis
{1 、 h θ ( x ) = − 40 + 0.25 x 1 、 h θ ( x ) = 200 + 0.1 x 1 、 h θ ( x ) = − 150 + 0.4 x \begin{cases} 1、h_\theta (x)=-40+0.25x\\ 1、h_\theta (x)=200+0.1x\\ 1、h_\theta (x)=-150+0.4x\\ \end{cases} 1hθ(x)=40+0.25x1hθ(x)=200+0.1x1hθ(x)=150+0.4x
[ 1 2104 1 1416 1 1543 1 852 ] ×[ − 40 200 − 150 0.25 0.1 0.4 ] =[ 482 410 692 314 342 416 344 352 464 173 285 191 ] \begin{bmatrix} {1}&2104\\ 1&1416\\ 1&1543\\ 1&852 \end{bmatrix}\times \begin{bmatrix} {-40}&200&{-150}\\ 0.25&0.1&0.4 \end{bmatrix}=\begin{bmatrix} 482&410&692\\ 314&342&416 \\344&352&464\\ 173&285&191 \end{bmatrix} 1111210414161543852×[400.252000.11500.4]=482314344173410342352285692416464191

Matrix multiplication

3 × 5 = 5 × 3 3\times 5=5\times3 3×5=5×3 “Commutative”
Let Aand B be matrices.Then in general.
A × B ≠ B × A A\times B \ne B\times A A×B=B×A(not commutative)
E . g .[ 1 1 0 0 ] ×[ 0 0 2 0 ] =[ 2 0 0 0 ]≠ [ 0 0 2 0 ] ×[ 1 1 0 0 ] =[ 0 0 2 2 ] E.g. \begin{bmatrix} {1}&1\\ 0&0\\ \end{bmatrix} \times\begin{bmatrix} {0}&0\\ 2&0\\ \end{bmatrix}=\begin{bmatrix} {2}&0\\ 0&0\\ \end{bmatrix}\\\ne\\\begin{bmatrix} {0}&0\\ 2&0\\ \end{bmatrix}\times \begin{bmatrix} {1}&1\\ 0&0\\ \end{bmatrix} =\begin{bmatrix} {0}&0\\ 2&2\\ \end{bmatrix} E.g.[1010]×[0200]=[2000]=[0200]×[1010]=[0202]
A × B m × n n × m A × B ∼ m × m B × A ∼ n × n \begin{array}{|lll} A\times B\\ m\times n\quad n\times m\\ A\times B\sim m\times m \\ B\times A\sim n\times n \end{array} A×Bm×nn×mA×Bm×mB×An×n
Associative
3 × 5 × 2 = ( 3 × 5 ) × 2 = 3 × ( 5 × 2 ) 3\times 5\times2=({3\times 5})\times2=3\times (5\times2) 3×5×2=(3×5)×2=3×(5×2)
A × B × C → ( A × B ) × C → A × ( B × C ) ( s a m e  a n s w e r ) A\times B\times C\quad \to (A\times B)\times C\\\quad\quad\quad\quad\quad\quad \to A\times (B\times C)(same\, answer) A×B×C(A×B)×CA×(B×C)(sameanswer)
Let D=B × \times ×C. computeA A × D A\times D A×D
Let E=A × \times ×B. computeA E × C E\times C E×C

Identity Matrix

Denoted I I I(or In×n I_{n\times n} In×n) \quad\quad\quad 1 ∼ i d e n t i t y 1\sim identity 1identity \quad\quad\quad
1 × z = z × 1 = z 1\times z=z\times 1=z 1×z=z×1=z \quad\quad\quad for any z

Example of identity matrix:

[ 1 ] [ 1 0 0 1 ] [ 1 0 0 0 1 0 0 0 1 ] [ 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 ] [ 1 1 ⋱ 1 ] ( I n f o r m a l l y ) \begin{bmatrix} 1 \end{bmatrix}\begin{bmatrix} {1}&0\\ 0&1\\ \end{bmatrix}\begin{bmatrix} 1&0&0\\ 0&1&0\\ 0&0&1 \end{bmatrix}\begin{bmatrix} 1&0&0&0\\ 0&1&0&0\\ 0&0&1&0\\ 0&0&0&1 \end{bmatrix}\begin{bmatrix} 1&{}&{}&{} \\ {}&1&{}&{} \\ {}&{}& \ddots &{} \\ {}&{}&{}&1 \end{bmatrix}(Informally) [1][1001]1000100011000010000100001111(Informally)
For any matrix A
A × I = I × A = A m × n n × n m × m m × n m × n A\times I =I\times A=A\\m\times n\quad n\times n\quad m\times m\quad m\times n\quad m\times n A×I=I×A=Am×nn×nm×mm×nm×n
I n × nN o t e : A × B ≠ B × A i n  g e n e r a l A × I = I × A ✔ I_{n\times n}\begin{array}{|lll} Note:\\ A\times B\ne B\times A \quad in \,general\\ A\times I= I\times A ✔ \end{array} In×nNote:A×B=B×AingeneralA×I=I×A

Inverse and Transpose(逆运算以及转置矩阵)

1=“Identity” \quad 3 ( 3−1 ) = 1 3(3^{-1})=1 3(31)=1 \quad 12 ( 1 2−1 ) = 1 12(12^{-1})=1 12(121)=1
Mot all numbers have an inverse \quad → 0 ( 0−1 ) \to0(0^{-1}) 0(01)but 0−1 → 0^{-1}\to 01undefined

Matrix invers

If A is an m × m m\times m m×m matrix(square matrix{has the same row &column}),and if it has an inverse.
→ A A−1 = A−1 A = I \to AA^{-1}=A^{-1}A=I AA1=A1A=I \quad A 逆 矩 阵 A−1 A逆矩阵A^{-1} AA1 \quad
A = [ 0 0 0 0 ] 无 逆 矩 阵 A=\begin{bmatrix} {0}&0\\ 0&0\\ \end{bmatrix}无逆矩阵 A=[0000]
E . g .[ 3 4 2 16 ] [ 0.4 − 0.1 − 0.05 0.75 ] =[ 1 0 0 1 ] =I 2 × 2E.g.\begin{bmatrix} 3&4\\ 2&16\\ \end{bmatrix}\begin{bmatrix} 0.4&{-0.1}\\ {-0.05}&0.75\\ \end{bmatrix}=\begin{bmatrix} 1&0\\ 0&1\\ \end{bmatrix}=I_{2\times 2} E.g.[32416][0.40.050.10.75]=[1001]=I2×2
the way to caculate

caculate the inverse matrix

Octave

A=[3 4;2 16]pinv(A)

Matlab

Matrices that don’t have an inverse are “singular” or “degenerate”

Matrix Transpose

example:

A = [ 1 2 0 3 5 9 ] A=\begin{bmatrix} 1&2&0\\ 3&5&9\\ \end{bmatrix} A=[132509] A T = [ 1 3 2 5 0 9 ] \quad A^T=\begin{bmatrix} 1&3\\ 2&5\\ 0&9 \end{bmatrix} AT=120359 r o w → c o l u m n \quad row \to column rowcolumn
Let A be a m × n m\times n m×n matrix,and let B = A T B=A^T B=AT
Then B is a n × m n\times m n×m matrix, and
Bij = Aji B_{ij}=A_{ji} Bij=Aji
B 21 = A 12 = 2 B_{21}=A_{12}=2 B21=A12=2
B 32 = A 23 = 9 B_{32}=A_{23}=9 B32=A23=9

the end

有相关配套程序请点击这里

China香烟网