#1. 导入相关包

import numpy as np #导入numpy科学计算包
import pandas as pd #导入pandas数据分析包
from pandas import Series, DataFrame    #Series是类似于一维数组的对象
import matplotlib.pyplot as plt #导入绘图的包
import sklearn.datasets as datasets #直接从sklearn的datasets API中导入数据集,例如scikit-learn 内置有一些小型标准数据集,不需要从某个外部网站下载任何文件,用datasets.load_xx()加载。

from sklearn.model_selection import train_test_split  # train_test_split是交叉验证中常用的函数,功能是从样本中随机的按比例选取train data和test data。

from sklearn.metrics import r2_score  #导入评判标准,计算预测值和真实值的拟合程度
# 机器算法模型

{'data': array([[6.3200e-03, 1.8000e+01, 2.3100e+00, ..., 1.5300e+01, 3.9690e+02,4.9800e+00],[2.7310e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9690e+02,9.1400e+00],[2.7290e-02, 0.0000e+00, 7.0700e+00, ..., 1.7800e+01, 3.9283e+02,4.0300e+00],...,[6.0760e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,5.6400e+00],[1.0959e-01, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9345e+02,6.4800e+00],[4.7410e-02, 0.0000e+00, 1.1930e+01, ..., 2.1000e+01, 3.9690e+02,7.8800e+00]]),'target': array([24. , 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5, 18.9, 15. ,18.9, 21.7, 20.4, 18.2, 19.9, 23.1, 17.5, 20.2, 18.2, 13.6, 19.6,15.2, 14.5, 15.6, 13.9, 16.6, 14.8, 18.4, 21. , 12.7, 14.5, 13.2,13.1, 13.5, 18.9, 20. , 21. , 24.7, 30.8, 34.9, 26.6, 25.3, 24.7,21.2, 19.3, 20. , 16.6, 14.4, 19.4, 19.7, 20.5, 25. , 23.4, 18.9,35.4, 24.7, 31.6, 23.3, 19.6, 18.7, 16. , 22.2, 25. , 33. , 23.5,19.4, 22. , 17.4, 20.9, 24.2, 21.7, 22.8, 23.4, 24.1, 21.4, 20. ,20.8, 21.2, 20.3, 28. , 23.9, 24.8, 22.9, 23.9, 26.6, 22.5, 22.2,23.6, 28.7, 22.6, 22. , 22.9, 25. , 20.6, 28.4, 21.4, 38.7, 43.8,33.2, 27.5, 26.5, 18.6, 19.3, 20.1, 19.5, 19.5, 20.4, 19.8, 19.4,21.7, 22.8, 18.8, 18.7, 18.5, 18.3, 21.2, 19.2, 20.4, 19.3, 22. ,20.3, 20.5, 17.3, 18.8, 21.4, 15.7, 16.2, 18. , 14.3, 19.2, 19.6,23. , 18.4, 15.6, 18.1, 17.4, 17.1, 13.3, 17.8, 14. , 14.4, 13.4,15.6, 11.8, 13.8, 15.6, 14.6, 17.8, 15.4, 21.5, 19.6, 15.3, 19.4,17. , 15.6, 13.1, 41.3, 24.3, 23.3, 27. , 50. , 50. , 50. , 22.7,25. , 50. , 23.8, 23.8, 22.3, 17.4, 19.1, 23.1, 23.6, 22.6, 29.4,23.2, 24.6, 29.9, 37.2, 39.8, 36.2, 37.9, 32.5, 26.4, 29.6, 50. ,32. , 29.8, 34.9, 37. , 30.5, 36.4, 31.1, 29.1, 50. , 33.3, 30.3,34.6, 34.9, 32.9, 24.1, 42.3, 48.5, 50. , 22.6, 24.4, 22.5, 24.4,20. , 21.7, 19.3, 22.4, 28.1, 23.7, 25. , 23.3, 28.7, 21.5, 23. ,26.7, 21.7, 27.5, 30.1, 44.8, 50. , 37.6, 31.6, 46.7, 31.5, 24.3,31.7, 41.7, 48.3, 29. , 24. , 25.1, 31.5, 23.7, 23.3, 22. , 20.1,22.2, 23.7, 17.6, 18.5, 24.3, 20.5, 24.5, 26.2, 24.4, 24.8, 29.6,42.8, 21.9, 20.9, 44. , 50. , 36. , 30.1, 33.8, 43.1, 48.8, 31. ,36.5, 22.8, 30.7, 50. , 43.5, 20.7, 21.1, 25.2, 24.4, 35.2, 32.4,32. , 33.2, 33.1, 29.1, 35.1, 45.4, 35.4, 46. , 50. , 32.2, 22. ,20.1, 23.2, 22.3, 24.8, 28.5, 37.3, 27.9, 23.9, 21.7, 28.6, 27.1,20.3, 22.5, 29. , 24.8, 22. , 26.4, 33.1, 36.1, 28.4, 33.4, 28.2,22.8, 20.3, 16.1, 22.1, 19.4, 21.6, 23.8, 16.2, 17.8, 19.8, 23.1,21. , 23.8, 23.1, 20.4, 18.5, 25. , 24.6, 23. , 22.2, 19.3, 22.6,19.8, 17.1, 19.4, 22.2, 20.7, 21.1, 19.5, 18.5, 20.6, 19. , 18.7,32.7, 16.5, 23.9, 31.2, 17.5, 17.2, 23.1, 24.5, 26.6, 22.9, 24.1,18.6, 30.1, 18.2, 20.6, 17.8, 21.7, 22.7, 22.6, 25. , 19.9, 20.8,16.8, 21.9, 27.5, 21.9, 23.1, 50. , 50. , 50. , 50. , 50. , 13.8,13.8, 15. , 13.9, 13.3, 13.1, 10.2, 10.4, 10.9, 11.3, 12.3,  8.8,7.2, 10.5,  7.4, 10.2, 11.5, 15.1, 23.2,  9.7, 13.8, 12.7, 13.1,12.5,  8.5,  5. ,  6.3,  5.6,  7.2, 12.1,  8.3,  8.5,  5. , 11.9,27.9, 17.2, 27.5, 15. , 17.2, 17.9, 16.3,  7. ,  7.2,  7.5, 10.4,8.8,  8.4, 16.7, 14.2, 20.8, 13.4, 11.7,  8.3, 10.2, 10.9, 11. ,9.5, 14.5, 14.1, 16.1, 14.3, 11.7, 13.4,  9.6,  8.7,  8.4, 12.8,10.5, 17.1, 18.4, 15.4, 10.8, 11.8, 14.9, 12.6, 14.1, 13. , 13.4,15.2, 16.1, 17.8, 14.9, 14.1, 12.7, 13.5, 14.9, 20. , 16.4, 17.7,19.5, 20.2, 21.4, 19.9, 19. , 19.1, 19.1, 20.1, 19.9, 19.6, 23.2,29.8, 13.8, 13.3, 16.7, 12. , 14.6, 21.4, 23. , 23.7, 25. , 21.8,20.6, 21.2, 19.1, 20.6, 15.2,  7. ,  8.1, 13.6, 20.1, 21.8, 24.5,23.1, 19.7, 18.3, 21.2, 17.5, 16.8, 22.4, 20.6, 23.9, 22. , 11.9]),'feature_names': array(['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD','TAX', 'PTRATIO', 'B', 'LSTAT'], dtype='<U7'),'DESCR': ".. _boston_dataset:\n\nBoston house prices dataset\n---------------------------\n\n**Data Set Characteristics:**  \n\n    :Number of Instances: 506 \n\n    :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.\n\n    :Attribute Information (in order):\n        - CRIM     per capita crime rate by town\n        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.\n        - INDUS    proportion of non-retail business acres per town\n        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)\n        - NOX      nitric oxides concentration (parts per 10 million)\n        - RM       average number of rooms per dwelling\n        - AGE      proportion of owner-occupied units built prior to 1940\n        - DIS      weighted distances to five Boston employment centres\n        - RAD      index of accessibility to radial highways\n        - TAX      full-value property-tax rate per $10,000\n        - PTRATIO  pupil-teacher ratio by town\n        - B        1000(Bk - 0.63)^2 where Bk is the proportion of black people by town\n        - LSTAT    % lower status of the population\n        - MEDV     Median value of owner-occupied homes in $1000's\n\n    :Missing Attribute Values: None\n\n    :Creator: Harrison, D. and Rubinfeld, D.L.\n\nThis is a copy of UCI ML housing dataset.\nhttps://archive.ics.uci.edu/ml/machine-learning-databases/housing/\n\n\nThis dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.\n\nThe Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic\nprices and the demand for clean air', J. Environ. Economics & Management,\nvol.5, 81-102, 1978.   Used in Belsley, Kuh & Welsch, 'Regression diagnostics\n...', Wiley, 1980.   N.B. Various transformations are used in the table on\npages 244-261 of the latter.\n\nThe Boston house-price data has been used in many machine learning papers that address regression\nproblems.   \n     \n.. topic:: References\n\n   - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.\n   - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.\n",'filename': 'boston_house_prices.csv','data_module': 'sklearn.datasets.data'}

