机器学习预测octave代码
发布时间
阅读量:
阅读量
预测是机器学习最基本的框架,可以理解机器学习的迭代方法,代价函数与最优解等等基本概念。下面介绍单变量和多变量的线性回归预测,该任务是吴恩达老师机器学习课上的第一次编程作业。如果大家对octave的用法不是很熟悉,可以去看吴老师第二章课上的讲解视频。
1.单变量房价预测
gradientDescent.m:
这个函数是一个梯度下降的训练过程,每一次减去对theta的偏导数乘以alpha除以m.
利用梯度下降求theta的公式如下:

对theta求偏导数的过程如下:、

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
theta = theta-alpha/m*X'*((X*theta)-y);
J_history(iter)=computeCost(X,y,theta);//代价函数中间变量J的计算,上面等式计算theta的过程已经算出J的偏导
% ============================================================
% Save the cost J in every iteration
end
computeCost.m 代价函数:
function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cost.
J = 1/(2*m)*sum(((X*theta)-y).^2);
% =========================================================================
end
在ex1中运行的到可视化的结果:



叉子的位置被确定为最佳位置!
基于多个因素的房价预测与单变量相同的梯度下降算法(该算法用于多因素房价预测):
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
% theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCostMulti) and gradient here.
%
theta = theta-alpha/m*X'*((X*theta)-y);
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCostMulti(X, y, theta);
end
end
归一化featureNormalize:
function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X
% FEATURENORMALIZE(X) returns a normalized version of X where
% the mean value of each feature is 0 and the standard deviation
% is 1. This is often a good preprocessing step to do when
% working with learning algorithms.
% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
% of the feature and subtract it from the dataset,
% storing the mean value in mu. Next, compute the
% standard deviation of each feature and divide
% each feature by it's standard deviation, storing
% the standard deviation in sigma.
%
% Note that X is a matrix where each column is a
% feature and each row is an example. You need
% to perform the normalization separately for
% each feature.
%
% Hint: You might find the 'mean' and 'std' functions useful.
%
mu = mean(X_norm);
sigma = std(X_norm);
X_norm(:,1) = (X_norm(:,1)-mu(1))/sigma(1);
X_norm(:,2) = (X_norm(:,2)-mu(2))/sigma(2);
% ============================================================
end
normalEqn().m:
就输入这个式子来算theta的最优解

结果:

两个实验的结果对比:

全部评论 (0)
还没有任何评论哟~
