决策树

决策树优点:

  1. 建立的模型容易可视化,容易理解
  2. 完全不受数据缩放的影响,即不需要数据预处理

分类树

构建分类树模型

from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_wine
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

数据观察

data = load_wine()
print(data.keys())
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names'])
print(data.data)
print(data.target)
print(data.target_names)
print(data.feature_names)
[[1.423e+01 1.710e+00 2.430e+00 ... 1.040e+00 3.920e+00 1.065e+03]
 [1.320e+01 1.780e+00 2.140e+00 ... 1.050e+00 3.400e+00 1.050e+03]
 [1.316e+01 2.360e+00 2.670e+00 ... 1.030e+00 3.170e+00 1.185e+03]
 ...
 [1.327e+01 4.280e+00 2.260e+00 ... 5.900e-01 1.560e+00 8.350e+02]
 [1.317e+01 2.590e+00 2.370e+00 ... 6.000e-01 1.620e+00 8.400e+02]
 [1.413e+01 4.100e+00 2.740e+00 ... 6.100e-01 1.600e+00 5.600e+02]]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
['class_0' 'class_1' 'class_2']
['alcohol', 'malic_acid', 'ash', 'alcalinity_of_ash', 'magnesium', 'total_phenols', 'flavanoids', 'nonflavanoid_phenols', 'proanthocyanins', 'color_intensity', 'hue', 'od280/od315_of_diluted_wines', 'proline']

构建分类树

tree = DecisionTreeClassifier(random_state=0)
x1,x2,y1,y2 = train_test_split(data.data,data.target,random_state=0)
tree.fit(x1,y1)
print('训练集分数:',tree.score(x1,y1))
print('测试集分数:',tree.score(x2,y2))
训练集分数: 1.0			 很明显过拟合了,文末添加一个完整的代码
测试集分数: 0.9333333333333333

查看所有数据

pd.concat([pd.DataFrame(data.data),pd.DataFrame(data.target)],axis=1)
01234567891011120
014.231.712.4315.6127.02.803.060.282.295.641.043.921065.00
113.201.782.1411.2100.02.652.760.261.284.381.053.401050.00
213.162.362.6718.6101.02.803.240.302.815.681.033.171185.00
314.371.952.5016.8113.03.853.490.242.187.800.863.451480.00
413.242.592.8721.0118.02.802.690.391.824.321.042.93735.00
.............................................
17313.715.652.4520.595.01.680.610.521.067.700.641.74740.02
17413.403.912.4823.0102.01.800.750.431.417.300.701.56750.02
17513.274.282.2620.0120.01.590.690.431.3510.200.591.56835.02
17613.172.592.3720.0120.01.650.680.531.469.300.601.62840.02
17714.134.102.7424.596.02.050.760.561.359.200.611.60560.02

178 rows × 14 columns

重要参数

不纯度:criterion
基尼系数:gini(默认),一般情况下都用这个,速度快
信息熵:entropy,数据欠拟合时用这个,速度慢

画树
import graphviz
import sklearn.tree as lcy
dot_data = lcy.export_graphviz(tree
                   ,out_file=None
                   ,feature_names=data.feature_names
                   ,class_names=data.target_names
                   ,filled=True			filled设置是否在节点上显示颜色
                   ,rounded=True)		rounded是使每个节点变成圆角
graph = graphviz.Source(dot_data)
graph

在这里插入图片描述

输出每个特征的重要性,总和为1

.feature_importances_

a  = zip(data.feature_names,tree.feature_importances_)
for i in a:
    print(i)
('alcohol', 0.0)
('malic_acid', 0.01888131743327655)
('ash', 0.0221650248129768)
('alcalinity_of_ash', 0.0)
('magnesium', 0.0)
('total_phenols', 0.0)
('flavanoids', 0.4324191914130787)
('nonflavanoid_phenols', 0.0)
('proanthocyanins', 0.0)
('color_intensity', 0.4031560026054714)
('hue', 0.0)
('od280/od315_of_diluted_wines', 0.0)
('proline', 0.1233784637351966)

预剪枝

一棵树在无任何剪枝操作下很容易无线生长,然后训练集分数为1,造成过拟合

  1. max_depth:设置最大深度,建议从3开始

  2. min_samples_leaf = N:每个节点分支后的子节点都必须包括至少N个样本,不然的话不分这个支。一般搭配max_depth使用,建议从5开始,也可以输入浮点数作为样本量的百分比来使用

  3. min_samples_split = N:每个节点必须包括N个节点才允许分支

重要属性

  1. .feature_importances_ 查看各个特征对模型的重要性

回归树

重要的特征是不能外推,,即不能在训练数据范围外进行预测

import matplotlib.pyplot as plt
import numpy as np
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split

# numpy产生随机数
rng = np.random.RandomState(1)
X = np.sort(5*rng.rand(80,1),axis=0)        #rand生成0~1之间的数
y = np.sin(X).ravel()
y[::5] += 3*(0.5 - rng.rand(16))

# 开始模型预测
tree3 = DecisionTreeRegressor(max_depth=3)
tree5 = DecisionTreeRegressor(max_depth=5)
# x1 , x2 , y1 , y2 = train_test_split(X , y , random_state=5)
tree3.fit(X , y)
tree5.fit(X ,y)

a = np.linspace(0,5,500).reshape(-1,1)
b3 = tree3.predict(a)
b5 = tree5.predict(a)

# 画出图形
# plt.plot(X , y , 'g.')
plt.scatter(X , y , c='g' , s=30 , label = "moudle" , alpha=.8)
plt.plot(a , b3 , 'r-' , label="max_depth=3" , linewidth=4)
plt.plot(a , b5 , 'b-' , label="max_depth=5" , linewidth=2)
plt.xlabel("data")
plt.ylabel("target")
plt.title("tree")
plt.legend()
plt.show()

在这里插入图片描述




tree2 = DecisionTreeClassifier(criterion='gini'
                              ,max_depth=3
                              ,min_samples_leaf=6
                              ,random_state=0)
x11,x21,y11,y21 = train_test_split(data.data,data.target,random_state=0)

tree2.fit(x1,y1)
print('训练集参数:',tree.score(x11,y11))
print('测试集参数:',tree.score(x21,y21))

dot_data = lcy.export_graphviz(tree2
                   ,out_file=None
                   ,feature_names=data.feature_names
                   ,class_names=data.target_names
                   ,filled=True
                   ,rounded=True)
graph = graphviz.Source(dot_data)
graph

训练集参数: 1.0 虽然说只有三层,但是还是能在训练集上百分之百拟合
测试集参数: 0.9333333333333333
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值