码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-06-11 14:26:06      阅读:5081      评论:0      收藏:0      [点我收藏+]

标签:

计算熵的函数:

# -*- 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));

 

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

标签:

原文地址:http://www.cnblogs.com/vanwoos/p/5575241.html

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