为了更好的学习,充分复习自己学习的知识,总结课内重要知识点,每次完成作业后都会更博。
英文非官方笔记
(1)代价函数
(2)梯度下降
重要参数:学习率α(太小导致收敛慢,太大导致不能收敛)和迭代次数
(1)导入数据
data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
(2)可视化(画图)
function plotData(x, y)
figure 1; 打开一个新的图片
plot(x,y,'rx','MarkerSize',10);
ylabel('profit in $10,000s');
xlabel('Population of City in $10,000')
end
plotData(X, y) %散点图
(3)代价函数及梯度下降
X = [ones(m, 1), data(:,1)]; % 对X加入一个列向量0
theta = zeros(2, 1); % 初始化拟合参数
% 梯度下降参数设置
iterations = 1500;
alpha = 0.01;
% 计算代价函数
function J = computeCost(X, y, theta)
m = length(y); % 训练样本的数量
J = 0;
h_theta_x = X*theta;
J = sum((h_theta_x - y).^2)/(2*m);
end
J = computeCost(X, y, theta);
% 运行梯度下降
m = length(y);
J_history = zeros(num_iters, 1);
theta_s = theta;
for iter = 1:num_iters
theta(1) = theta(1) - alpha/m * sum(X*theta_s -y);
theta(2) = theta(2) - alpha/m * sum((X*theta_s -y).*X(:,2));
theta_s = theta;
J_history(iter) = computeCost(X, y, theta); % 保存每次迭代时的代价函数值
end
end
theta = gradientDescent(X, y, theta, alpha, iterations);% 画线性拟合图
hold on; % 保持前一张图可视,添加
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')hold off % 在图片上不再加新东西
(4)可视化代价函数J
theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);
J_vals = zeros(length(theta0_vals), length(theta1_vals)); % 初始化0阶矩阵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);
end
end
% 在调用sueface之前对J_vals矩阵转置
J_vals = J_vals';
figure;
surf(theta0_vals, theta1_vals, J_vals) %3D绘制
xlabel('\theta_0'); ylabel('\theta_1');
% 等高线绘制
figure;
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))xlabel('\theta_0');
ylabel('\theta_1');
hold on;
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);