2、 Linear Regression 线性回归 [Stanford - Machine Learning]

来源:互联网 发布:python写网络爬虫 编辑:程序博客网 时间:2024/05/10 22:33
线性回归就是在(features+1)维空间中通过梯度下降的方法找到一条直线,使这条直线尽量匹配所有的点(每一个点是一个样本,当然可以自己构建样本如:长*宽=面积)


线性回归的知识点:(图片看不清可以在新标签打开或者保存)

·


线性回归如何编程实现:(Matlab)

1、Cost Function函数:可以用来观察每次的Cost是否下降(把每一次梯度下降递归的结果保存起来,作图就可以观察到了),便于选择合适的训练速率alpha

function J = computeCost(X, y, theta)m = length(y); % number of training examplesJ=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 examplesJ_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);endend

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 Datadata = 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 XX = [ones(m, 1) X];% Choose some alpha valuealpha = 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 graphfigure;plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);xlabel('Number of iterations');ylabel('Cost J');% Display gradient descent's resultfprintf('Theta computed from gradient descent: \n');fprintf(' %f \n', theta);fprintf('\n');% Estimate the price of a 1650 sq-ft, 3 br houseXX = [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);

0 0