码迷,mamicode.com
首页 > 其他好文 > 详细

决策树

时间:2015-06-03 13:30:21      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

计算数据集的香农熵

from math import log
def calcShannonEnt(dataSet):
    numEntries = len(dataset)
    labelCounts = {}
    for featVec in dataset:
        currentLabel = featVec[-1]
        if currentLabel not in labelCounts.keys():
            labelCountspcurrentLabel] = 0
        labelCounts[currentLabel] += 1
    shannonEnt = 0.0
    for key in labelCounts:
        prob = float(labelCounts[key])/numEntries
        shannonEnt -= prob * log(prob,2)
    return shannonEnt

 H=-∑p(xi)log(2,p(xi)) (i=1,2,..n)

决策树

标签:

原文地址:http://www.cnblogs.com/battle-lee/p/4548768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!