首页 > 安全资讯 >

编程开发练习:决策树

17-07-08

决策树伪代码。

决策树伪代码

输入:训练集 D={(x1,y1),(x2,y2),...,(xn,yn)}
??? 属性集 A={a1,a2,...,ad}
输出: 以node为根结点的一颗决策树

过程:createBranch()
检测数据集中的每个子项是否属于同一分类:
  if so return 类标签;
  else
      寻找划分数据集的最好特征
      划分数据集
      创建分支节点
          for 每个划分的子集
              调用函数createBranch并增加返回结果到分支界结点中
      return 分支结点

数据集

from http://archive.ics.uci.edu/ml/machine-learning-databases/lenses/lenses.names
+ Number of Instances: 24
+ Number of Attributes: 4 (all nominal)
+ Attribute Information:
+ age of the patient:
+ (1) young
+ (2) pre-presbyopic
+ (3) presbyopic
+ spectacle prescription:
+ (1) myope
+ (2) hypermetrope
+ astigmatic:
+ (1) no
+ (2) yes
+ tear production rate:
+ (1) reduced
+ (2) normal
+ Class Distribution:
+ hard contact lenses: 4
+ soft contact lenses: 5
+ no contact lenses: 15

决策树算法python实现

main.py

import trees
import treePlotter

fr = open('lenses.txt')
lenses = [inst.strip().split('\t') for inst in fr.readlines()]
lensesLabels = ['age', 'prescript', 'astigmatic', 'tearRate']
lensesTree = trees.createTree(lenses, lensesLabels)
treePlotter.createPlot(lensesTree)

trees.createTree

#input:DataSet, Attributes
#output;decision tree
def createTree(dataSet,labels):
    classList = [example[-1] for example in dataSet]
    if classList.count(classList[0]) == len(classList):
        return classList[0] #stop splitting when all of the classes are equal
    if len(dataSet[0]) == 1: #stop splitting when there are no more features in dataSet
        return majorityCnt(classList)
    bestFeat = chooseBestFeatureToSplit(dataSet)
    bestFeatLabel = labels[bestFeat]
    myTree = {bestFeatLabel:{}}
    del(labels[bestFeat])
    featValues = [example[bestFeat] for example in dataSet]
    uniqueVals = set(featValues)
    for value in uniqueVals:
        subLabels = labels[:]       #copy all of labels, so trees don't mess up existing labels
        myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value),subLabels)
    return myTree

trees.majorityCnt

#input:list
#output:the most frequent num in the list
#algorithm:hash
def majorityCnt(classList):
    classCount={}
    for vote in classList:
        if vote not in classCount.keys(): classCount[vote] = 0
        classCount[vote] += 1
    sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)
    return sortedClassCount[0][0]

trees.chooseBestFeatureToSplit

Information Gain: Gain(D,a)=Ent(D)?∑Vv=1|Dv|DEnt(Dv)
一般而言,信息增益越大,则意味着使用属性a来划分所获得的“纯度提升”越大。

#input: dataSet
#output: current bestFeature
#algorithm: find the feature with best information gain
def chooseBestFeatureToSplit(dataSet):
    numFeatures = len(dataSet[0]) - 1      #the last column is used for the labels
    baseEntropy = calcShannonEnt(dataSet)
    bestInfoGain = 0.0; bestFeature = -1
    for i in range(numFeatures):        #iterate over all the features
        featList = [example[i] for example in dataSet]#create a list of all the examples of this feature
        uniqueVals = set(featList)       #get a set of unique values
        newEntropy = 0.0
        for value in uniqueVals:
            subDataSet = splitDataSet(dataSet, i, value)
            prob = len(subDataSet)/float(len(dataSet))
            newEntropy += prob * calcShannonEnt(subDataSet)
        infoGain = baseEntropy - newEntropy     #calculate the info gain; ie reduction in entropy
        if (infoGain > bestInfoGain):       #compare this to the best gain so far
            bestInfoGain = infoGain         #if better than current best, set to best
            bestFeature = i
    return bestFeature                      #returns an integer

trees.calcShannonEnt

Information Entropy: Ent(D)=?∑k=1|y|pklog2pk
|y|: 数据集D中,样本种类的个数。

#input: current dataSet
#output: the information gain of current dataSet
#algorithm: Ent(D)
def calcShannonEnt(dataSet):
    numEntries = len(dataSet)
    labelCounts = {}
    for featVec in dataSet: #the the number of unique elements and their occurance
        currentLabel = featVec[-1]
        if currentLabel not in labelCounts.keys(): labelCounts[currentLabel] = 0
        labelCounts[currentLabel] += 1
    shannonEnt = 0.0
    for key in labelCounts:
        prob = float(labelCounts[key])/numEntries
        shannonEnt -= prob * log(prob,2) #log base 2
    return shannonEnt

trees.splitDataSet

假定离散属性 aV 个可能的取值 a1,a2,...,aV,若使用a来对样本集D进行划分则会产生 V 个分支结点,其中第 v 个分支结点包含了 D 中所有在属性 a 上取值为 aV 的样本,记为 Dv

'''
input: dataSet: the dataset we'll split;
       axis: the feature we'll split on;
       value: the value of the feature.
output: dataset splitting on a given feature.
'''
def splitDataSet(dataSet, axis, value):
    retDataSet = []
    for featVec in dataSet:
        if featVec[axis] == value:
            reducedFeatVec = featVec[:axis]     #chop out axis used for splitting
            reducedFeatVec.extend(featVec[axis+1:])
            retDataSet.append(reducedFeatVec)
    return retDataSet

决策树可视化

使用Matplotlib注解绘制决策树

treePlotter.createPlot

def createPlot(inTree):
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    axprops = dict(xticks=[], yticks=[])
    createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)    #no ticks
    #createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
    plotTree.totalW = float(getNumLeafs(inTree))
    plotTree.totalD = float(getTreeDepth(inTree))
    plotTree.xOff = -0.5/plotTree.totalW; plotTree.yOff = 1.0;
    plotTree(inTree, (0.5,1.0), '')
    plt.show()
相关文章
最新文章
热点推荐