机器学习(三)矩阵的运算及性质


Log

2022.01.01感觉寒假的事要多起来了,赶紧抽时间学习
2022.01.02像是在复习大一时候学的线性代数与几何,一些学过的技巧方法记法都可以写进去补充一下


一、矩阵和向量(matrix&vectors)

基本概念

  • 矩阵(matrix): 由数字组成的矩形阵列(Rectangular array of numbers),写在方括号内(二维数组的一种表示方式)。
  • 维数(Dimension of matrix): 行数(rows)乘列数(columns)。
    A = [ 1402 1328 793 1277 975 894 1434 1491 ] 4 × 2 m a t r i x R 4 × 2 A=\left[ \begin{matrix} 1402 & 1328 \\ 793 & 1277 \\ 975 & 894 \\ 1434 & 1491 \\ \end{matrix} \right]\\ \quad \\ 4×2\quad matrix\\ R^{4×2} A=140279397514341328127789414914×2matrixR4×2
    Matrix   Elements ( e n t r i e s   o f   m a t r i x ) A i j = “ i ,   j   e n t r y "   i n   t h e   i t h r o w ,   j t h c o l u m n \textbf{Matrix Elements}(entries\ of\ matrix)\quad\quad\\ \quad\\ A_{ij}=“i,\ j\ entry"\ in \ the\ i^{th}row,\ j^{th}column Matrix Elements(entries of matrix)Aij=i, j entry" in the ithrow, jthcolumn
  • 向量(vector): 只有一列的矩阵,例如下面的四维向量。
    4 − d i m e n s i o n a l   v e c t o r R 4 y = [ 123 543 362 485 ] y i = i t h   e l e m r n t 4-dimensional\ vector\\ R^4\\ \quad\\\begin{aligned} y&=\left[\begin{matrix} 123\\ 543\\ 362\\ 485 \end{matrix}\right]\\ \quad\\ y_i&=i^{th} \ elemrnt \end{aligned} 4dimensional vectorR4yyi=123543362485=ith elemrnt
    1 − i n d e x e d v s 0 − i n d e x e d y = [ y 1 y 2 y 3 y 4 ] y = [ y 0 y 1 y 2 y 3 ] 1-indexed \quad vs\quad 0-indexed\\ y=\left[\begin{matrix} y_1\\ y_2\\ y_3\\ y_4 \end{matrix}\right] \qquad y=\left[\begin{matrix} y_0\\ y_1\\ y_2\\ y_3 \end{matrix}\right]\\ 1indexedvs0indexedy=y1y2y3y4y=y0y1y2y3
  • 一般来说,用大写字母表示矩阵,用小写字母表示数字或向量。

二、矩阵运算

1.矩阵加法和减法(Matrix Addition and Subtraction)

  • 矩阵加法两个矩阵必须要行列相等

[ a 11 a 12 a 21 a 22 a 31 a 32 a 41 a 42 ] + [ b 11 b 12 b 21 b 22 b 31 b 32 b 41 b 42 ] = [ a 11 + b 11 a 12 + b 12 a 21 + b 21 a 22 + b 22 a 31 + b 31 a 32 + b 32 a 41 + b 41 a 42 + b 42 ] \left[\begin{matrix} a_{11} & a_{12}\\ a_{21} & a_{22}\\ a_{31} & a_{32}\\ a_{41} & a_{42} \end{matrix}\right] + \left[\begin{matrix} b_{11} & b_{12}\\ b_{21} & b_{22}\\ b_{31} & b_{32}\\ b_{41} & b_{42} \end{matrix}\right] =\left[\begin{matrix} a_{11}+b_{11} & a_{12}+b_{12}\\ a_{21}+b_{21} & a_{22}+b_{22}\\ a_{31}+b_{31} & a_{32}+b_{32}\\ a_{41}+b_{41} & a_{42}+b_{42} \end{matrix}\right] a11a21a31a41a12a22a32a42+b11b21b31b41b12b22b32b42=a11+b11a21+b21a31+b31a41+b41a12+b12a22+b22a32+b32a42+b42

