决策树学习笔记2(Python)

一、导入数据

1、库

%matplotlib inline

import matplotlib.pyplot as plt

import pandas as pd

2、sklearn导入数据

from sklearn.datasets.california_housing import fetch_california_housing
housing = fetch_california_housing()
print(housing.DESCR)

3、建树

from sklearn import tree
dtr = tree.DecisionTreeRegressor(max_depth = 2)
dtr.fit(housing.data[:,[6,7]], housing.target)

4、可视化

# 可视化显示, 先安装graphviz
dot_data = \
    tree.export_graphviz(
        dtr, #构造的对象
        out_file = None,
        feature_names = housing.feature_names[6:8], #特征名字传进来
        filled = True,
        impurity = False,
        rounded = True
    )
# pip install pydotplus
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())

                                     

5、参数选择:

                                  

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) #test_size:测试取10% random_state:生成随机数的种子
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=5, return_train_score=True)#cv:交叉验证
grid.fit(data_train, target_train)
grid.cv_results_, grid.best_params_, grid.best_score_

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值