吴恩达网课编程练习:
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
周志华西瓜书课后习题: