欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

吴恩达机器学习课程ex1:Linear Regression

程序员文章站 2022-07-13 08:51:53
...

吴恩达机器学习课程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