机器学习之线性回归(2)——代码

本文通过一组房价数据,使用线性回归模型预测房价趋势。首先对数据进行预处理,然后利用梯度下降法求解最优参数,最终绘制出拟合曲线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以一个简单的例子来实现线性回归代码。

如题:

year = [2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013];
price =[2000,2500,2900,3147,4515,4903,5365,5704,6853,7971,8561,10000,11280,12900];

year和price分别表示年份和房价,拟合一条线性回归的函数来建立年份和房价的关系。

代码如下:

clear all; 
close all; 
clc
x1=[2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013];
% x=[0,1,2,3,4,5,6,7,8,9,10,11,12,13]';
y=[2000,2500,2900,3147,4515,4903,5365,5704,6853,7971,8561,10000,11280,12900];
m = length(y); % number of training examples

% Plot the training data
figure; 
plot(x1, y, 'o');
ylabel('price')
xlabel('year')

% Gradient descent
%这里我将输入数据归一化处理的,因为直接用原数据SGD不能随机梯度下降;另外可以直接将年份减小也可行。
x=(x1-min(x1).*ones(size(x1)))./(max(x1)-min(x1));

theta = ones(size(x(:,1))+1,1); % initialize fitting parameters
MAX_ITR = 1500;
alpha = 0.002;
for num_iterations = 1:MAX_ITR    
theta1 = (theta(1,1).*x+repmat(theta(2,1),size(y))-y)*x';
b = sum((theta(1,1).*x+repmat(theta(2,1),size(y))-y));
    % Here is the actual update
    theta = theta -alpha .* [theta1;b];    
end
theta
hold on; % keep previous plot visible

plot_y = theta(1,1)*x+theta(2,1);
plot(x1, plot_y, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure

贴一下结果:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值