吳恩達機器學習練習1——單變數線性迴歸
阿新 • • 發佈:2019-01-31
機器學習練習1——單變數線性迴歸
單變數線性迴歸
代價函式:
梯度下降
練習1
資料集
X代表poplation,y代表profits
資料集的視覺化
function plotData(x, y) figure; data = load('ex1data1.txt'); x = data(:,1); y = data(:,2); plot(x,y,'rx','MarkerSize',10); xlabel('Population of a city in 10,000s'); ylabel('Profit in &10,000s'); end
代價函式
%代價函式
%computeCost.m
function J = computeCost(X, y, theta)
m = length(y);
J = 0;
h = X*theta;
J = sum(h-y).^2/(2*m);
end
梯度下降法
%梯度下降 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=X*theta; t1 = theta(1) - alpha*(1/m)*sum(h-y); t2 = theta(2) - alpha*(1/m)*sum((h-y).*X(:,2)); theta = [t1;t2]; J_history(iter) = computeCostMulti(X, y, theta); end end