决策树分类算法的案例(代码实现及运行测试)

1 案例需求

我们的任务就是训练一个决策树分类器,输入身高和体重,分类器能给出这个人是胖子还是瘦子。

所用的训练数据如下,这个数据一共有10个样本,每个样本有2个属性,分别为身高和体重,第三列为类别标签,表示“胖”或“瘦”。该数据保存在1.txt中。

1.5 50 thin

1.5 60 fat

1.6 40 thin

1.6 60 fat

1.7 60 thin

1.7 80 fat

1.8 60 thin

1.8 90 fat

1.9 70 thin

1.9 80 fat

2 模型分析

决策树对于“是非”的二值逻辑的分枝相当自然。而在本数据集中,身高与体重是连续值怎么办呢?

虽然麻烦一点,不过这也不是问题,只需要找到将这些连续值划分为不同区间的中间点,就转换成了二值逻辑问题。

本例决策树的任务是找到身高、体重中的一些临界值,按照大于或者小于这些临界值的逻辑将其样本两两分类,自顶向下构建决策树。

3 python实现

使用python的机器学习库,实现起来相当简单和优雅

# -*- coding: utf-8 -*-

import numpy as np

import scipy as sp

from sklearn import tree

from sklearn.metrics import precision_recall_curve

from sklearn.metrics import classification_report

from sklearn.cross_validation import train_test_split

''' 数据读入 '''

data   = []

labels = []

with open("d:\\python\\ml\\data\\1.txt") as ifile:

    for line in ifile:

        tokens = line.strip().split(' ')

        data.append([float(tk) for tk in tokens[:-1]])

        labels.append(tokens[-1])

x = np.array(data)

labels = np.array(labels)

y = np.zeros(labels.shape)

''' 标签转换为0/1 '''

y[labels=='fat']=1

''' 拆分训练数据与测试数据 '''

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)

''' 使用信息熵作为划分标准,对决策树进行训练 '''

clf = tree.DecisionTreeClassifier(criterion='entropy')

print(clf)

clf.fit(x_train, y_train)

''' 把决策树结构写入文件 '''

with open("tree.dot", 'w') as f:

      f = tree.export_graphviz(clf, out_file=f)

''' 系数反映每个特征的影响力。越大表示该特征在分类中起到的作用越大 '''

print(clf.feature_importances_)

'''测试结果的打印'''

answer = clf.predict(x_train)

print(x_train)

print(answer)

print(y_train)

print(np.mean( answer == y_train))

'''准确率与召回率'''

precision, recall, thresholds = precision_recall_curve(y_train, clf.predict(x_train))

answer = clf.predict_proba(x)[:,1]

print(classification_report(y, answer, target_names = ['thin', 'fat']))

这时候会输出

[ 0.2488562  0.7511438]

array([[  1.6,  60. ],

       [  1.7,  60. ],

       [  1.9,  80. ],

       [  1.5,  50. ],

       [  1.6,  40. ],

       [  1.7,  80. ],

       [  1.8,  90. ],

       [  1.5,  60. ]])

array([ 1.,  0.,  1.,  0.,  0.,  1.,  1.,  1.])

array([ 1.,  0.,  1.,  0.,  0.,  1.,  1.,  1.])

1.0

             precision    recall  f1-score   support

       thin       0.83      1.00      0.91         5

        fat        1.00      0.80      0.89         5

avg / total       1.00      1.00      1.00         8

array([ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.,  0.,  0.])

array([ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.])

可以看到,对训练过的数据做测试,准确率是100%。但是最后将所有数据进行测试,会出现1个测试样本分类错误。

说明本例的决策树对训练集的规则吸收的很好,但是预测性稍微差点。

4 决策树的保存

一棵决策树的学习训练是非常耗费运算时间的,因此,决策树训练出来后,可进行保存,以便在预测新数据时只需要直接加载训练好的决策树即可

本案例的代码中已经决策树的结构写入了tree.dot中。打开该文件,很容易画出决策树,还可以看到决策树的更多分类信息。

本例的tree.dot如下所示:

digraph Tree {

0 [label="X[1] <= 55.0000\nentropy = 0.954434002925\nsamples = 8", shape="box"] ;

1 [label="entropy = 0.0000\nsamples = 2\nvalue = [ 2.  0.]", shape="box"] ;

0 -> 1 ;

2 [label="X[1] <= 70.0000\nentropy = 0.650022421648\nsamples = 6", shape="box"] ;

0 -> 2 ;

3 [label="X[0] <= 1.6500\nentropy = 0.918295834054\nsamples = 3", shape="box"] ;

2 -> 3 ;

4 [label="entropy = 0.0000\nsamples = 2\nvalue = [ 0.  2.]", shape="box"] ;

3 -> 4 ;

5 [label="entropy = 0.0000\nsamples = 1\nvalue = [ 1.  0.]", shape="box"] ;

3 -> 5 ;

6 [label="entropy = 0.0000\nsamples = 3\nvalue = [ 0.  3.]", shape="box"] ;

2 -> 6 ;

}

