2、 Linear Regression 线性回归 [Stanford - Machine Learning]
2014-11-27 00:37
453 查看
线性回归就是在(features+1)维空间中通过梯度下降的方法找到一条直线,使这条直线尽量匹配所有的点(每一个点是一个样本,当然可以自己构建样本如:长*宽=面积)
线性回归的知识点:(图片看不清可以在新标签打开或者保存)
·
线性回归如何编程实现:(Matlab)
1、Cost Function函数:可以用来观察每次的Cost是否下降(把每一次梯度下降递归的结果保存起来,作图就可以观察到了),便于选择合适的训练速率alpha
2、Gradient descent函数:循环调用,不断修正theta的值,直到收敛
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 = theta - X'*(X*theta-y) * alpha/m;
% Save the cost J in every iteration
J_history(iter) = computeCostMulti(X, y, theta);
end
end
3、Feature Scaling:对训练样本X,因为不同样本代表的信息不同,大小差距很大,一般要进行缩放或者归一化
function [X_norm, mu, sigma] = featureNormalize(X)
mu = mean(X); % 保存平均值 后面预测新的样本时用到,因为新的样本也要先Normalize处理
sigma = std(X,0,1); % 保存标准差 后面预测新的样本时用到
X_norm = (X-repmat(mean(X),length(X),1))./repmat(sigma,length(X),1);
end
4、有了以上三个函数就可以求出theta了,也就是求出了我们的直线,最后让我们实现它,并预测一个样本
% Load Data
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
% Scale features and set them to zero mean
[X mu sigma] = featureNormalize(X);
% Add intercept term to X
X = [ones(m, 1) X];
% Choose some alpha value
alpha = 1;
num_iters = 50;
% Init Theta and Run Gradient Descent
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
% Plot the convergence graph
figure;
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
% Display gradient descent's result
fprintf('Theta computed from gradient descent: \n');
fprintf(' %f \n', theta);
fprintf('\n');
% Estimate the price of a 1650 sq-ft, 3 br house
XX = [1650 3];
XX=[ones(1, 1) (XX - mu)./sigma];
price = XX *theta ;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
'(using gradient descent):\n $%f\n'], price);
线性回归的知识点:(图片看不清可以在新标签打开或者保存)
·
线性回归如何编程实现:(Matlab)
1、Cost Function函数:可以用来观察每次的Cost是否下降(把每一次梯度下降递归的结果保存起来,作图就可以观察到了),便于选择合适的训练速率alpha
function J = computeCost(X, y, theta) m = length(y); % number of training examples J=sum((X*theta-y).^2) /(2*m); end
2、Gradient descent函数:循环调用,不断修正theta的值,直到收敛
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 = theta - X'*(X*theta-y) * alpha/m;
% Save the cost J in every iteration
J_history(iter) = computeCostMulti(X, y, theta);
end
end
3、Feature Scaling:对训练样本X,因为不同样本代表的信息不同,大小差距很大,一般要进行缩放或者归一化
function [X_norm, mu, sigma] = featureNormalize(X)
mu = mean(X); % 保存平均值 后面预测新的样本时用到,因为新的样本也要先Normalize处理
sigma = std(X,0,1); % 保存标准差 后面预测新的样本时用到
X_norm = (X-repmat(mean(X),length(X),1))./repmat(sigma,length(X),1);
end
4、有了以上三个函数就可以求出theta了,也就是求出了我们的直线,最后让我们实现它,并预测一个样本
% Load Data
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
% Scale features and set them to zero mean
[X mu sigma] = featureNormalize(X);
% Add intercept term to X
X = [ones(m, 1) X];
% Choose some alpha value
alpha = 1;
num_iters = 50;
% Init Theta and Run Gradient Descent
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
% Plot the convergence graph
figure;
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
% Display gradient descent's result
fprintf('Theta computed from gradient descent: \n');
fprintf(' %f \n', theta);
fprintf('\n');
% Estimate the price of a 1650 sq-ft, 3 br house
XX = [1650 3];
XX=[ones(1, 1) (XX - mu)./sigma];
price = XX *theta ;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
'(using gradient descent):\n $%f\n'], price);
相关文章推荐
- Machine Learning(Stanford)| 斯坦福大学机器学习笔记--第一周(5.线性回归的梯度下降)
- 线性回归——Stanford Machine Learning (1)
- 【Stanford|斯坦福-机器学习:线性回归-单特征梯度下降+动态图】python3实现
- Machine Learning(Stanford)| 斯坦福大学机器学习笔记--第一周(2.线性回归,代价函数)
- stanford coursera 机器学习编程作业 exercise 5(正则化线性回归及偏差和方差)
- Stanford机器学习笔记-1.线性回归
- 机器学习之线性回归
- 线性回归---公式推导
- [ML]机器学习之线性回归
- 机器学习入门:线性回归及梯度下降
- 多特征线性回归
- 机器学习简易入门(一) - 线性回归
- Coursera2014 机器学习第一周 单变量线性回归
- 线性回归原理推导
- (一)神经网络入门之线性回归
- 线性回归-2-梯度下降
- 机器学习笔记一【线性回归】
- R语言笔记之线性回归及其衍生(续)
- 5.1 多元线性回归下的梯度下降
- 机器学习之线性回归 (Python SKLearn)