线性回归(机器学习练习代码1)

主程序:
fprintf('Plotting Data ...\n')
data = load('ex1data1.txt');%载入数据,数据需在在pathway里
X = data(:, 1); y = data(:, 2);%拆分x,y
m = length(y); % number of training examples
plotData(X, y);
fprintf('Program paused. Press enter to continue.\n');
pause;

X = [ones(m, 1), data(:,1)]; % Add a column of ones to x,即x0
theta = zeros(2, 1); % initialize fitting parameters
iterations = 1500;;%定义计算最佳theta的迭代次数
alpha = 0.01;;%定义计算theta的变化尺度
fprintf('\nTesting the cost function ...\n')
% compute and display initial cost
J = computeCost(X, y, theta);%计算初始的cost
fprintf('With theta = [0 ; 0]\nCost computed = %f\n', J);
fprintf('Expected cost value (approx) 32.07\n');
J = computeCost(X, y, [-1 ; 2]);;%计算theta为-1和2的cost
fprintf('\nWith theta = [-1 ; 2]\nCost computed = %f\n', J);
fprintf('Expected cost value (approx) 54.24\n');
fprintf('Program paused. Press enter to continue.\n');
pause;

fprintf('\nRunning Gradient Descent ...\n')
% run gradient descent
theta = gradientDescent(X, y, theta, alpha, iterations); ;%计算最佳theta值
hold on; % keep previous plot visible
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
% Predict values for population sizes of 35,000 and 70,000计算出了theta带入实际数据,进行估计
predict1 = [1, 3.5] *theta;
fprintf('For population = 35,000, we predict a profit of %f\n',...
    predict1*10000);
predict2 = [1, 7] * theta;
fprintf('For population = 70,000, we predict a profit of %f\n',...
    predict2*10000);

fprintf('Visualizing J(theta_0, theta_1) ...\n')
% Grid over which we will calculate J
theta0_vals = linspace(-10, 10, 100);% linspace是Matlab中的均分计算指令,用于产生x1,x2之间的N点行线性的矢量。其中x1、x2、N分别为起始值、终止值、元素个数。若默认N,默认点数为100
theta1_vals = linspace(-1, 4, 100);


% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));


% Fill out J_vals
for i = 1:length(theta0_vals)
    for j = 1:length(theta1_vals)
 t = [theta0_vals(i); theta1_vals(j)];
 J_vals(i,j) = computeCost(X, y, t);%计算每一个theta的J(cost)的值
    end
end




% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
% Surface plot
figure;%第二幅图
surf(theta0_vals, theta1_vals, J_vals)% 用surf函数画出来的图 叫surf 三维着色表面图、三维表面图、表面图
xlabel('\theta_0'); ylabel('\theta_1');

figure;%第三幅图
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))% contour函数在 MATLAB 中,该函数用于绘制矩阵的 等高线
xlabel('\theta_0'); ylabel('\theta_1');
hold on;

plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

function J = computeCost(X, y, theta)

J = 0;
J = 1 / (2 * m) * (X * theta - y)' * (X * theta  - y);

end

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

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
  theta=theta - alpha/m*X'*(X*theta-y)
    J_history(iter) = computeCost(X, y, theta);
end

end

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

function plotData(x, y)
figure; % open a new figure window
plot (x,y,'rx', 'MarkerSize', 10);
xlabel ('population');
ylabel ('revenue');

end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值