2.标量乘法(Scalar Multiplication)

  • 标量在这里指的是实数
    k × [ a 11 a 12 a 21 a 22 a 31 a 32 a 41 a 42 ] = [ k a 11 k a 12 k a 21 k a 22 k a 31 k a 32 k a 41 k a 42 ] [ a 11 a 12 a 21 a 22 a 31 a 32 a 41 a 42 ] / k = 1 k × [ a 11 a 12 a 21 a 22 a 31 a 32 a 41 a 42 ] k×\left[\begin{matrix} a_{11} & a_{12}\\ a_{21} & a_{22}\\ a_{31} & a_{32}\\ a_{41} & a_{42} \end{matrix}\right] =\left[\begin{matrix} ka_{11} & ka_{12}\\ ka_{21} & ka_{22}\\ ka_{31} & ka_{32}\\ ka_{41} & ka_{42} \end{matrix}\right] \\ \quad \\ \left[\begin{matrix} a_{11} & a_{12}\\ a_{21} & a_{22}\\ a_{31} & a_{32}\\ a_{41} & a_{42} \end{matrix}\right]/k =\frac{1}{k}×\left[\begin{matrix} a_{11} & a_{12}\\ a_{21} & a_{22}\\ a_{31} & a_{32}\\ a_{41} & a_{42} \end{matrix}\right] k×a11a21a31a41a12a22a32a42=ka11ka21ka31ka41ka12ka22ka32ka42a11a21a31a41a12a22a32a42/k=k1×a11a21a31a41a12a22a32a42

3.矩阵乘法(Matrix Multiplication)

  • 要点:
    • 方法:左行乘右列
    • 前提:左列等右行
      A × B = C A : i × k B : k × j C : i × j c i j = a i 1 ∗ b 1 j + . . . + a i k ∗ b k j A×B=C\\ A:i×k \\ B:k×j \\ C:i×j \\c_{ij}=a_{i1}*b_{1j}+...+a_{ik}*b_{kj} A×B=CA:i×kB:k×jC:i×jcij=ai1b1j+...+aikbkj
  • 应用:

H o u s e   s i z e s : 2104 1416 1534 852 h θ ( x ) = − 40 + 0.25 x House\ sizes: \\2104 \\1416 \\1534 \\852 \\ h_θ(x) = -40 + 0.25x House sizes:210414161534852hθ(x)=40+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 × 852 ] \left[\begin{matrix} 1 & 2104\\ 1 & 1416\\ 1 & 1534\\ 1 & 852 \end{matrix}\right] × \left[\begin{matrix} -40 \\ 0.25 \end{matrix}\right] =\left[\begin{matrix} \begin{aligned} -40&×1+0.25×2104\\ -40&×1+0.25×1416\\ -40&×1+0.25×1534\\ -40&×1+0.25×852 \end{aligned} \end{matrix}\right] 1111210414161534852×[400.25]=40404040×1+0.25×2104×1+0.25×1416×1+0.25×1534×1+0.25×852
  • 当有3个假设函数时
    H o u s e   s i z e s : 2104 1416 1534 852 1. h θ ( x ) = − 40 + 0.25 x 2. h θ ( x ) = 200 + 0.1 x 3. h θ ( x ) = − 150 + 0.4 x House\ sizes: \\2104 \\1416 \\1534 \\852 \\\begin{aligned} 1.h_θ(x) &= -40 + 0.25x\\ 2.h_θ(x) &= 200+ 0.1x\\ 3.h_θ(x) &= -150 + 0.4x\end{aligned} House sizes:2104141615348521.hθ(x)2.hθ(x)3.hθ(x)=40+0.25x=200+0.1x=150+0.4x
  • 变为矩阵乘法计算结果:
    [ 1 2104 1 1416 1 1534 1 852 ] × [ − 40 200 − 150 0.25 0.1 0.4 ] = [ 486 410 692 314 342 416 344 353 464 173 285 191 ] \left[\begin{matrix} 1 & 2104\\ 1 & 1416\\ 1 & 1534\\ 1 & 852 \end{matrix}\right] × \left[\begin{matrix} \color{red}-40 &200 &\color{blue}-150\\ \color{red}0.25&0.1&\color{blue}0.4 \end{matrix}\right] =\left[\begin{matrix} \color{red}486&410&\color{blue}692\\ \color{red}314&342&\color{blue}416\\ \color{red}344&353&\color{blue}464\\ \color{red}173&285&\color{blue}191 \end{matrix}\right] 1111210414161534852×[400.252000.11500.4]=486314344173410342353285692416464191