#2. 读取数据boston = datasets.load_boston()
data_name = boston['feature_names']
data_1 = boston['data'][:5]
train = boston.data # 样本
# print(train.shape[0])                #输出为506
target = boston.target # 标签
# print(target.shape[0])                #输出为506
# 切割数据样本集合测试集
X_train, x_test, y_train, y_true = train_test_split(train, target, test_size=0.2)
#参数:所要划分的样本特征集;所要划分的样本结果;
# 20%测试集;80%训练集

#波士顿房价预测----Lasso
from sklearn.linear_model import Lasso # 线性回归算法Lasso回归,可用作特征筛选
# 模型训练
lasso = Lasso() #实例化lasso模型
lasso.fit(X_train, y_train)  # 模型训练
# 预测数据
y_pre_lasso = lasso.predict(x_test)

#R^2 score,即决定系数,反映因变量的全部变异能通过回归关系被自变量解释的比例。计算公式:R^2=1-\frac{SSE}{SST}
lasso_score = r2_score(y_true, y_pre_lasso)

print('w = ', lasso.coef_) # w值
print('b = ', lasso.intercept_) # b值
print("训练集得分:{:.2f}".format(lasso.score(X_train, y_train)))
print("测试集得分:{:.2f}".format(lasso.score(x_test, y_true)))
# 绘图
# Lasso
plt.plot(y_true, label='true')
plt.plot(y_pre_lasso,'r:', label='lasso')
plt.legend()  #使用plt.legend()使上述plt.plot()代码产生效果
plt.show()
# plt.savefig('F:/新桌面/pred_GT.jpg')

