决策树

  • 一、介绍
  • 二、DT简单实践
  • 1、分类
  • 2、回归
  • 理论
  • 1)公式及概念
  • (1)信息增益
  • (2)信息增益比
  • (3)基尼系数
  • 2)不同的生成算法
  • 3)关于剪枝
  • 4)例子
  • python实现
  • 在scikit-learn中


一、介绍

决策树(decision tree) 是一种基本的分类与回归方法。其目的是创建一种模型从数据特征中学习简单的决策规则来预测一个目标变量的值。

决策树模型呈树形结构,在分类问题中,表示基于特征对实例进行分类的过程。它可以认为是if-then 规则的集合,也可以认为是定义在特征空间与类空间上的条件概率分布。其主要优点是模型具有可读性,分类速度快。学习时,利用训练数据,根据损失函数最小化的原则建立决策树模型。预测时,对新的数据,利用决策树模型进行分类。

python决策树返回香农熵 决策树 python sklearn_python

二、DT简单实践

1、分类

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.datasets import load_iris
from sklearn import tree

# import some data to play with
iris = load_iris()
# we only take the first two features. We could avoid this ugly
# slicing by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target

h = .02  # step size in the mesh

# Create color maps
cmap_light = ListedColormap(['orange', 'cyan', 'cornflowerblue'])
cmap_bold = ListedColormap(['darkorange', 'c', 'darkblue'])

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)

# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)

# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold,
            edgecolor='k', s=20)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.title("GaussianNB 3-Class classification")

plt.savefig("DT_clf.jpg", dpi=100)

python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_02


将决策规则保存在pdf中,查看。

import graphviz 
dot_data = tree.export_graphviz(clf, out_file=None, 
                     feature_names=iris.feature_names[:2],  
                     class_names=iris.target_names,  
                     filled=True, rounded=True,  )  
graph = graphviz.Source(dot_data)  
graph.render("DT_clf")

python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_03

2、回归

输入X是单个实数值,并且输出Y是X的正弦和余弦。

当输出值之间没有关联时,一个很简单的处理该类型的方法是建立一个n独立模型。对于决策树,这一策略可以很容易地用于多输出问题。 这需要以下更改:

  • 在叶中存储n个输出值,而不是一个;
  • 通过计算所有n个输出的平均减少量来作为分裂标准.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor

# Create a random dataset
rng = np.random.RandomState(1)
X = np.sort(200 * rng.rand(100, 1) - 100, axis=0)
y = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T
y[::5, :] += (0.5 - rng.rand(20, 2))

# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_3 = DecisionTreeRegressor(max_depth=8)
regr_1.fit(X, y)
regr_2.fit(X, y)
regr_3.fit(X, y)

# Predict
X_test = np.arange(-100.0, 100.0, 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)
y_3 = regr_3.predict(X_test)

# Plot the results
plt.figure()
s = 25
plt.scatter(y[:, 0], y[:, 1], c="navy", s=s,
            edgecolor="black", label="data")
plt.scatter(y_1[:, 0], y_1[:, 1], c="cornflowerblue", s=s,
            edgecolor="black", label="max_depth=2")
plt.scatter(y_2[:, 0], y_2[:, 1], c="red", s=s,
            edgecolor="black", label="max_depth=5")
plt.scatter(y_3[:, 0], y_3[:, 1], c="orange", s=s,
            edgecolor="black", label="max_depth=8")
plt.xlim([-6, 6])
plt.ylim([-6, 6])
plt.xlabel("target 1")
plt.ylabel("target 2")
plt.title("Multi-output Decision Tree Regression")
plt.legend(loc="best")
plt.savefig("DT_reg", dpi=100)

python决策树返回香农熵 决策树 python sklearn_机器学习_04

理论

1)公式及概念

(1)信息增益

信息增益到底用来做什么? 对于数据集 python决策树返回香农熵 决策树 python sklearn_python_05 而言,信息增益依赖于特征,不同的特征往往具有不同的信息增益。如果对于特征 python决策树返回香农熵 决策树 python sklearn_python_06的信息增益比其他信息增益大,那么特征 python决策树返回香农熵 决策树 python sklearn_python_06具有更强的分类能力,即更适合用于划分。

python决策树返回香农熵 决策树 python sklearn_python_08

  • python决策树返回香农熵 决策树 python sklearn_决策树_09
  • python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_10为共有K个类
  • python决策树返回香农熵 决策树 python sklearn_决策树_11 为属于类python决策树返回香农熵 决策树 python sklearn_python_12的样本个数
  • 特征python决策树返回香农熵 决策树 python sklearn_决策树_13python决策树返回香农熵 决策树 python sklearn_决策树_14个不同的取值,则可以根据特征python决策树返回香农熵 决策树 python sklearn_决策树_13的取值将python决策树返回香农熵 决策树 python sklearn_机器学习_16划分为python决策树返回香农熵 决策树 python sklearn_决策树_14个子集python决策树返回香农熵 决策树 python sklearn_决策树_18python决策树返回香农熵 决策树 python sklearn_算法_19 … , python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_20python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_21python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_22
  • python决策树返回香农熵 决策树 python sklearn_决策树_23 为子集python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_22中属于类python决策树返回香农熵 决策树 python sklearn_python_12的样本的集合,当然python决策树返回香农熵 决策树 python sklearn_python_26python决策树返回香农熵 决策树 python sklearn_决策树_23
