机器学习之路: python 回归树 DecisionTreeRegressor 预测波士顿房价,,python3 学习
机器学习之路: python 回归树 DecisionTreeRegressor 预测波士顿房价,,python3 学习
python3 学习api的使用
git:https://github.com/linyi0604/MachineLearning
代码:
1 from sklearn.datasets import load_boston 2 from sklearn.cross_validation import train_test_split 3 from sklearn.preprocessing import StandardScaler 4 from sklearn.tree import DecisionTreeRegressor 5 from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error 6 import numpy as np 7 8 ‘‘‘ 9 回归树:10 严格上说 回归树不能算是回归11 叶子节点是一团训练数据的均值 不是连续 具体的预测值12 13 解决特征非线性的问题14 不要求特征标准化和统一量化15 16 容易过于复杂丧失泛化能力17 稳定性较差,细微改变会导致树结构发生重大变化18 19 ‘‘‘20 21 # 1 准备数据22 # 读取波士顿地区房价信息23 boston = load_boston()24 # 查看数据描述25 # print(boston.DESCR) # 共506条波士顿地区房价信息,每条13项数值特征描述和目标房价26 # 查看数据的差异情况27 # print("最大房价:", np.max(boston.target)) # 5028 # print("最小房价:",np.min(boston.target)) # 529 # print("平均房价:", np.mean(boston.target)) # 22.53280632411067730 31 x = boston.data32 y = boston.target33 34 # 2 分割训练数据和测试数据35 # 随机采样25%作为测试 75%作为训练36 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=33)37 38 39 # 3 训练数据和测试数据进行标准化处理40 ss_x = StandardScaler()41 x_train = ss_x.fit_transform(x_train)42 x_test = ss_x.transform(x_test)43 44 ss_y = StandardScaler()45 y_train = ss_y.fit_transform(y_train.reshape(-1, 1))46 y_test = ss_y.transform(y_test.reshape(-1, 1))47 48 # 4 使用回归树进行训练和预测49 # 初始化k近邻回归模型 使用平均回归进行预测50 dtr = DecisionTreeRegressor()51 # 训练52 dtr.fit(x_train, y_train)53 # 预测 保存预测结果54 dtr_y_predict = dtr.predict(x_test)55 56 # 5 模型评估57 print("回归树的默认评估值为:", dtr.score(x_test, y_test))58 print("平回归树的R_squared值为:", r2_score(y_test, dtr_y_predict))59 print("回归树的均方误差为:", mean_squared_error(ss_y.inverse_transform(y_test),60 ss_y.inverse_transform(dtr_y_predict)))61 print("回归树的平均绝对误差为:", mean_absolute_error(ss_y.inverse_transform(y_test),62 ss_y.inverse_transform(dtr_y_predict)))63 64 ‘‘‘65 回归树的默认评估值为: 0.706650591253343866 平回归树的R_squared值为: 0.706650591253343867 回归树的均方误差为: 22.74669291338583668 回归树的平均绝对误差为: 3.0874015748031569 ‘‘‘
机器学习之路: python 回归树 DecisionTreeRegressor 预测波士顿房价
相关内容
- python strip() 函数,pythonstrip函数,strip()用于把
- 关于Python pandas模块输出每行中间省略号问题,pythonpa
- python进阶之内置函数和语法糖触发魔法方法,python进阶
- 使用conda管理python环境,condapython环境,一、动机最近打
- Ubuntu Desktop 16.04 LTS 下成功配置Jupyter的两个python内核版
- python while 循环打印九九乘法表,pythonwhile,方向一i = 1
- python实现网页登录时的rsa加密流程,pythonrsa加密流程
- python中的作用域以及内置函数globals()-全局变量、local
- Python 文件 truncate() 方法,pythontruncate,概述Python 文
- 基于python3.6.6的scrapy环境部署+图像识别插件安装,pyt
评论关闭