香农熵-互信息-entropy

概念

 信息熵--单个随机变量的信息量
    香农熵又称信息熵,反映了一条信息的信息量大小和它的不确定性之间的关系,是信息量的度量,单位为 bit
 互信息--两个随机变量间相关信息的多少 
 
  互信息不假设数据的分布,可以应用于各种类型的数据。
   依赖样本量:互信息的估计依赖于样本量,样本量不足可能导致估计不准确。
 熵可以用来计算一个系统中的失序现象,即混乱程度 熵、联合熵、条件熵以及互信息之间的关系是
    条件熵(Conditional Entropy)
	联合熵(Joint 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 count_prob(X,Y):
    # 使用字典统计每一个(x,y)元素出现的次数
    d_xy = dict()  # x的字典
    for i in range(X.size):
        if (X[i], Y[i]) in d_xy:
            d_xy[X[i], Y[i]] += 1
        else:
            d_xy[X[i], Y[i]] = 1
    # 计算每个元素出现的概率
    p_xy = dict()
    for xy in d_xy.keys():
        p_xy[xy] = d_xy[xy] / X.size
    return p_xy
    

### ( H(X) ) 是单个变量的熵
def entropy(x):
	counts = counter(x) #每个变量出现的次数
	prob = [i/len(x) for i in counts.values()] # 每个变量发生的概率
	return -sum([p*math.log(p) for p in prob if p > 0 ]) # 计算信息熵
	
def entropy(prob):
    return -np.sum([p * np.log2(p) for p in prob if p > 0])
	
	
import math
def joint_entropy(joint_prob):
    """
    计算给定联合概率分布的联合熵 joint_prob = {(0, 0): 0.25, (0, 1): 0.25, (1, 0): 0.5, (1, 1): 0}
    :param joint_prob: 一个字典,键为一对元素,值为它们同时发生的概率
    :return: 联合熵
    """
    entropy = 0.0
    for pair, prob in joint_prob.items():
        if prob > 0:
            entropy -= prob * math.log(prob, 2)
    return entropy
 
# 示例使用
joint_prob = {(0, 0): 0.25, (0, 1): 0.25, (1, 0): 0.5, (1, 1): 0}
print(joint_entropy(joint_prob))  # 输出联合熵的值

### np.bincount()	
### joint_prob = {(0, 0): 0.25, (0, 1): 0.25, (1, 0): 0.5, (1, 1): 0}
def joint_entropy(joint_prob):
    #### joint_prob = {(0, 0): 0.25, (0, 1): 0.25, (1, 0): 0.5, (1, 1): 0}
    return -np.sum([p * np.log2(p) for row in joint_prob for p in row if p > 0])
	
def mutual_information(x, y):
    joint_prob, x_prob, y_prob = calculate_probabilities(x, y)
    return entropy(x_prob) + entropy(y_prob) - joint_entropy(joint_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))

方法

scikit-learn库中的函数
     scikit-learn提供了mutual_info_classif函数
	 scikit-learn提供了mutual_info_regression函数
使用`scipy.stats`的`entropy`函数计算联合概率和条件概率,进而得到互信息

代码示例2

from scipy.stats import entropy
import numpy as np

def calculate_probabilities(x, y):
#### 计算histogram2d两个变量的联合频数,然后通过除以总样本数得到联合概率
#### 计算频次直方图(就是计算每段区间的样本数),而并不想画图显示它们,那么可以直接用 np.histogram()。
### bins : int or array_like or [int, int] or [array, array], optional
    x_bins,x_bins =20,20
    joint_prob = np.histogram2d(x, y, bins=(x_bins,y_bins))[0] / len(x)
    x_prob = np.histogram(x, bins=x_bins)[0] / len(x)
    y_prob = np.histogram(y, bins=x_bins)[0] / len(y)
    return joint_prob, x_prob, y_prob
	
def mutual_information(x, y):
    joint_prob, x_prob, y_prob = calculate_probabilities(x, y)
    mi = entropy(x_prob) + entropy(y_prob) - entropy(joint_prob.flatten())
    return mi
	
p和q是两个离散随机变量的概率分布列表。joint_probability函数通过将每个元素的概率相乘来计算联合概率	
# 定义一个函数来计算联合概率
def joint_probability(p, q):
    # 计算两个随机变量的联合概率
    joint_prob = [p[i]*q[i] for i in range(len(p))]
    return joint_prob
 
# 示例:计算两个离散随机变量的联合概率
# 概率分布p和q
p = [0.1, 0.2, 0.3, 0.4]
q = [0.4, 0.3, 0.2, 0.1]
 
# 调用函数计算联合概率
joint_prob = joint_probability(p, q)
print(joint_prob)

计算

 # 获取不重复的(x,y,z)
data_xyz = data.drop_duplicates(subset=None, keep='first', inplace=False)

参考

 https://blog.csdn.net/FFXNFFXN/article/details/124713431
posted @   辰令  阅读(121)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-10-09 Python_滑动窗口
2023-10-09 Python_numpy向量化编程以及激活函数
2021-10-09 车辆测试平台
2020-10-09 数据存储_HBase基本内容
2020-10-09 数据服务_基于SpringBoot提供数据服务
点击右上角即可分享
微信分享提示