机器学习之线性回归
2018-12-09 本文已影响15人
MrWisdom
设线性回归的训练集为
其中 , , m为训练集的样本个数,p为样本特征数。
作线性回归,设其回归模型为
其中均为p+1维向量。
最小化性能指标
梯度为
其中,
Matlab代码实现为
clear ; close all; clc
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
%normalization
mu = mean(X); % mean value
sigma = std(X); % standard deviation
X_norm = (X - repmat(mu,size(X,1),1)) ./ repmat(sigma,size(X,1),1);
X = [ones(m, 1) X]; % Add intercept term to X
% Choose some alpha value
alpha = 0.01;
num_iters = 8500;
theta = zeros(3, 1);
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
theta = theta - alpha / m * X' * (X * theta - y);
J_history(iter) = computeCostMulti(X, y, theta);
end
function J = computeCostMulti(X, y, theta)
m = length(y);
J = 0;
J = sum((X * theta - y).^2) / (2*m);
end