Linear Regression
参考网页:http://ufldl.stanford.edu/tutorial/supervised/LinearRegression/
线性回归的目标是通过一个输入预测目标值
,比如通过房子大小,房间数,离市区的远近等特征预测房子的价格。那么我们就需要找到一个函数
,对于每个训练样本
预测
,如果能够通过训练样本找到一个好的函数,那么对于测试样本,我们就可以给出一个很好的预测。
我们使用线性函数:
目标函数:
![]()
使用矩阵表示:
梯度:

矩阵表示:
实验结果:
使用作者提供的数据housing.mat
RMS training error: 4.737612
RMS testing error: 4.609681

实验主要代码:
exla_linerg.m
% %This exercise uses a data from the UCI repository: % Bache, K. & Lichman, M. (2013). UCI Machine Learning Repository % http://archive.ics.uci.edu/ml % Irvine, CA: University of California, School of Information and Computer Science. % %Data created by: % Harrison, D. and Rubinfeld, D.L. % ''Hedonic prices and the demand for clean air'' % J. Environ. Economics & Management, vol.5, 81-102, 1978. % addpath ../common addpath ../common/minFunc_2012/minFunc addpath ../common/minFunc_2012/minFunc/compiled % Load housing data from file. data = load('housing.data'); data=data'; % put examples in columns % Include a row of 1s as an additional intercept feature. data = [ ones(1,size(data,2)); data ]; % Shuffle examples. data = data(:, randperm(size(data,2))); % Split into train and test sets % The last row of 'data' is the median home price. train.X = data(1:end-1,1:400); train.y = data(end,1:400); test.X = data(1:end-1,401:end); test.y = data(end,401:end); m=size(train.X,2); n=size(train.X,1); % Initialize the coefficient vector theta to random values. theta = rand(n,1); % Run the minFunc optimizer with linear_regression.m as the objective. % % TODO: Implement the linear regression objective and gradient computations % in linear_regression.m % tic; options = struct('MaxIter', 200); theta = minFunc(@linear_regression, theta, options, train.X, train.y); fprintf('Optimization took %f seconds.\n', toc); % Run minFunc with linear_regression_vec.m as the objective. % % TODO: Implement linear regression in linear_regression_vec.m % using MATLAB's vectorization features to speed up your code. % Compare the running time for your linear_regression.m and % linear_regression_vec.m implementations. % % Uncomment the lines below to run your vectorized code. %Re-initialize parameters %theta = rand(n,1); %tic; %theta = minFunc(@linear_regression_vec, theta, options, train.X, train.y); %fprintf('Optimization took %f seconds.\n', toc); % Plot predicted prices and actual prices from training set. actual_prices = train.y; predicted_prices = theta'*train.X; % Print out root-mean-squared (RMS) training error. train_rms=sqrt(mean((predicted_prices - actual_prices).^2)); fprintf('RMS training error: %f\n', train_rms); % Print out test RMS error actual_prices = test.y; predicted_prices = theta'*test.X; test_rms=sqrt(mean((predicted_prices - actual_prices).^2)); fprintf('RMS testing error: %f\n', test_rms); % Plot predictions on test data. plot_prices=true; if (plot_prices) [actual_prices,I] = sort(actual_prices); predicted_prices=predicted_prices(I); plot(actual_prices, 'rx'); hold on; plot(predicted_prices,'bx'); legend('Actual Price', 'Predicted Price'); xlabel('House #'); ylabel('House price ($1000s)'); end
linear_regression.m
function [f,g] = linear_regression(theta, X,y) % % Arguments: % theta - A vector containing the parameter values to optimize. % X - The examples stored in a matrix. % X(i,j) is the i'th coordinate of the j'th example. % y - The target value for each example. y(j) is the target for example j. % m=size(X,2); n=size(X,1); f=0; g=zeros(size(theta)); % % TODO: Compute the linear regression objective by looping over the examples in X. % Store the objective function value in 'f'. % % TODO: Compute the gradient of the objective with respect to theta by looping over % the examples in X and adding up the gradient for each example. Store the % computed gradient in 'g'. %%% YOUR CODE HERE %%% error = theta'*X - y; f = 1/2 * sum( error(:).^2 ) ; g = X * error';

浙公网安备 33010602011771号