代码模拟线性回归解决机器学习问题

代码模拟线性回归解决机器学习问题

# 数据的导入
from sklearn.datasets import load_boston
X,y = load_boston(return_X_y = True)
print(X.shape,y.shape)
# (506, 13) (506,)
# 数据的预处理
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import torch.utils.data as Data
standardscaler = StandardScaler()
X = standardscaler.fit_transform(X)
train_x,test_x,train_y,test_y = train_test_split(X,y,test_size = 0.2)
print(train_x.shape,train_y.shape) #(404, 13) (404,)
print(test_x.shape,test_y.shape)   # (404, 13) (404,)
(102, 13) (102,)
# 数据的训练
w = np.random.rand(1,13)    # 参数
b = np.random.rand()        # 常数项
loss_all = []               # 训练误差
for i in range(30):
    pre_y = np.sum(w*train_x,axis=1 ) + b
    loss = np.sum(pow(pre_y - train_y,2))/(2 * len(train_x))              # 计算损失函数
    loss_all.append(loss)
    print('第{}次,损失是{}'.format(i+1,loss))
    w = w - 0.1 * np.dot(train_x.T,pre_y - train_y)/len(train_x)         # 参数更新
    b = b - 0.1 * np.sum(pre_y - train_y)/len(train_x)                   
    print("第{}次,系数是{}".format(i+1,w))
    print("第{}次,常系数是{}".format(i+1,b))
"""
第30次,损失是13.401357529069488
第30次,系数是[[-0.84860307  0.56084003 -0.40875167  0.9102585  -0.4299894   3.79253055
   0.18943605 -1.11629341  0.83174091 -0.66907291 -1.67139862  0.94988543
  -3.01703923]]
第30次,常系数是21.628916738586867
"""
import matplotlib.pyplot as plt
# 绘制损失图像
plt.plot(loss_all,'r-')
plt.title("Train Loss")
plt.show()

在这里插入图片描述

# 计算在测试集上的损失
pre_y = np.sum(w * test_x,axis =1) + b 
loss = np.sum(pow(test_y - pre_y,2))/len(test_x)
print("测试机上损失是:",loss)
# 测试机上损失是: 18.43885106692336
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

love2study

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值