决策树建树及参数调优策略实战
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
#引入数据
from sklearn.datasets.california_housing import fetch_california_housing
housing = fetch_california_housing()
print(housing.DESCR)
#导入sklearn 建树包 fit(x,y) x:训练样本 y:记录样本标签
from sklearn import tree
dtr = tree.DecisionTreeRegressor(max_depth=2)
dtr.fit(housing.data[:,[6,7]],housing.target)
可视化
#先安装Graphviz 官网下就行
dot_data = \
tree.export_graphviz(
dtr,
out_file = None,
feature_names = housing.feature_names[6:8],
filled = True,
impurity = False,
rounded = True
)
import os
os.environ["PATH"] += os.pathsep + 'C:\Program Files (x86)\Graphviz2.38\bin'
#指定图像模板
import pydotplus
graph = pydotplus.graph_from_dot_data(dot_data)
graph.get_nodes()[7].set_fillcolor("#FFF2DD")
from IPython.display import Image
Image(graph.create_png())
生成树图:
from sklearn.model_selection import train_test_split
data_train,data_test,target_train,target_test = \
train_test_split(housing.data,housing.target,test_size=0.1,random_state =42)
dtr = tree.DecisionTreeRegressor(random_state=42)
dtr.fit(data_train,target_train)
dtr.score(data_test,target_test)
from sklearn.ensemble import RandomForestRegressor
rfr = RandomForestRegressor(random_state =42)
rfr.fit(data_train,target_train)
rfr.score(data_test,target_test)
#参数调优,交叉验证
from sklearn.model_selection import GridSearchCV
tree_param_grid = {'min_samples_split':list((3,6,9)),'n_estimators':list((10,50,100))}
grid = GridSearchCV(RandomForestRegressor(),param_grid = tree_param_grid,cv = 3)
grid.fit(data_train,target_train)
grid.grid_scores_ ,grid.best_params_ ,grid.best_score_