Multivariate Linear Regression代码-Andrew NG Machine Learning Ex3

来源:互联网 发布:淘宝账号可以注销了 编辑:程序博客网 时间:2024/05/09 13:26

大神Andrew NG课程Exercise 2作业代码,题目详见网页:


点击打开链接


matlab实现代码如下所示:

x = load('ex3x.dat');y = load('ex3y.dat');m = length(x);x = [ones(m,1), x];% Feature Scalingsigma = std(x);mu = mean(x);x(:,2) = (x(:,2) - mu(2))./ sigma(2);x(:,3) = (x(:,3) - mu(3))./ sigma(3);alpha = [0.01, 0.03, 0.1, 0.3, 1.0, 1.3]; %% Your initial learning rate %%plotstyle = {'b', 'r', 'g', 'k', 'b--', 'r--'};J = zeros(50, 1); for k = 1:6theta = zeros(size(x(1,:)))'; % initialize fitting parametersfor num_iterations = 1:50    J(num_iterations) = ((x*theta-y)'*(x*theta-y))/(2*m); %% Calculate your cost function here %%    theta = theta - alpha(k)*((x*theta-y)'*x)'/m;          %% Result of gradient descent update %%endif alpha(k)==1    thetaBest = theta;end    % now plot J% technically, the first J starts at the zero-eth iteration% but Matlab/Octave doesn't have a zero indexhold on;plot(0:49, J(1:50),char(plotstyle(k)));endxlabel('Number of iterations');ylabel('Cost J');legend('0.01', '0.03', '0.1', '0.3', '1.0', '1.3');price_grad_desc = dot(thetaBest, [1, (1650 - mu(2))/sigma(2), (3 - mu(3))/sigma(3)]);% Normal Equation% Reload Data.x = load('ex3x.dat');y = load('ex3y.dat');m = length(x);x = [ones(m,1), x];theta_normal = (x' * x)\x' * y;price_normal = dot(theta_normal, [1, 1650, 3]);


0 0
原创粉丝点击