2-4单因子线性回归实战

线性回归实战:波士顿房价预测

https://www.biaodianfu.com/linear-regression-boston-dataset.html
任务:
基于generated_data.csv房价数据,建立线性回归模型,预测x=3.5对应的y值,评估模型表现
https://www.bilibili.com/video/BV1nHK5e2Emc?spm_id_from=333.788.videopod.episodes&vd_source=3b0b5e17378c3a0cad28da42df63bae9&p=10
数据:https://github.com/fbozhang/python/blob/master/jupyter/generated_data.csv

#load the data
import pandas as pd
data = pd.read_csv('generated_data.csv')
data.head()
#data赋值
x = data.loc[:,'x']
y = data.loc[:,'y']
print(x, y)
#visualize the data
from matplotlib import pyplot as plt
plt.figure(figsize=(10,10))
plt.scatter(x,y)
plt.show()
#set up a linear regression model
from sklearn.linear_model import LinearRegression
lr_model = LinearRegression()
print(x)
print(type(x),x.shape)
import numpy as np
x = np.array(x)
x = x.reshape(-1,1)
y = np.array(y)
y = y.reshape(-1,1)
print(x)
print(type(x),x.shape)
print(type(y),y.shape)

lr_model.fit(x,y)
y_predict = lr_model.predict(x)
print(y_predict)
print(y)
y_3 = lr_model.predict([[3.5]])
print(y_3)
# a\b 打印
a = lr_model.coef_
b = lr_model.intercept_
print(a,b)
from sklearn.metrics import mean_squared_error,r2_score
MSE = mean_squared_error(y,y_predict)
R2 = r2_score(y,y_predict)
print(MSE,R2)
plt.figure()
plt.scatter(y,y_predict)
plt.show()

MSE越小越好,R2分数越接近1越好
y’ vs y 集中度越高越好(越接近直线分布)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值