首页 > 人工智能 > > 机器学习之多变量线性回归
机器学习之多变量线性回归
简述
多变量指的是输入特征有多个。
根据吴恩达老师的机器学习课程推荐,机器学习研究最好使用octave/matlab作为工具,效率更高,方便移植。
本文使用octave进行学习。
代价函数
computeCostMulti.m 用来计算多个输入特征时的代价函数:
function J = computeCostMulti (X, y, theta)
m = length(y);
J = 0;
J = sum((X * theta - y).^2) / (2*m);
endfunction
梯度下降
gradientDescentMulti.m 梯度下降函数:
和单变量是不一样的,但是也适用于单变量。以下做梯度下降的同时用J_history记录了每次代价函数J(θ)的值。
function [theta, J_history] = gradientDescentMulti (X, y, theta, alpha, num_iters)
m = length(y);
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
endfunction
特征值缩放
featureNormalize.m 为了优化梯度下降的收敛速度,采用特征缩放的技巧,使各特征值的范围尽量一致。
function [X_norm, mu, sigma] = featureNormalize (X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
mu = mean(X);
sigma = std(X);
X_norm = (X - repmat(mu, size(X, 1), 1)) ./ repmat(sigma, size(X, 1), 1);
endfunction
正规方程
normalEqn.m 对于一些线性回归问题来说,正规方程法给出了一个更好的解决问题的方式,但只适用线性模型,且矩阵需可逆:
function [theta] = normalEqn (X, y)
theta = zeros(size(X, 2), 1);
theta = inv(X' * X) * X' * y;
endfunction
可视化
综合上述的函数,对数据进行可视化操作。
1. 加载数据并把数据进行特征缩放处理,获取数据ex1data2.txt (https://github.com/peedeep/Coursera/blob/master/ex1/ex1data2.txt)进入网址下载。
%% Exercise 1: Linear regression with multiple variables
%% ================ 1.Feature Normalization ================
clear; close all; clc
fprintf('Loading data ...\n');
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
fprintf('First 10 examples from the dataset: \n');
fprintf(' x = [%.0f %.0f], y = %0.f \n', [X(1:10, :), y(1:10,:)]');
fprintf('Program paused. Press enter to continue.\n');
pause;
fprintf('Normalizing Features ...\n');
[X mu sigma] = featureNormalize(X);
X = [ones(m, 1), X];
2. 调用方法进行梯度下降,并画出随着迭代次数的变化J(θ)的值
%% ================ 2.Gradient Descent ================
fprintf('Running gradient descent...\n');
alpha = 0.01;
num_iters = 8500;
theta = zeros(3, 1);
[theta J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
figure;
plot(1: numel(J_history), J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');
fprintf('theta from gradient descent\n');
fprintf('theta: %f\n', theta);
fprintf('\n');
price = [1 (([1650 3] - mu) ./ sigma)] * theta;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house(using gradient descent):\n $%f\n'], price);
fprintf('Program paused. Press enter to continue.\n');
pause;
3. 用三维图演示出训练数据真实值与预测值
使用正规方程求解最优值
%% ================ 3.Normal Equations ================
data = csvread('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);
X = [ones(m, 1), X];
theta = normalEqn(X, y);
fprintf('Theta computed from the normal equations: \n');
fprintf(' %f \n', theta);
fprintf('\n');
price = [1 1650 3] * theta;
fprintf(['Predicted price of a 1650 sq-ft, 3 br house(using normal equations):\n $%f\n'], price);
结果使用梯度下降得到的 θ与正规方程得到的 θ进行预测的时候结果是一样的
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2318/viewspace-3145140/ ,如需转载,请注明出处,否则将追究法律责任。