Python | 信息熵 Information Entropy
def counter(list):
c_dict = {}
for i in list:
if i in c_dict:
c_dict[i] += 1
else:
c_dict[i] = 1
return c_dict
def entropy(x):
counts = counter(x) #每个变量出现的次数
prob = [i/len(x) for i in counts.values()] # 每个变量发生的概率
return -sum([i*math.log(i) for i in prob]) # 计算信息熵
x = np.array([2,3,4,1,1,3,4,5,6,2,1,3,4,5,5,6,7,3,2,4,4,2])
print(entropy(x))