WEEK 1、2、3
本文为个人笔记,只记了重要内容,不适合新手入手
线性回归
- 样本
- ,假设 具有 个特征
- 假想函数(目标函数):
- 表达式视具体情况而定
线性回归
中线性
的含义是参数 都是一次的,而非 的自变量,回归
应该是代价函数 的自变量的回归- 称为参数,Machine Learning 的目的就是求出参数的合适取值使得 更能体现 的映射关系
- 为此我们提出了一个衡量参数取值好坏的函数——代价函数:
- 现在问题转变为了求使得代价函数 最小时的参数 ,下面给出两种方法:
梯度下降法
时间复杂度
,适合当 n > 10000 时 (k 为学习步数)
- 称学习速率(或步长),取值视情况而定,取值过大会导致不收敛;若当取值适当,在趋近极值时 也会变小,所以此时梯度下降法是收敛的,所以没必要担心 在趋近极值点的时候不改变导致无法收敛。
- 注意要先求出所有的 ,再对所有的
若取
,求偏导后可以写成:
用矩阵来表述的话:
特征优化:
尽量让
- 为第 个特征的平均取值, 为第 个特征的极差
传统方法
时间复杂度 ,适合当 n < 10000 时
直接令
,解得:
*
code
featureNormalize
function [X_norm, mu, sigma] = featureNormalize(X)
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
for iter = 1:size(X, 2)
mu(iter) = mean(X(:, iter));
sigma(iter) = std(X(:, iter));
X_norm(:, iter) = (X(:, iter) - mu(iter)) / sigma(iter);
end
end
computeCostMulti
function J = computeCost(X, y, theta)
m = length(y); % number of training examples
J = 1 / (2 * m) * sum((X * theta - y) .^ 2);
end
gradientDescentMulti
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
theta -= alpha / m * X' * (X * theta - y);
J_history(iter) = computeCostMulti(X, y, theta);
end
end
normalEqn
function [theta] = normalEqn(X, y)
theta = zeros(size(X, 2), 1);
theta = pinv(X' * X) * X' * y;
end
Octave
https://www.gnu.org/software/octave/
Ubuntu Install:
sudo apt-add-repository ppa:octave/stable
sudo apt-get update
sudo apt-get install octave
Octave 入门:
内容比较多,推荐两篇文章
http://blog.csdn.net/weixin_36106941/article/details/64443944
https://www.cnblogs.com/leezx/p/5635056.html
逻辑回归
线性回归是用来预测某个点的取值,逻辑回归是预测某个点具有某种特征的概率
为了达到我们的目的,重现建立模型:
Note: or always , and
log
is ln
可以写成:
对代价函数求偏倒后发现和线性回归代价函数求偏倒的结果形式上是完全一样的:
cost function code
function [J, grad] = costFunction(theta, X, y)
m = length(y); % number of training examples
tmp = sigmoid(X*theta);
J = -1 / m * (y'*log(tmp)+(1-y)'*log(1-tmp));
grad = 1 / m * X' * (sigmoid(X * theta) - y);
end
高级优化算法
Optimization algorithms:
- Gradient descent
- Conjugate gradient
- BFGS
- L-BFGS
Advantages:
- No need to manually pick
- Often faster than grdicent descent.
disadvantages:
- More complex
调用 Octave 优化算法 example :
[first] 定义 cost function:
costFunction.m
function [jval, gradient] = costFunction(theta, X, y)
% jval := J(theta)
% gradient := grad J(theta)
[then] 键入命令
options = optimset('GrandObj', 'on', 'MaxIter', '100');
initialTheta = zeros(n + 1, 1)
[optTheta, functionVal, exitFlag] = fminunc(@(t)costFunction(t, X, y), initialTheta, options);
正则化项
线性回归
为了防止 overfitting(过度拟合),对 cost function 引入了正则化项
求偏导:
注意!!! 不需要惩罚
梯度下降法
Repeat {
}
对上整理后:
由于 ,可以将 写成
常规方法
直接令
,解得:
- Suppose (m: examples , n : features)
- 在 时,括号内的矩阵总是可逆的
逻辑回归
对 cost function 引入了正则化项
求偏导数后结果形式和线性回归是一样的:
注意!!! 不需要惩罚
cost function code
function [J, grad] = costFunctionReg(theta, X, y, lambda)
tmp = sigmoid(X*theta);
J = -1 / m * (y'*log(tmp)+(1-y)'*log(1-tmp)) + lambda / (2 * m) * sum(theta(2:size(theta, 1),1) .^ 2);
theta(1) = 0;
grad = 1 / m * (X' * (tmp - y) + lambda * theta);
end
https://www.coursera.org/learn/machine-learning
教学方: Andrew Ng, Co-founder, Coursera; Adjunct Professor, Stanford University; formerly head of Baidu AI Group/Google Brain