#波士顿房价预测----Ridge Regression

#1.模型导入
#注意:其他包的导入如Lasso回归
from sklearn.linear_model import Ridge # 线性回归算法Ridge回归,岭回归
ridge = Ridge(alpha=1,max_iter=1000) # 模型实例化
#alpha:正则化力度,必须是一个正浮点数。正则化提升了问题的条件,减少了估计器的方差。
#max_iter:共轭梯度求解器的最大迭代次数。

ridge.fit(X_train, y_train) # 模型训练
# 模型预测
y_pre_ridge = ridge.predict(x_test)

# print('预测结果:', y_pre_ridge)
print('w = ', ridge.coef_) # w值
print('b = ', ridge.intercept_) # b值
print("训练集得分:{:.2f}".format(ridge.score(X_train, y_train)))
print("测试集得分:{:.2f}".format(ridge.score(x_test, y_true)))

ridge_score = r2_score(y_true, y_pre_ridge)   #决定系数,反映因变量的全部变异能通过回归关系被自变量解释的比例
#绘图
plt.plot(y_true, label='true')
plt.plot(y_pre_ridge,'r:', label='ridge')
plt.legend()
plt.show()
# plt.savefig(''F:/新桌面/回归曲线.jpg')

# 波士顿房价预测----Multi LinearRegression

#1.模型导入
#注意:其他包的导入如Lasso回归
from sklearn.linear_model import LinearRegression # 多元线性回归算法
linear= LinearRegression()
linear.fit(X_train, y_train)
# 预测数据
y_pre_linear = linear.predict(x_test)
print('预测结果:', y_pre_linear)
print('w = ', linear.coef_) # w值
print('b = ', linear.intercept_) # b值
print("训练集得分:{:.2f}".format(linear.score(X_train, y_train)))
print("测试集得分:{:.2f}".format(linear.score(x_test, y_true)))
linear_score = r2_score(y_true, y_pre_linear)
# 绘图
plt.plot(y_true, label='true')
plt.plot(y_pre_linear,'r:', label='linear', )
plt.legend()
plt.show()
# plt.savefig(''F:/新桌面/第三个曲线.jpg')

