当前位置:   article > 正文

python多元线性回归--波士顿房价预测_python画出波士顿房价脊回归图

python画出波士顿房价脊回归图

数据集:http://lib.stat.cmu.edu/datasets/boston

访问这个网页可以看到里面有对每个数据集的介绍,特征(506行,13列),最后一列是房价

1:数据集导入,导包

  1. from sklearn.datasets import load_boston
  2. from sklearn.linear_model import LinearRegression # 导入线性回归类
  3. from sklearn.model_selection import train_test_split # 数据集的划分

在python这边导的包数据是特征和结果分开的

2:获取数据

  1. #获取数据
  2. boston = load_boston()
  3. print(boston)
  4. # 目标值 特征值
  5. print(boston.data.shape) # 特征(506行,13列)
  6. x,y = boston.data,boston.target #分别获取特征值和目标值

3:对数据集进行划分

  1. # 3. 数据集划分 ( 训练集 + 测试集)
  2. x_train,x_test,y_train,y_test = train_test_split(x,y,test_size= 0.2,random_state=22) # random_state 随机种子

4:模型建立,评估

  1. # 4. 模型建立
  2. lr = LinearRegression()
  3. # 用回归对象训练模型 --- y = wx+b ---》 得到w和b的值
  4. lr.fit(x_train,y_train)
  5. print("权重w:\n",lr.coef_)
  6. print('截距b:\n',lr.intercept_)
  7. # 5. 预测
  8. y_pre = lr.predict(x_test)
  9. print("真实值:\n",y_test)
  10. print("预测值:\n",y_pre)
  11. # 6.评估(数据预测的相关性)
  12. print("测试集评估:\n",lr.score(x_test,y_test))

运行结果:

我们可以使用上一篇文章的绘图进行画图,评估出来的测试集结果为 0.7657465943591131

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/842060
推荐阅读
相关标签
  

闽ICP备14008679号