吳恩達網課程式設計練習:
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
周志華西瓜書課後習題:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIwczX0xiRGZkRGZ0Xy9GbvNGL2EzXlpXazxCNjRkT1MWbiBHazoVdsdUZoJlMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZwpmLwEzN1ATNzkDM2ETNwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)