import matplotlib.pyplot as plt
from math import log
import numpy as np
#计算二元信息熵
def entropy(props, base=2):
sum = 0
for prop in props:
sum += prop * log(prop, base)
return sum * -1
#构造数据
x = np.arange(0.01,1,0.01)
props = []
for i in x:
props.append([i, 1-i])
y = [entropy(i) for i in props]
plt.plot(x,y)
plt.xlabel("p(x)")
plt.ylabel("H(x)")
plt.show()
