Information Gain 计算 python实现

本文介绍了信息熵的计算方法,并提供了Python实现。包括了单一分布的信息熵计算函数I(*args)、多分类情况下的信息熵计算函数E(num,*args),以及信息增益计算函数Gain(i,e)。这些函数可用于评估数据集的不确定性,为决策树等机器学习算法提供支持。



from math import log

def I(*args):
    total = sum(args) + 0.0
    result = 0.0
    for i in args:
        if i == 0:
            result += 0
        else:
            result += i / total * log( i / total, 2)
    return -result

#num表示分类的个数
def E(num, *args):
    if len(args) % num != 0:
        print "Error len(args)"
    result = 0.0
    total = sum(args)
    for x in xrange(len(args) / num):
        k = x * num
        total_up = 0.0 + sum(args[k:k + num])
        result += total_up / total * I(*args[k:k + num])
    return result

def Gain(i, e):
    return i - e

#取个别名
i = I
e = E
g = Gain

if __name__ == "__main__":
    #for example
    print i(9, 5)
    print e(2, 2, 3, 4, 0, 3, 2)




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值