天天看點

機器學習 學習打卡 D1

吳恩達網課程式設計練習:

Programming Exercise 1: Linear Regression

warmUpExercise.m

function A = warmUpExercise()
A = eye(5);
end
           

plotData.m

function plotData(x, y)
figure   % open a new figure window
plot(x, y, 'rx', 'MarkerSize', 10); 
ylabel('Profit in $10,000s');
xlabel('Population of City in 10,000s');
end
           

computeCost.m

function J = computeCost(X, y, theta)
m = length(y); % number of training examples
J = 0;
J = 1/(2*m)*sum((X*theta-y).^2);
end
           

gradientDescent.m

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
    for j=1:length(theta)
        thetanew(j) = theta(j)-alpha/m*sum((X*theta-y).*X(:,j));
    end
    theta = thetanew';
    J_history(iter) = computeCost(X, y, theta);
 end
 end


           

周志華西瓜書課後習題:

機器學習 學習打卡 D1

繼續閱讀