【笔记】Machine Learning - Andrew Ng
Coursera
Bilibili 5-1
Lecture notes
Multiple features
Notation :
\(n\) = number of features, that is each training example is a vector \(\vec{x} = (x_1, x_2, \dots, x_n)^T\)
\(x^{(i)}\) denotes \(i^{th}\) training example
\(x^{(i)}_{j}\) denotes feature \(j\) in \(i^{th}\) training example
\(y^{(i)}\) for each \(x^{(i)}\)
Hypothesis
Suppose \(h_{\theta}(\vec{x}) = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \dots + \theta_n x_n = (\theta_0, \dots, \theta_n) \cdot (x_0, x_1, \dots, x_n)^T = \Theta^T \vec{x}\)
Here always \(x_0 = 1\)
: Multivariate linear regression 多元线性回归
and the cost function : \(J(\Theta) = \sum_{i}f(h_{\theta}(\vec{x}^{(i)}), y^{(i)})\)
is defined as :
Gradient descent algorithm
minimize cost function \(J(\Theta) = J(\theta_0, \theta_1, \dots, \theta_n)\)
repeat until convergence :
\(\theta_i := \theta_i - \alpha \frac{\partial}{\partial \theta_i}J(\Theta)\)
- Feature Scaling
对特征值做一些处理,用来提升梯度下降的速度,对于以下描述的情况:
显然由于 \(\alpha\) 是常量,对于在同一点上偏导过大的维,迈的步子会比较大;例如二维画出的等高线,坡度越陡跨的越大;
若两个维度的二阶导差距很大,在等高线上就表现成一个偏心率很大的椭圆;此时若 \(\alpha\) 很小,则长轴方向走的慢;若 \(\alpha\) 大,则短轴方向波动大难以收敛;
Feature Scaling :就是放缩一下,将每个 feature 的取值范围都放缩至大概同一个数量级,例如可以除以各自原本的取值范围;
(感觉重点应该是令每个维度的二阶导相当..?)
Mean normalization:令每个特征值都以 0 为平均值,其实就是 replace \(x_i\) with \(x_i-\mu_i\),除了 \(x_0 = 1\)
最终有 \(x_i' = \frac{x_i - \mu_i}{s_i}\)
其中 \(\mu_i\) 是训练出的 \(x_i\) 的平均值,\(s_i\) 是 \(x_i\) 的取值范围大小(max-min)或标准差或之类的能刻画取值范围大小的东西,最后每个特征值的取值大概都在 [-1, 1] 即可 - Debugging
根据 \(J(\theta)\) 和迭代次数的曲线判断有没有正常运行 - Selecting Learning Rate \(\alpha\)
能够证明的是只要 \(\alpha\) 足够小,每次迭代后 \(J(\theta)\) 都会减小
总之就是如果 \(J\) 在迭代时无法收敛,就试试减小 \(\alpha\),但太小又会跑的慢,所以根据 debug 的表现尝试不同的 \(\alpha\) 值:1e-2, 1e-1, 1, ...
emmmmm
Polynomial Regression 多项式回归
Fit a polynomial \(\theta_0 + \theta_1 x + \theta_2 x^2 + \dots\) to data
可以令 \(x_i' = x^i\),就变成了 \(h_{\theta}(\vec{x}')\) 的 Multivariate linear regression。。。
至于应该选哪些阶来拟合(包括根号或其他函数),根据图像和个人经验。。。然后把各项当成不同变量就行了
Normal Equation 正规方程
一阶偏导为 0 的地方就可能能取到最小值,对于上文定义过的 cost function,有这么一种方法用于计算:
suppose \(m\) examples \((\vec{x}^{(1)}, y^{(1)}), \dots, (\vec{x}^{(m)}, y^{(m)})\),
\(n\) features, \(\vec{x} = (x_0, x_1, \dots, x_n)^T \in {\mathbb{R}}^{n+1}\)
then \(X = ((\vec{x}^{(1)})^T, (\vec{x}^{(2)})^T, \dots, (\vec{x}^{(m)})^T)^T\) (m*(n+1) matrix)
and \(\vec{y} = (y^{(1)}, y^{(1)}, \dots, y^{(m)})^T\)
then :
in Octave :
pinv(X'*X)*X'*y
// pinv : pseudo-inverse 伪逆,总之就是当矩阵不可逆时也能保证算出来的结果正确
in this way feature scaling is not necessary
\(O(n^3)\), however