2.决策树算法分析
ID3算法 | 在各节点上用信息增益法作为属性选择标准,只适合离散的描述属性 |
C4.5算法 | 用信息增益率选择节点属性,可处理离散的描述属性,连续的描述属性 |
CART算法 | 有效的非参数分类和回归方法,终结点为连续变量时,为回归树,终结点为分类变量,为分类树 |
ID3基于信息熵来选择最佳测定属性,选择有最大信息增益值的属性为测试属性,样本集的划分依照测试属性进行。
#-*- coding: utf-8 -*-
import pandas as pd
inputfile = 'sales_data.xls'
data = pd.read_excel(inputfile, index_col = u'num') #导入数据
#数据是类别标签,要将它转换为数据
data[data == u'good'] = 1
data[data == u'yes'] = 1
data[data == u'high'] = 1
data[data != 1] = -1
x = data.iloc[:,:3].as_matrix().astype(int)
y = data.iloc[:,3].as_matrix().astype(int)
print data
from sklearn.tree import DecisionTreeClassifier as DTC
dtc = DTC(criterion='entropy') #建立决策树模型,基于信息熵
dtc.fit(x, y) #训练模型
#导入相关函数,可视化决策树。
from sklearn.tree import export_graphviz
x = pd.DataFrame(x)
from sklearn.externals.six import StringIO
x = pd.DataFrame(x)
with open("tree.dot", 'w') as f:
f = export_graphviz(dtc, feature_names = x.columns, out_file = f)
数据例举:
num | wealth | weekend | p_sale | num_sale |
1 | bad | yes | yes | high |
2 | bad | yes | yes | high |
3 | bad | yes | yes | high |
4 | bad | no | yes | high |
5 | bad | yes | yes | high |
6 | bad | no | yes | high |
7 | bad | yes | no | high |
8 | good | yes | yes | high |
9 | good | yes | no | high |
10 | good | yes | yes | high |