一个python的计算熵(entropy)的函数

计算熵的函数:

# -*- coding: utf-8 -*-
import math

#the function to calculate entropy, you should use the probabilities as the parameters
def entropy(*c):
    result=-1;
    if(len(c)>0):
        result=0;
    for x in c:
        result+=(-x)*math.log(x,2)
    return result;
    
if (__name__=="__main__"):
    print(entropy(1/3,2/3));

 

文章来自:http://www.cnblogs.com/vanwoos/p/5575241.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3