标签:oat ima hold pre 简单的 权重 style rate 结果
%% 清屏
clc;
clear all;
close all;
%% 数据预处理
data = importdata(‘studentscores.csv‘);
x = data.data(:,1);
y = data.data(:,2);
% 原始数据的散点图
figure
plot(x,y,‘o‘);
xlabel(‘Hours‘);
ylabel(‘Scores‘);
hold on;
num = length(x); % 样本数目
x = [ones(num,1),x];
%% 梯度下降法求参数
MaxIt = 1000; % 最大迭代次数
W = zeros(size(x(1,:)))‘ ; % 初始权重向量
L_rate = 0.05; % 学习率
for It=1:MaxIt
% 计算损失函数梯度
grad = (1/num).*x‘*(x*W - y);
W = W - L_rate.*grad;
L_rate = 0.05*0.95;
end
plot(x(:,2),x*W,‘-‘);
legend(‘训练集‘,‘线性回归方程‘);
hold off;
上面是简单的一元线性回归,试验结果如下:

数据:

标签:oat ima hold pre 简单的 权重 style rate 结果
原文地址:https://www.cnblogs.com/mysterygust/p/12897314.html