python做波士顿房价预测相关推荐

  1. 手把手教你学Python之波士顿房价预测(scikit-learn的应用)

    目录 1.波士顿房价预测介绍 2.线性回归算法 3.调用scikit-learn库实现房价预测 1.波士顿房价预测介绍 问题描述:波士顿房价数据集统计的是20世纪70年代中期波士顿郊区房价的中位数,统 ...

  2. 用Python做一个房价预测小工具!

    哈喽,大家好. 今天给大家介绍一个非常适合新手入门的机器学习实战案例. 这是一个房价预测的案例,来源于 Kaggle 网站,是很多算法初学者的第一道竞赛题目. 该案例有着解机器学习问题的完整流程,包含 ...

  3. 用Python做一个房价预测小工具

    哈喽,大家好. 今天给大家介绍一个非常适合新手入门的机器学习实战案例. 这是一个房价预测的案例,来源于 Kaggle 网站,是很多算法初学者的第一道竞赛题目. 该案例有着解机器学习问题的完整流程,包含 ...

  4. 使用Python和Numpy进行波士顿房价预测任务(二)【深度学习入门_学习笔记】

    使用Python和Numpy进行波士顿房价预测任务–详解 目录: 一.数据处理 (1)读入数据 (2)数据形状变换 (3)数据集划分 (4)数据归一化处理 (5)封装成load data函数 二.模型 ...

  5. 波士顿房价预测python决策树_波士顿房价预测 - 最简单入门机器学习 - Jupyter

    机器学习入门项目分享 - 波士顿房价预测 该分享源于Udacity机器学习进阶中的一个mini作业项目,用于入门非常合适,刨除了繁琐的部分,保留了最关键.基本的步骤,能够对机器学习基本流程有一个最清晰 ...

  6. Python机器学习/数据挖掘项目实战 波士顿房价预测 回归分析

    Python机器学习/数据挖掘项目实战 波士顿房价预测 回归分析 此数据源于美国某经济学杂志上,分析研究波士顿房价( Boston HousePrice)的数据集. 在这个项目中,你将利用马萨诸塞州波 ...

  7. 使用Python和Numpy进行波士顿房价预测任务(一)【深度学习入门_学习笔记】

    波士顿房价预测是一个经典的机器学习任务,类似于程序员世界的"Hello World".和大家对房价的普遍认知相同,波士顿地区的房价是由诸多因素影响的. 下载数据集:https:// ...

  8. 波士顿房价预测python代码_Python之机器学习-波斯顿房价预测

    AI 人工智能 Python之机器学习-波斯顿房价预测 波士顿房价预测 导入模块 import pandas as pd import numpy as np import matplotlib.py ...

  9. 波士顿房价预测python代码_使用Python和Numpy构建神经网络模型——波士顿房价预测案例...

    原标题:使用Python和Numpy构建神经网络模型--波士顿房价预测案例

  10. python模型预测_【超级干货!】教你用Python做回归模型预测房价

    原标题:[超级干货!]教你用Python做回归模型预测房价 欢迎关注天善智能 hellobi.com,我们是专注于商业智能BI,大数据,数据分析领域的垂直社区,学习.问答.求职,一站式搞定! 对商业智 ...

最新文章

  1. mysql驱动(github上的)
  2. 【Java】泛型中 extends 和 super 的区别?
  3. 拼车日滴滴派单的那些事
  4. sap.ushell.Container.getService.done的设计思路
  5. Java提高篇 —— Java三大特性之继承
  6. PHP中一些有用的函数
  7. ffmpeg播放器 android,Android使用FFmpeg(六)--ffmpeg实现音视频同步播放
  8. JavaScript中的字面量
  9. WindowsServer2008R2系统版本升级
  10. 火狐中国区linux无法同步,教大家火狐浏览器同步数据失败的几点原因
  11. c++和opencv小知识:openCV中convertTo的用法
  12. python-回调函数
  13. 【滤波跟踪】基于多源信息融合算法实现多旋翼无人机组合导航系统含Matlab源码
  14. python统计人的视角_Python--统计人的视角
  15. ROS编译ORB-SLAM2或其各种变种的算法遇到的编译问题
  16. 2021-07-07随笔
  17. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java小微企业ERP软件物料需求模块设计55ss9
  18. 在线文本替换工具 、支持正则表达式(博客园文章里添加Javascript或<script>语句)
  19. EasyUI之Linkbutton按钮
  20. opus 源码下载 以及 相关资料

热门文章

  1. 00 后搞视频号月入过万,怎么做?
  2. 2018年下半年各省软考报名时间及网址
  3. 代挂php源码,[PHP源码]开心QQ等级代挂源码,无后门任意搭建分站
  4. 打造私人搜书系统之系统设计
  5. 计蒜课:Fantastic Graph(图论)(有源汇的上下界的可行流)
  6. 传奇开服教程:传奇添加地图花屏原因与解决方法
  7. 目标决定人生——没有目标就失去一切 (转载)
  8. spring-security实现权限管理
  9. java 生成临时目录_如何在Java中创建临时目录/文件夹?
  10. 《剑指offer》读后感