(机器学习的矩阵)(向量、矩阵与多元线性回归)

机器学习的矩阵

等公交车时我们期待大家要排队,如果 将人改为数字,也就是数字排队,这个就是矩阵(matrix)。

矩阵的表达方式

矩阵的行与列

矩阵是由row和clo(column的简写)组成。

\bigl(\begin{smallmatrix} 1 &2 &3 \\ 4 &5 &6 \\ 7 &8 &8 \end{smallmatrix}\bigr)

row翻译为行,col翻译为列。m\times n矩阵中m代表行(row),n代表列(column)。

矩阵变量名称

矩阵的变量名称常用大写英文字母表示,下列是设定矩阵的变量名称是A。

A=\bigl(\begin{smallmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{smallmatrix}\bigr)

常见的矩阵表达方式

其它矩阵表达方式:

\begin{bmatrix} 1 &2 \\ 3 &4 \end{bmatrix}                \begin{vmatrix} 1 & 2\\ 3 & 4 \end{vmatrix}                \begin{Vmatrix} 1 & 2\\ 3 & 4 \end{Vmatrix}

矩阵元素表达方式

矩阵元素常用下标表示,可以参考下列书写方式:

a_{ij}

i是行号,j是列号。

\begin{pmatrix} a_{11} & \cdots & a_{1n}\\ \vdots & \ddots & \vdots \\ a_{m1} & \cdots & a_{mn} \end{pmatrix}                        \begin{pmatrix} a_{1,1} & \cdots & a_{1,n}\\ \vdots & \ddots & \vdots \\ a_{m,1} & \cdots & a_{m,n} \end{pmatrix}

矩阵相加与相减

基础概念

有2个矩阵如下:

A=\begin{pmatrix} a_{1,1} & \cdots &a_{1,n} \\ \vdots & \ddots &\vdots \\ a_{m,1} & \cdots & a_{m,n} \end{pmatrix}                        B=\begin{pmatrix} b_{1,1} & \cdots &b_{1,n} \\ \vdots & \ddots &\vdots \\ b_{m,1} & \cdots & b_{m,n} \end{pmatrix}

矩阵相加或相减,相当于相同位置的元素执行相加或是相减,所以不同大小的矩阵无法执行相加减,如下所示:

A+B=\begin{pmatrix} a_{1,1}+b_{1,1} & \cdots &a_{1,n}+b_{1,n} \\ \vdots & \ddots &\vdots \\ a_{m,1}+b_{m,1} & \cdots & a_{m,n}+b_{m,n} \end{pmatrix}

A-B=\begin{pmatrix} a_{1,1}-b_{1,1} & \cdots &a_{1,n}-b_{1,n} \\ \vdots & \ddots &\vdots \\ a_{m,1}-b_{m,1} & \cdots & a_{m,n}-b_{m,n} \end{pmatrix}

矩阵加减运算的交换律与结合律是成立的。

交换律:A+B=B+A

结合律:(A+B)+C=A+(B+C)

Python实践

定义矩阵可以使用numpy的matrix()方法,有一个矩阵如下:

A=\begin{pmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{pmatrix}

>>> import numpy as np
>>> A=np.matrix([[1,2,3],[4,5,6]])
>>> A
matrix([[1, 2, 3],
        [4, 5, 6]])
>>>

矩阵相加与相减的应用

import numpy as np

A = np.matrix([[1, 2, 3], [4, 5, 6]])
B = np.matrix([[4, 5, 6], [7, 8, 9]])

print('A + B = {}'.format(A + B))
print('A - B = {}'.format(A - B))

运行结果如下:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
A + B = [[ 5  7  9]
 [11 13 15]]
A - B = [[-3 -3 -3]
 [-3 -3 -3]]

[Done] exited with code=0 in 4.159 seconds

矩陈乘以实数

矩阵可以乘以实数,操作方式是每个矩阵元素乘以该实数,下列是将矩阵乘以实数k的实例。

kA=\begin{pmatrix} ka_{1,1} & \cdots & ka_{1,n}\\ \vdots & \ddots & \vdots \\ ka_{m,1} & \cdots &ka_{m,n} \end{pmatrix}

矩阵乘以实数的交换律、结合律与分配律是成立的。

交换律:kA=Ak

结合律:jkA=j(kA)

分配律:(j+k)A=jA+kA

k(A+B)=kA+kB

矩阵乘以2。

import numpy as np

A = np.matrix([[1, 2, 3], [4, 5, 6]])

print('2 * A   = {}'.format(2 * A))
print('0.5 * A = {}'.format(0.5 * A))

运行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
2 * A   = [[ 2  4  6]
 [ 8 10 12]]
0.5 * A = [[0.5 1.  1.5]
 [2.  2.5 3. ]]

[Done] exited with code=0 in 4.375 seconds

矩阵乘法

矩阵相乘很重要一点是,左侧矩阵的列数与右侧矩阵的行数要相同,才可以执行矩阵相乘。

乘法基本规则

A矩阵是i\times j,B矩阵是j\times k

AB_{ik}=\sum_{i=1}^{n}a_{i,j}b_{j,k}

使用numpy模块,可以使用*或是@运算符执行矩阵的乘法。

import numpy as np

A = np.matrix([[1, 2], [3, 4]])
B = np.matrix([[5, 6], [7, 8]])
print('A * B = {}'.format(A * B))

C = np.matrix([[1, 0, 2], [-1, 3, 1]])
D = np.matrix([[3, 1], [2, 1], [1, 0]])
print('C @ D = {}'.format(C @ D))

运行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
A * B = [[19 22]
 [43 50]]
C @ D = [[5 1]
 [4 2]]

[Done] exited with code=0 in 2.09 seconds

乘法案例

下表是甲和乙要购买水果的数量:

名字 香蕉 芒果 苹果
2 3 1
3 2 5

下表是超市与百货公司的水果价格:

水果名称 超市价格 百货公司价格
香蕉 30 50
芒果 60 80
苹果 50 60

计算甲和乙在超市和百货公司购买各需要多少金额。

import numpy as np

A = np.matrix([[2, 3, 1], [3, 2, 5]])
B = np.matrix([[30, 50], [60, 80], [50, 60]])
print('A * B = {}'.format(A * B))

运行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
A * B = [[290 400]
 [460 610]]

[Done] exited with code=0 in 2.403 seconds

假设各式水果热量如下:

水果 热量
香蕉 30卡路里
芒果 50卡路里
苹果 20卡路里

甲和乙各吃数量如下,计算会产生多少卡路里。

名字 香蕉 芒果 苹果
1 2 1
2 1 2

代码如下:

import numpy as np

A = np.matrix([[1, 2, 1], [2, 1, 2]])
B = np.matrix([[30], [50], [20]])
print('A * B = {}'.format(A * B))

运行结果如下:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
A * B = [[150]
 [150]]

[Done] exited with code=0 in 2.34 seconds

矩阵乘法规则

矩阵运算时,结合律和分配律是成立的。

结合律:A\times B\times C=(A\times B)\times C=A\times (B\times C)

分配律:A\times (B-C)=A\times B-A\times C

矩阵运算时,交换律是不成立的。

A\times B 不等于 B\times A

验证A\times B 不等于 B\times A

代码如下:

import numpy as np

A = np.matrix([[1, 2], [3, 4]])
B = np.matrix([[5, 6], [7, 8]])
print('A * B = {}'.format(A * B))
print('B * A = {}'.format(B * A))

运行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
A * B = [[19 22]
 [43 50]]
B * A = [[23 34]
 [31 46]]

[Done] exited with code=0 in 4.095 seconds

方形矩阵

一个矩阵如果行数(row)等于列数(column),方形矩阵(square matrix)。

单位矩阵

一个方形矩阵如果从左上到右下对角线的元素皆是1,其它元素皆是0,这个矩阵称单位矩阵(identity matrix)。

单位矩阵有时用大写英文E或I表示。

单位矩阵就类似阿拉伯数字1,任何矩阵与单位矩阵相乘,结果皆是原来的矩阵。

验证与单位矩阵相乘结果不变。

import numpy as np

A = np.matrix([[1, 2], [3, 4]])
B = np.matrix([[1, 0], [0, 1]])
print('A * B = {}'.format(A * B))
print('B * A = {}'.format(B * A))

运行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
A * B = [[1 2]
 [3 4]]
B * A = [[1 2]
 [3 4]]

[Done] exited with code=0 in 2.165 seconds

反矩阵

基础概念

只有方形矩阵(square matrix)才可以有反矩阵(inverse matrix),一个矩阵乘以它的反矩阵,可以得到单位矩阵E,可以参考下列概念。

A\times A^{-1}=E         或是        A^{-1}\times A=E

如果一个的矩阵,它的反矩阵公式如下:

A=\begin{pmatrix} a_{1,1} &a_{1,2} \\ a_{2,1} &a_{2,2} \end{pmatrix}        A^{-1}=\frac{1}{a_{1,1}a_{2,2}-a_{1,2}a_{2,1}}\begin{pmatrix} a_{2,2} &-a_{1,2} \\ -a_{2,1} & a_{1,1} \end{pmatrix}

反矩阵另一个存在条件是a_{1,1}a_{2,2}-a_{1,2}a_{2,1}不等于0。下列是一个矩阵A与反矩阵A^{-1}的实例。

A=\begin{pmatrix} 2 &3 \\ 5 &7 \end{pmatrix}                A^{-1}=\frac{1}{14-15}\begin{pmatrix} 7 &-3 \\ -5 & 2 \end{pmatrix}=\begin{pmatrix} -7 &3 \\ 5 &-2 \end{pmatrix}

Python实践

导入numpy模块,可以使用inv()方法计算反矩阵。

import numpy as np

A = np.matrix([[2, 3], [5, 7]])
B = np.linalg.inv(A)
print('A_inv = {}'.format(B))
print('E     = {}'.format((A * B).astype(np.int64)))

运行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
A_inv = [[-7.  3.]
 [ 5. -2.]]
E     = [[1 0]
 [0 0]]

[Done] exited with code=0 in 4.747 seconds

用反矩阵解联立方程式

假设有一个联立方程式如下:

3x+2y=5

x+2y=-1

将上述联立方程式使用下列矩阵表达。

\begin{pmatrix} 3 &2 \\ 1 &2 \end{pmatrix}\begin{pmatrix} x\\ y \end{pmatrix}=\begin{pmatrix} 5\\ -1 \end{pmatrix}

\begin{pmatrix} 3 &2 \\ 1 &2 \end{pmatrix}的反矩阵是\begin{pmatrix} 0.5 &-0.5 \\ -0.25 &0.75 \end{pmatrix},在等号两边乘以相同的反矩阵,可以得到下列结果。

\begin{pmatrix} 0.5 &-0.5 \\ -0.25 &0.75 \end{pmatrix}\begin{pmatrix} 3 &2 \\ 1 &2 \end{pmatrix}\begin{pmatrix} x\\ y \end{pmatrix}=\begin{pmatrix} 0.5 &-0.5 \\ -0.25 &0.75 \end{pmatrix}\begin{pmatrix} 5\\ -1 \end{pmatrix}

推导可以得到下列结果。

\begin{pmatrix} x\\ y \end{pmatrix}=\begin{pmatrix} 3\\ -2 \end{pmatrix}

可以得到上述联立方程式的解是x=3,y=-2。

使用反矩阵概念验证上述执行结果 。

import numpy as np

A = np.matrix([[3, 2], [1, 2]])
A_inv = np.linalg.inv(A)
B = np.matrix([[5], [-1]])
print('{}'.format(A_inv * B))

运行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
[[ 3.]
 [-2.]]

[Done] exited with code=0 in 2.025 seconds

张量

张量是数学的堆栈结构。

张量用轴空间表示。

标量则是0轴张量,向量称1轴张量,矩阵称2轴张量,3维空间称3轴张量。

定义3维数据,同时使用shape()方法列出数据外形。

import numpy as np

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

print('{}'.format(A))
print('shape = {}'.format(np.shape(A)))

执行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\ch21_10.py"
[[[ 1  2]
  [ 3  4]]

 [[ 5  6]
  [ 7  8]]

 [[ 9 10]
  [11 12]]]
shape = (3, 2, 2)

[Done] exited with code=0 in 1.479 seconds

转置矩阵

基础概念

转置矩阵的概念就是将矩阵内列的元素与行的元素对调,所以n\times m的矩阵就可以转成m\times n的矩阵。

矩阵是A,转置矩阵的表达方式是A^T

Python实践

设计转置矩阵时可以使用numpy模块的transpose(),也可以使用T。

转置矩阵的应用。

import numpy as np

A = np.array([[0, 2, 4, 6],
              [1, 3, 5, 7]])              
B = A.T
print('{}'.format(B))
C = np.transpose(A)
print('{}'.format(C))

运行结果:

[Running] python -u "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\tempCodeRunnerFile.py"
[[0 1]
 [2 3]
 [4 5]
 [6 7]]
[[0 1]
 [2 3]
 [4 5]
 [6 7]]

[Done] exited with code=0 in 2.633 seconds

转置矩阵的规则 

转置矩阵可以再转置还原矩阵内容。

(A^T)^T=A

矩阵相加再转置,等于各矩阵转置再相加。

(A+B)^T=A^T+B^T

标量c乘矩阵再载转置,与先转置再乘以标量结果相同。

(cA)^T=cA^T

转置矩阵后再做反矩阵,等于反矩阵后转置

(A^T)^{-1}=(A^{-1})^T

矩阵相乘再转置,等于各矩阵转置后交换次序再相乘。

(AB)^T=B^TA^T

转置矩阵的应用

省略

向量、矩阵与多元线性回归

向量应用在线性回归

单纯的线性方程式如下:

y=ax+b

x代表每年的拜访数据,y是每年国际证照的销售数据。如果数据量庞大,收集了n年,则可以使用向量表达此数据。

x=(x_1\; x_2\cdots x_n)        #下标代表第n年,x_n是第n年拜访客户次数

y=(y_1\; y_2\cdots y_n)        #下标代表第n年,y_n是第n年销售考卷数

由于上述x_ny_n代入y=ax+b会有误差\varepsilon,所以可以为误差加上下标,这样误差可以使用误差向量表示:

\varepsilon =(\varepsilon_1\;\varepsilon_2\;\cdots \;\varepsilon_n)

现在的线性方程式:y=ax+b+\varepsilon

现在斜率a与截距b是标量,由于斜率a乘以向量x后会是n维向量,所以必须将标量b改为向量,如下所示:b=(b_1\;b_2\;\cdots \;b_n)

整个线性方程式执行推导:

y=ax+b+\varepsilon

\varepsilon =y-ax-b

使用最小平方法计算误差平方的总和:\varepsilon _{i}^{2}=\sum_{i=1}^{n}\varepsilon _{i}^{2}

误差向量\varepsilon的内积,推导公式:\varepsilon _{i}^{2}=\sum_{i=1}^{n}\varepsilon _{i}^{2}=\left \| \varepsilon \right \|^2

执行误差平方最小化,等同是计算向量内积:\varepsilon \cdot \varepsilon =(y-ax-b)\cdot (y-ax-b) 

向量应用在多元线性回归

在多元回归中,习惯会用\beta当作斜率的系数,截距则用\beta_0代替,整个多元回归通式可以使用下列公式表达:y=\beta _1x_1+\beta _2x_2+\cdots +\beta _nx_n+\beta _0+\varepsilon

矩阵应用在多元线性回归

省略

将截距放入矩阵

省略

简单的线性回归

省略

猜你喜欢

转载自blog.csdn.net/DXB2021/article/details/127196611