一.任务
决策树实战task:
1、基于iris_data.csv数据,建立决策树模型,评估模型表现
2、可视化决策树结构
3、修改min_samples_leaf参数,对比模型结果
二.实战
#加载数据
import pandas as pd
import numpy as np
data = pd.read_csv('iris_data.csv')
data.head()
#定义X,y
X = data.drop(['target','label'],axis=1)
y = data.loc[:,'label']
print(X.shape,y.shape)#打印X,y的维度
#建立决策树模型
from sklearn import tree
dc_tree = tree.DecisionTreeClassifier(criterion='entropy',min_samples_leaf=5)#决策树分裂出来的叶子最少要有5个样本,如果再往下分发现少于5个样本节点就没有必要往下分了
dc_tree.fit(X,y)
#评估模型
y_predict = dc_tree.predict(X)
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y,y_predict)
print(accuracy) #准确率97%多一点
#可视化模型结构
%matplotlib inline
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(20,20))
tree.plot_tree(dc_tree,filled='True',feature_names=['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'],class_names=['setosa','versicolor','virginica'])
#filled 背景填充色,feature_names 属性名称 class_names 分类名称
dc_tree = tree.DecisionTreeClassifier(criterion='entropy',min_samples_leaf=1)
dc_tree.fit(X,y)
fig = plt.figure(figsize=(15,15))
tree.plot_tree(dc_tree,filled='True',feature_names=['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'],class_names=['setosa','versicolor','virginica'])
#将min_samples_leaf改为1看一下模型结构图
决策树实战summary:
1、通过建立决策树,可实现对标签数据的有效分类;
2、通过修改叶子节点最少样本数对应参数min_samples_leaf,可调控制树的分支深度;
3、核心算法参考链接:https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier
数据集.
提取码:6i01