(2)信息增益比

特征python决策树返回香农熵 决策树 python sklearn_python_06 对训练数据集python决策树返回香农熵 决策树 python sklearn_python_05 的信息增益比python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_30定义为其信息增益python决策树返回香农熵 决策树 python sklearn_决策树_31与训练数据集D 关于特征A 的值的熵python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_32之比,即
python决策树返回香农熵 决策树 python sklearn_决策树_33
python决策树返回香农熵 决策树 python sklearn_算法_34

(3)基尼系数

假设有K 个类,样本点属于第k 类的概率为python决策树返回香农熵 决策树 python sklearn_算法_35, 就是上文中的python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_36,则概率分布的基尼指数定义为:
python决策树返回香农熵 决策树 python sklearn_python_37

2)不同的生成算法

  • ID3 算法:使用信息增益
  • C4.5 的生成算法:使用信息增益比
  • CART(classification and regression tree) 算法:
    CART 算法由以下两步组成:
    (1)决策树生成:基于训练数据集生成决策树,生成的决策树要尽量大:
    (2) 决策树剪枝: 用验证数据集对己生成的树进行剪枝并选择最优子树
  • a、分类树:使用基尼系数
  • b、回归树:用平方误差最小化准则
  • 输入空间划分为M 个单元python决策树返回香农熵 决策树 python sklearn_机器学习_38python决策树返回香农熵 决策树 python sklearn_机器学习_39 , … , python决策树返回香农熵 决策树 python sklearn_python_40
    划分方法:
    采用启发式的方法,选择第python决策树返回香农熵 决策树 python sklearn_python_41个变量python决策树返回香农熵 决策树 python sklearn_python_42和它取的值python决策树返回香农熵 决策树 python sklearn_机器学习_43,作为切分变量和切分点,并定义两个区域。遍历变量 python决策树返回香农熵 决策树 python sklearn_python_41python决策树返回香农熵 决策树 python sklearn_python_42,对固定的切分变量python决策树返回香农熵 决策树 python sklearn_python_41扫描切分点python决策树返回香农熵 决策树 python sklearn_机器学习_43,使下式达到最小值。
    python决策树返回香农熵 决策树 python sklearn_python_48
    单元python决策树返回香农熵 决策树 python sklearn_python_49上的python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_50的最优值python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_51python决策树返回香农熵 决策树 python sklearn_python_49上的所有输入实例python决策树返回香农熵 决策树 python sklearn_python_53对应的输出python决策树返回香农熵 决策树 python sklearn_决策树_54的均值python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_55
  • 预测输出。python决策树返回香农熵 决策树 python sklearn_python_56

3)关于剪枝

剪枝(pruning)是决策树学习算法对付"过拟合"的主要手段。决策树剪枝的基本策略有"预剪枝" 和"后剪枝"。

  • 预剪枝:在决策树生成时,对于要使用的划分属性比较使用前后泛化性能(如:精度)是否提高,若未提高则禁止使用该划分属性作为决策树节点。
  • 后剪枝:先从训练集生成一棵完整决策树,再自底向上对树中的所有非叶结点进行逐一考察。

一种简单的决策树学习的剪枝算法。决策树的剪枝往往通过极小化决策树整体的损失函数(loss function) 或代价函数(cost function) 来实现。

python决策树返回香农熵 决策树 python sklearn_算法_57

python决策树返回香农熵 决策树 python sklearn_python决策树返回香农熵_58

python决策树返回香农熵 决策树 python sklearn_机器学习_59

  • 经验熵:python决策树返回香农熵 决策树 python sklearn_机器学习_60
  • 损失函数:python决策树返回香农熵 决策树 python sklearn_机器学习_61

python决策树返回香农熵 决策树 python sklearn_python_62 的叶结点个数为python决策树返回香农熵 决策树 python sklearn_机器学习_63,t 是树 python决策树返回香农熵 决策树 python sklearn_python_62 的叶结点。该叶节点有python决策树返回香农熵 决策树 python sklearn_决策树_65个样本点,其中python决策树返回香农熵 决策树 python sklearn_python_66类的样本点有python决策树返回香农熵 决策树 python sklearn_python_67个。

4)例子

python决策树返回香农熵 决策树 python sklearn_决策树_68


python决策树返回香农熵 决策树 python sklearn_算法_69

python实现

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from collections import Counter
import math
from math import log
import pprint

