Python 08 决策树分类

Q:对数据集,分别采用信息增益和Gini指标,利用sklearn的DecisionTreeClassifier 函数构建决策树

 代码:

from matplotlib import pyplot as plt
# 特征
a1 = [1,1,1,1,1,0,0,0,1,1]
a2 = [0,1,1,0,1,0,0,0,1,0]
X=[]
for i in range(len(a1)):
    x = [a1[i],a2[i]]
    X.append(x)
# 类别
Y = [1,1,1,0,1,0,0,0,0,0]

from sklearn import tree

# clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = tree.DecisionTreeClassifier(criterion='gini')
clf = clf.fit(X,Y)

tree.plot_tree(clf,filled=True)
plt.title("Decision tree trained on all the features")
plt.show()
# 文本输出决策树
r = tree.export_text(clf)
print(r)

1、Gini指标:

(文本决策树)

|--- feature_1 <= 0.50
|   |--- feature_0 <= 0.50
|   |   |--- class: 0
|   |--- feature_0 >  0.50
|   |   |--- class: 0
|--- feature_1 >  0.50
|   |--- class: 1

 

 

 2、信息增益:

(文本决策树)

|--- feature_0 <= 0.50
|   |--- class: 0
|--- feature_0 >  0.50
|   |--- feature_1 <= 0.50
|   |   |--- class: 0
|   |--- feature_1 >  0.50
|   |   |--- class: 1

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值