吴恩达机器学习课程ex1:Linear Regression
matlab实现
# computeCost.m
function J = computeCost(X, y, theta)
m = length(y);
J = 0;
h_func = X * theta;
J = (1 / (2 * m)) * sum((h_func - y).^2);
end
# gradientDescent.m
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y);
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
h_func = X * theta;
theta = theta - 0.01 * (X') * (1/m) * (h_func - y);
J_history(iter) = computeCost(X, y, theta);
end
end