# 书上题目5.1
def create_data():
    datasets = [['青年', '否', '否', '一般', '否'],
               ['青年', '否', '否', '好', '否'],
               ['青年', '是', '否', '好', '是'],
               ['青年', '是', '是', '一般', '是'],
               ['青年', '否', '否', '一般', '否'],
               ['中年', '否', '否', '一般', '否'],
               ['中年', '否', '否', '好', '否'],
               ['中年', '是', '是', '好', '是'],
               ['中年', '否', '是', '非常好', '是'],
               ['中年', '否', '是', '非常好', '是'],
               ['老年', '否', '是', '非常好', '是'],
               ['老年', '否', '是', '好', '是'],
               ['老年', '是', '否', '好', '是'],
               ['老年', '是', '否', '非常好', '是'],
               ['老年', '否', '否', '一般', '否'],
               ]
    labels = [u'年龄', u'有工作', u'有自己的房子', u'信贷情况', u'类别']
    # 返回数据集和每个维度的名称
    return datasets, labels

datasets, labels = create_data()
train_data = pd.DataFrame(datasets, columns=labels)
#利用ID3算法生成决策树,例5.3
# 定义节点类 二叉树
class Node:
    def __init__(self, root=True, label=None, feature_name=None, feature=None):
        self.root = root
        self.label = label
        self.feature_name = feature_name
        self.feature = feature
        self.tree = {}
        self.result = {'label:': self.label, 'feature': self.feature, 'tree': self.tree}

    def __repr__(self):
        return '{}'.format(self.result)

    def add_node(self, val, node):
        self.tree[val] = node

    def predict(self, features):
        if self.root is True:
            return self.label
        return self.tree[features[self.feature]].predict(features)
    
class DTree:
    def __init__(self, epsilon=0.1):
        self.epsilon = epsilon
        self._tree = {}

    # 熵
    @staticmethod
    def calc_ent(datasets):
        data_length = len(datasets)
        label_count = {}
        for i in range(data_length):
            label = datasets[i][-1]
            if label not in label_count:
                label_count[label] = 0
            label_count[label] += 1
        ent = -sum([(p/data_length)*log(p/data_length, 2) for p in label_count.values()])
        return ent

    # 经验条件熵
    def cond_ent(self, datasets, axis=0):
        data_length = len(datasets)
        feature_sets = {}
        for i in range(data_length):
            feature = datasets[i][axis]
            if feature not in feature_sets:
                feature_sets[feature] = []
            feature_sets[feature].append(datasets[i])
        cond_ent = sum([(len(p)/data_length)*self.calc_ent(p) for p in feature_sets.values()])
        return cond_ent

    # 信息增益
    @staticmethod
    def info_gain(ent, cond_ent):
        return ent - cond_ent

    def info_gain_train(self, datasets):
        count = len(datasets[0]) - 1
        ent = self.calc_ent(datasets)
        best_feature = []
        for c in range(count):
            c_info_gain = self.info_gain(ent, self.cond_ent(datasets, axis=c))
            best_feature.append((c, c_info_gain))
        # 比较大小
        best_ = max(best_feature, key=lambda x: x[-1])
        return best_

    def train(self, train_data):
        """
        input:数据集D(DataFrame格式),特征集A,阈值eta
        output:决策树T
        """
        _, y_train, features = train_data.iloc[:, :-1], train_data.iloc[:, -1], train_data.columns[:-1]
        # 1,若D中实例属于同一类Ck,则T为单节点树,并将类Ck作为结点的类标记,返回T
        if len(y_train.value_counts()) == 1:
            return Node(root=True,
                        label=y_train.iloc[0])

        # 2, 若A为空,则T为单节点树,将D中实例树最大的类Ck作为该节点的类标记,返回T
        if len(features) == 0:
            return Node(root=True, label=y_train.value_counts().sort_values(ascending=False).index[0])

        # 3,计算最大信息增益 同5.1,Ag为信息增益最大的特征
        max_feature, max_info_gain = self.info_gain_train(np.array(train_data))
        max_feature_name = features[max_feature]

        # 4,Ag的信息增益小于阈值eta,则置T为单节点树,并将D中是实例数最大的类Ck作为该节点的类标记,返回T
        if max_info_gain < self.epsilon:
            return Node(root=True, label=y_train.value_counts().sort_values(ascending=False).index[0])

        # 5,构建Ag子集
        node_tree = Node(root=False, feature_name=max_feature_name, feature=max_feature)

        feature_list = train_data[max_feature_name].value_counts().index
        for f in feature_list:
            sub_train_df = train_data.loc[train_data[max_feature_name] == f].drop([max_feature_name], axis=1)

            # 6, 递归生成树
            sub_tree = self.train(sub_train_df)
            node_tree.add_node(f, sub_tree)

        # pprint.pprint(node_tree.tree)
        return node_tree

    def fit(self, train_data):
        self._tree = self.train(train_data)
        return self._tree

    def predict(self, X_test):
        return self._tree.predict(X_test)
datasets, labels = create_data()
data_df = pd.DataFrame(datasets, columns=labels)
dt = DTree()
tree = dt.fit(data_df)
{'label:': None, 'feature': 2, 'tree': {'否': {'label:': None, 'feature': 1, 'tree': {'否': {'label:': '否', 'feature': None, 'tree': {}}, '是': {'label:': '是', 'feature': None, 'tree': {}}}}, '是': {'label:': '是', 'feature': None, 'tree': {}}}}
dt.predict(['老年', '否', '否', '一般'])
'否'

在scikit-learn中

分类:DecisionTreeClassifier 回归:DecisionTreeRegressor