推荐直接网站在线阅读:aicoting AI算法面试学习在线网站
向量与矩阵
向量(Vector)
向量可以看作是有序数值的集合,用于表示样本特征。
例如,一个房屋数据可以表示为: <math xmlns="http://www.w3.org/1998/Math/MathML"> x = [ 面积 房间数 楼层 ] = [ 120 3 5 ] \mathbf{x} = \begin{bmatrix} \text{面积} \\ \text{房间数} \\ \text{楼层} \end{bmatrix} = \begin{bmatrix} 120 \\ 3 \\ 5 \end{bmatrix} </math>x= 面积房间数楼层 = 12035
在机器学习中,样本就是向量,特征数决定了向量的维度。
矩阵(Matrix)
多个样本堆叠成矩阵,例如一个包含 m 个样本、每个样本 n 个特征的数据集可以表示为: <math xmlns="http://www.w3.org/1998/Math/MathML"> X = [ x 11 x 12 ⋯ x 1 n x 21 x 22 ⋯ x 2 n ⋮ ⋮ ⋱ ⋮ x m 1 x m 2 ⋯ x m n ] X = \begin{bmatrix} x_{11} & x_{12} & \cdots & x_{1n} \\ x_{21} & x_{22} & \cdots & x_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ x_{m1} & x_{m2} & \cdots & x_{mn} \end{bmatrix} </math>X= x11x21⋮xm1x12x22⋮xm2⋯⋯⋱⋯x1nx2n⋮xmn
其中,每一行是一个样本,每一列是一个特征。
向量运算与几何意义
内积(Dot Product)
两个向量 <math xmlns="http://www.w3.org/1998/Math/MathML"> a , b ∈ R n \mathbf{a}, \mathbf{b} \in \mathbb{R}^n </math>a,b∈Rn 的内积定义为: <math xmlns="http://www.w3.org/1998/Math/MathML"> a ⋅ b = ∑ i = 1 n a i b i = a T b \mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^n a_i b_i = \mathbf{a}^T \mathbf{b} </math>a⋅b=∑i=1naibi=aTb
几何意义上,内积表示两个向量的相似度: <math xmlns="http://www.w3.org/1998/Math/MathML"> a ⋅ b = ∥ a ∥ ∥ b ∥ cos θ \mathbf{a} \cdot \mathbf{b} = \|\mathbf{a}\|\|\mathbf{b}\|\cos\theta </math>a⋅b=∥a∥∥b∥cosθ
这在推荐系统(如计算用户与物品的相似度)和分类(如支持向量机的超平面)中至关重要。
范数(Norm)
向量的长度由范数衡量。常见的有:
- L2 范数 : <math xmlns="http://www.w3.org/1998/Math/MathML"> x ∥ 2 = ∑ i = 1 n x i 2 \mathbf{x}\|2 = \sqrt{\sum{i=1}^n x_i^2} </math>x∥2=∑i=1nxi2
- L1 范数 : <math xmlns="http://www.w3.org/1998/Math/MathML"> x ∥ 1 = ∑ i = 1 n ∣ x i ∣ \mathbf{x}\|1 = \sum{i=1}^n |x_i| </math>x∥1=∑i=1n∣xi∣
在正则化中,L1 范数鼓励稀疏解(特征选择),L2 范数则抑制过大权重(防止过拟合)。
矩阵运算与模型计算
矩阵乘法
如果输入样本为 <math xmlns="http://www.w3.org/1998/Math/MathML"> x ∈ R n \mathbf{x} \in \mathbb{R}^n </math>x∈Rn,权重矩阵W为 <math xmlns="http://www.w3.org/1998/Math/MathML"> R m × n \mathbb{R}^{m \times n} </math>Rm×n ,则输出为: <math xmlns="http://www.w3.org/1998/Math/MathML"> y = W x \mathbf{y} = W \mathbf{x} </math>y=Wx
这就是神经网络中 一层全连接层 的计算方式。
转置与对称矩阵
- 矩阵转置 <math xmlns="http://www.w3.org/1998/Math/MathML"> A T A^T </math>AT 在内积与相似度计算中常见。
- 对称矩阵(如协方差矩阵)在 PCA、特征值分解中起核心作用。
在机器学习中,数据通常是高维的,直接处理既费计算又不易理解。特征值分解(Eigen Decomposition)和 奇异值分解 ( SVD ) 提供了强大的工具,让我们能够发现数据的"核心方向",实现降维、特征提取和压缩。
特征值分解与奇异值分解(SVD)
特征值分解
给定一个方阵 <math xmlns="http://www.w3.org/1998/Math/MathML"> R n × n \mathbb{R}^{n \times n} </math>Rn×n,如果存在标量 <math xmlns="http://www.w3.org/1998/Math/MathML"> λ \lambda </math>λ 和非零向量 <math xmlns="http://www.w3.org/1998/Math/MathML"> v \mathbf{v} </math>v,使得: <math xmlns="http://www.w3.org/1998/Math/MathML"> v = λ v \mathbf{v} = \lambda \mathbf{v} </math>v=λv,则称 <math xmlns="http://www.w3.org/1998/Math/MathML"> λ \lambda </math>λ 为 特征值 , <math xmlns="http://www.w3.org/1998/Math/MathML"> v \mathbf{v} </math>v为 特征向量。
考虑一个简单的 2x2 矩阵: <math xmlns="http://www.w3.org/1998/Math/MathML"> A = [ 2 1 1 2 ] A = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} </math>A=[2112]
求解特征值和特征向量: <math xmlns="http://www.w3.org/1998/Math/MathML"> det ( A − λ I ) = 0 ⇒ ∣ 2 − λ 1 1 2 − λ ∣ = 0 \det(A - \lambda I) = 0 \quad \Rightarrow \quad \begin{vmatrix} 2-\lambda & 1 \\ 1 & 2-\lambda \end{vmatrix} = 0 </math>det(A−λI)=0⇒ 2−λ112−λ =0
得到特征值 <math xmlns="http://www.w3.org/1998/Math/MathML"> λ 1 = 3 , λ 2 = 1 \lambda_1 = 3, \lambda_2 = 1 </math>λ1=3,λ2=1,对应特征向量分别为: <math xmlns="http://www.w3.org/1998/Math/MathML"> v 1 = [ 1 1 ] , v 2 = [ 1 − 1 ] \mathbf{v}_1 = \begin{bmatrix} 1 \\ 1 \end{bmatrix}, \quad \mathbf{v}_2 = \begin{bmatrix} 1 \\ -1 \end{bmatrix} </math>v1=[11],v2=[1−1]
矩阵 A 作用在 v1 上,方向不变,只拉伸了 3 倍;作用在 v2 上,方向不变,只拉伸了 1 倍。
特征值分解的应用场景包括:
- PCA 降维 :在 PCA(主成分分析)中,我们对数据的协方差矩阵 <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 m X T X \frac{1}{m} X^T X </math>m1XTX 进行特征值分解,得到最大方差方向上的主成分。
- 谱聚类:用 Laplacian 矩阵特征向量划分数据
- 稳态分析:马尔可夫链转移矩阵的特征值分析收敛性
奇异值分解(SVD)
对于任意矩阵 <math xmlns="http://www.w3.org/1998/Math/MathML"> X ∈ R m × n X \in \mathbb{R}^{m \times n} </math>X∈Rm×n,可以分解为: <math xmlns="http://www.w3.org/1998/Math/MathML"> X = U Σ V T X = U \Sigma V^T </math>X=UΣVT
其中:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> U ∈ R m × m U \in \mathbb{R}^{m \times m} </math>U∈Rm×m 和 <math xmlns="http://www.w3.org/1998/Math/MathML"> V ∈ R n × n V \in \mathbb{R}^{n \times n} </math>V∈Rn×n 是正交矩阵
- <math xmlns="http://www.w3.org/1998/Math/MathML"> Σ ∈ R m × n \Sigma \in \mathbb{R}^{m \times n} </math>Σ∈Rm×n是对角矩阵,包含奇异值 <math xmlns="http://www.w3.org/1998/Math/MathML"> σ 1 ≥ σ 2 ≥ ⋯ ≥ 0 \sigma_1 \ge \sigma_2 \ge \dots \ge 0 </math>σ1≥σ2≥⋯≥0
奇异值 表示矩阵在各个方向上的"能量大小"。SVD 可用于非 方阵,比特征值分解更通用。
SVD 在降维、信息检索(LSA)、推荐系统(矩阵分解)中应用广泛。
考虑矩阵 <math xmlns="http://www.w3.org/1998/Math/MathML"> X = [ 3 1 1 3 ] X = \begin{bmatrix} 3 & 1 \\ 1 & 3 \\ \end{bmatrix} </math>X=[3113]
SVD 分解结果近似为: <math xmlns="http://www.w3.org/1998/Math/MathML"> U = [ 1 2 − 1 2 1 2 1 2 ] , Σ = [ 4 0 0 2 ] , V T = [ 1 2 1 2 − 1 2 1 2 ] U = \begin{bmatrix} \frac{1}{\sqrt{2}} & -\frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \end{bmatrix}, \quad \Sigma = \begin{bmatrix} 4 & 0 \\ 0 & 2 \end{bmatrix}, \quad V^T = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ -\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \end{bmatrix} </math>U=[2 12 1−2 12 1],Σ=[4002],VT=[2 1−2 12 12 1]
矩阵 X 可以看作先旋转( <math xmlns="http://www.w3.org/1998/Math/MathML"> V T V^T </math>VT),再拉伸( <math xmlns="http://www.w3.org/1998/Math/MathML"> Σ \Sigma </math>Σ),再旋转(U),得到原矩阵。保留最大奇异值对应的方向,就能得到数据最重要的信息。
奇异值分解的应用场景包括:
- 推荐系统:矩阵分解找到用户和物品的潜在因子
- 图像压缩:保留主奇异值重建近似图像
- 潜在语义分析(LSA) :从文档-词矩阵提取潜在语义结构
线性代数与机器学习模型
线性回归
预测公式可以写作: <math xmlns="http://www.w3.org/1998/Math/MathML"> y ^ = X w \hat{\mathbf{y}} = X \mathbf{w} </math>y^=Xw
其中, <math xmlns="http://www.w3.org/1998/Math/MathML"> w \mathbf{w} </math>w 是回归系数。利用最小二乘法,参数解为: <math xmlns="http://www.w3.org/1998/Math/MathML"> w = ( X T X ) − 1 X T y \mathbf{w} = (X^T X)^{-1} X^T \mathbf{y} </math>w=(XTX)−1XTy
这是线性代数在回归分析中的直接应用。
支持向量机(SVM)
SVM 本质上是寻找一个超平面 <math xmlns="http://www.w3.org/1998/Math/MathML"> w T x + b = 0 \mathbf{w}^T \mathbf{x} + b = 0 </math>wTx+b=0,最大化数据点与超平面的几何间隔。这是典型的线性代数与凸优化结合的例子。
神经网络
神经网络中,每一层的计算都是矩阵运算: <math xmlns="http://www.w3.org/1998/Math/MathML"> h ( l ) = f ( W ( l ) h ( l − 1 ) + b ( l ) ) \mathbf{h}^{(l)} = f(W^{(l)} \mathbf{h}^{(l-1)} + \mathbf{b}^{(l)}) </math>h(l)=f(W(l)h(l−1)+b(l))
其中 <math xmlns="http://www.w3.org/1998/Math/MathML"> f ( ⋅ ) f(\cdot) </math>f(⋅)是激活函数。可见整个网络就是一连串的线性变换+非线性映射。
最新的文章都在公众号aicoting更新,别忘记关注哦!!!