https://blog.csdn.net/tangxianyu/article/details/105759989
import pandas as pd
from scipy.stats import entropy
my_list = [[1 / 8, 1 / 16, 1 / 32, 1 / 32],
[1 / 16, 1 / 8, 1 / 32, 1 / 32],
[1 / 16, 1 / 16, 1 / 16, 1 / 16],
[1 / 4, 0, 0, 0]
]
my_list = [[1 / 5, 1 / 5],
[1 / 5, 2 / 5]
]
my_array = np.array(my_list)
df = pd.DataFrame(my_array)
print(df)
col_sums = df.sum(axis=0)
print('X的边际分布', list(col_sums), 'X的熵', entropy(list(col_sums), base=2))
row_sums = df.sum(axis=1)
print('Y的边际分布', list(row_sums), 'Y的熵', entropy(list(row_sums), base=2))
H_x_given_y = []
for i, (p_y_b, p_x) in enumerate(zip(list(row_sums), my_list)):
print(f'条件:y_{i}', p_y_b, f'后验概率:', p_x, f'后验概率熵:', entropy(p_x, base=2), f'条件熵',
p_y_b * entropy(p_x, base=2))
H_x_given_y.append(p_y_b * entropy(p_x, base=2))
print('H(X|Y)条件熵', sum(H_x_given_y))
H_y_given_x = []
for i, (p_y_b, p_x) in enumerate(zip(list(col_sums), list(zip(*my_list)))):
print(f'条件:x_{i}', p_y_b, f'散布概率:', p_x, f'散布概率熵:', entropy(p_x, base=2), f'条件熵',
p_y_b * entropy(p_x, base=2))
H_y_given_x.append(p_y_b * entropy(p_x, base=2))
print('H(Y|X)条件熵', sum(H_y_given_x))
print('联合概率',)
print(df)
print('联合熵',entropy([item for sublist in my_list for item in sublist], base=2))
print(f'互信息H(X)-H(X|Y):{entropy(list(col_sums), base=2)}-{sum(H_x_given_y)}=',entropy(list(col_sums), base=2)-sum(H_x_given_y))
print(f'互信息H(Y)-H(Y|X):{entropy(list(row_sums), base=2)}-{sum(H_y_given_x)}=',entropy(list(row_sums), base=2)-sum(H_y_given_x))