根据这个信息,决策树应该长的如下这个样子:

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
决策树分类算法的Python代码实例可以通过以下步骤实现: 1. 构建数据集:将样本数据转换为可用于决策树分类算法的格式。 2. 计算数据集信息熵:使用信息熵来衡量数据集的混乱程度。 3. 计算信息增益:通过计算每个属性的信息增益来确定最佳的分裂属性。 4. 构造决策树:使用递归的方式构造决策树。 5. 实例化构造决策树:使用构造好的决策树对新的数据进行分类。 以下是一个简单的决策树分类算法的Python代码实例: ``` # 导入必要的库 import pandas as pd import numpy as np # 构建数据集 data = {'Outlook': ['Sunny', 'Sunny', 'Overcast', 'Rainy', 'Rainy', 'Rainy', 'Overcast', 'Sunny', 'Sunny', 'Rainy', 'Sunny', 'Overcast', 'Overcast', 'Rainy'], 'Temperature': ['Hot', 'Hot', 'Hot', 'Mild', 'Cool', 'Cool', 'Cool', 'Mild', 'Cool', 'Mild', 'Mild', 'Mild', 'Hot', 'Mild'], 'Humidity': ['High', 'High', 'High', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'Normal', 'Normal', 'High', 'Normal', 'High'], 'Wind': ['Weak', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Weak', 'Weak', 'Strong', 'Strong', 'Weak', 'Strong'], 'Play': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']} df = pd.DataFrame(data) # 计算数据集信息熵 def entropy(target_col): elements, counts = np.unique(target_col, return_counts=True) entropy = np.sum([(-counts[i]/np.sum(counts)) * np.log2(counts[i]/np.sum(counts)) for i in range(len(elements))]) return entropy # 计算信息增益 def InfoGain(data, split_attribute_name, target_name="Play"): total_entropy = entropy(data[target_name]) vals, counts = np.unique(data[split_attribute_name], return_counts=True) Weighted_Entropy = np.sum([(counts[i]/np.sum(counts)) * entropy(data.where(data[split_attribute_name]==vals[i]).dropna()[target_name]) for i in range(len(vals))]) Information_Gain = total_entropy - Weighted_Entropy return Information_Gain # 构造决策树 def ID3(data, originaldata, features, target_attribute_name="Play", parent_node_class=None): # 如果所有目标值都相同,则返回该值 if len(np.unique(data[target_attribute_name])) <= 1: return np.unique(data[target_attribute_name])[0] # 如果数据集为空,则返回父节点中最常见的目标值 elif len(data) == 0: return np.unique(originaldata[target_attribute_name])[np.argmax(np.unique(originaldata[target_attribute_name], return_counts=True)[1])] # 如果特征集为空,则返回父节点中最常见的目标值 elif len(features) == 0: return parent_node_class # 如果以上情况都不满足,则继续构造决策树 else: # 设置父节点的目标值 parent_node_class = np.unique(data[target_attribute_name])[np.argmax(np.unique(data[target_attribute_name], return_counts=True)[1])] # 选择最佳分裂属性 item_values = [InfoGain(data, feature, target_attribute_name) for feature in features] best_feature_index = np.argmax(item_values) best_feature = features[best_feature_index] # 构造决策树 tree = {best_feature:{}} features = [i for i in features if i != best_feature] for value in np.unique(data[best_feature]): value = value sub_data = data.where(data[best_feature] == value).dropna() subtree = ID3(sub_data, originaldata, features, target_attribute_name, parent_node_class) tree[best_feature][value] = subtree return(tree) # 实例化构造决策树 def predict(query, tree, default = 'Yes'): for key in list(query.keys()): if key in list(tree.keys()): try: result = tree[key][query[key]] except: return default result = tree[key][query[key]] if isinstance(result, dict): return predict(query, result) else: return result # 测试样本分类 data = {'Outlook': 'Sunny', 'Temperature': 'Cool', 'Humidity': 'High', 'Wind': 'Strong'} tree = ID3(df, df, df.columns[:-1]) print(predict(data, tree)) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_38220914

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值