信息熵
信息熵可以衡量变量的不确定性,变量的不确定性越大,熵也就越大。
信息熵的公式:
image.png
python实现:
import numpy as np
data=['a','b','c','a','a','b']
data1=np.array(data)
#计算信息熵的方法
def calc_ent(x):
"""
calculate shanno ent of x
"""
x_value_list = set([x[i] for i in range(x.shape[0])])
ent = 0.0
for x_value in x_value_list:
p = float(x[x == x_value].shape[0]) / x.shape[0]
logp = np.log2(p)
ent -= p * logp
print ent
calc_ent(data1)








网友评论