三、矩阵乘法的特性(properties)

  • 矩阵乘法不满足交换律(commutative property),即:
    A × B ≠ B × A A×B\neq B×A A×B=B×A
  • 矩阵乘法满足结合律(associative property),即:
    A × ( B × C ) = ( A × B ) × C A×(B×C)=(A×B)×C A×(B×C)=(A×B)×C
  • 单位矩阵(Identity Matrix): 可以看做是单位元,对角线都是1。
    A m × n ⋅ I n × n = I m × m ⋅ A m × n = A m × n I n × n = [ 1 0 0 . . . 0 0 1 0 . . . 0 0 0 1 . . . 0 . . . . . . . . . . . . . . . 0 0 0 . . . 1 ] n × n A_{m×n}·I_{\color{red}n×n}=I_{\color{red}m×m}·A_{m×n}=A_{m×n} \\\quad \\ I_{n×n}=\left[\begin{matrix} 1 & 0&0&...&0\\ 0 & 1&0&...&0\\ 0 & 0&1&...&0\\ ...& ...&...&...&...\\ 0& 0&0&...&1 \end{matrix}\right] \\\qquad\quad n×n Am×nIn×n=Im×mAm×n=Am×nIn×n=100...0010...0001...0...............000...1n×n

四、矩阵的特殊运算(operations)

1.矩阵的逆运算(matrix inverse operation)

  • 逆矩阵: 对于一个m阶(m×m)矩阵A(又叫方阵square),如果存在一个m阶矩阵B,满足以下性质,则称B为A的逆矩阵,记作A^(-1)。
    A B = B A = I 即 A A − 1 = A − 1 A = I \begin{aligned} AB&=BA&=I\\ 即AA^{-1}&=A^{-1}A&=I \end{aligned} ABAA1=BA=A1A=I=I
  • 例如:
    [ 3 4 2 16 ] ⏟ A × [ 0.4 − 0.1 − 0.05 0.075 ] ⏟ A − 1 = [ 1 0 0 1 ] ⏟ A A − 1 = I 2 × 2 \underbrace{ \left[\begin{matrix} 3&4\\ 2&16 \end{matrix}\right] }_{{A}}× \underbrace{ \left[\begin{matrix} 0.4&-0.1\\ -0.05&0.075 \end{matrix}\right] }_{{A^{-1}}} = \underbrace{ \left[\begin{matrix} 1&0\\ 0&1 \end{matrix}\right] }_{AA^{-1}}=I_{2×2} A [32416]×A1 [0.40.050.10.075]=AA1 [1001]=I2×2
  • 奇异矩阵(singular matrix): 即不存在逆矩阵的矩阵,又叫退化矩阵(degenerate matrix)
  • 求矩阵的逆:
    • 可以手算,但是通过软件会更容易一些
    • 下面用Octave软件演示一下

使用Octave计算矩阵的逆

  1. 打开Octave软件(CLI或者GUI都行,咱这里用的是图形界面)
    在这里插入图片描述

  2. 定义一个矩阵A:A = [ 3 4; 2 16]
    在这里插入图片描述

  3. 输入计算逆矩阵的语句:pinv(A)
    就得到了逆矩阵
    在这里插入图片描述

  4. 自定义矩阵inverseOFA并赋值为A的逆 :inverseOFA = pinv(A)
    在这里插入图片描述

  5. 验证相乘结果为单位矩阵:A * inverseOFA
    在这里插入图片描述
    可见主对角线上的元素均为1,由于计算机运算精度问题,其他元素并不为零,但是都是在e-16、e-17(10的指数幂)数量级上,可以近似看做0。

2.矩阵的转置运算(matrix transpose operation)

  • 矩阵的转置: 对于一个矩阵A,定义一个矩阵B,满足以下性质,则称B为A的转置,记作A^(T)或A’。
    b i j = a j i B = A T b_{ij}=a_{ji}\\\quad\\ B=A^T bij=ajiB=AT

总结

  • 本文简单的复习了一下机器学习中用到的一些关于矩阵的基本知识,包括矩阵的加法乘法逆运算转置等,同时也简单的介绍了一下如何使用Octave计算矩阵的逆
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

竹清兰香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值