1、熵:定义为信息的期望值。表示随机变量不确定性的度量。

2、信息增益:信息增益是相对于特征而言的。特征A对训练数据集D的信息增益g(D,A),定义为集合D的经验熵H(D)与特征A给定条件下D的经验条件熵H(D|A)之差,即:
信息增益:

3、信息增益率:特征A对训练数据集D的信息增益比
信息增益率:

4、GINI系数:基尼系数的性质与信息熵一样,度量随机变量的不确定度的大小
GINI系数:

5、决策树剪枝策略
预剪枝:边建立决策树边进行剪枝的操作(更实用)
后剪枝:当建立完决策树后来进行剪枝操作
Example
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets.california_housing import fetch_california_housing
# 数据集准备
housing_data = fetch_california_housing()
# 决策树模型
from sklearn import tree
dtr = tree.DecisionTreeRegressor(max_depth = 4)
dtr.fit(housing.data[:,:], housing_data .target)
#要可视化显示 需要安装 graphviz
dot_data = tree.export_graphviz(
dtr ,
out_file = None,
feature_names = housing.feature_names[0: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())
网友评论