栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

波士顿房价数据集——随机森林

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

波士顿房价数据集——随机森林

波士顿房价数据链接:https://pan.baidu.com/s/1JPrcNl1AgNCKEHCjOGyHvQ 
提取码:1234

import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import metrics  #评价函数库
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestRegressor #导入随机森林
from sklearn.model_selection import GridSearchCV  #网格搜索验证
from sklearn import tree 
import pydotplus #绘制随机森林
from IPython.display import Image,display #显示图像
%matplotlib inline  #在当前环境中显示图像
df = pd.read_csv("D:/波士顿房价预测/boston_housing_data.csv")
df.dropna(inplace=True)  #消除空值
x = df.drop(["MEDV"],axis = 1)  #x选取前13个特征
y = df["MEDV"]  #y选取房价
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state = 0)
#定义网格搜索
param_grid = {
    "n_estimators":[5,10,20,100,200],   #数值均可预设
    "max_depth":[3,5,7],
    "max_features":[0.6,0.7,0.8,1]
}
rf = RandomForestRegressor()
grid = GridSearchCV(rf,param_grid=param_grid,cv = 3) #在网格搜索前提下训练,调参助手——找到最优参数
grid.fit(x_train,y_train)   #训练
grid.best_params_  #查看最好参数

model = grid.best_estimator_  #选中最好的参数作为模型参数
model

plt.figure(figsize=(20,20))
  
estimator = model.estimators_[9]  #显示第9颗树
data = tree.export_graphviz(
    estimator,
    out_file=None,
    filled=True,
    rounded=True
)
graph = pydotplus.graph_from_dot_data(data)
graph
display(Image(graph.create_png()))

model.feature_importances_  #特征重要度分析,数值越大,影响越大

model.predict(x_test) #预测

#计算mse均分误差,开根号得均方根误差
MSE = metrics.mean_squared_error(y_test,model.predict(x_test))
MSE

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/300375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号