[英]Joint entropy in python
I have two arrays:我有两个数组:
import numpy as np
a = np.array(['1','2','3'])
b = np.array(['3','4','1','5'])
I want to calculate joint entropy.我想计算联合熵。 I've found some materials to make it like:
我找到了一些材料来制作它:
import numpy as np
def entropy(*X):
return = np.sum(-p * np.log2(p) if p > 0 else 0
for p in (np.mean(reduce(np.logical_and, (predictions == c for predictions, c in zip(X, classes))))
for classes in itertools.product(*[set(x) for x in X])))
Seems to work fine with len(a) = len(b)
but it ends with error if len(a) != len(b)
使用
len(a) = len(b)
似乎工作正常,但如果len(a) != len(b)
则以错误结束
UPD: Arrays a
and b
were created from exampled main input: UPD:数组
a
和b
是从示例主输入创建的:
b:3 p1:1 p2:6 p5:7
b:4 p1:2 p7:2
b:1 p3:4 p5:8
b:5 p1:3 p4:4
Where array a
was created from p1 values.其中数组
a
是从 p1 值创建的。 So not every line consists of every pK
but every has b
property.所以不是每条线都包含每个
pK
但每条线都有b
属性。 I need to calculate mutual information I(b,pK)
for each pK
.我需要为每个
pK
计算互信息I(b,pK)
。
Assuming you are talking about the Joint Shannon Entropy , the formula straightforward:假设你在谈论联合香农熵,公式很简单:
The problem with this, when I look at what you've done so far, is that you lack P(x,y)
, ie the joint probability of the two variables occurring together.问题在于,当我查看您到目前为止所做的工作时,您缺少
P(x,y)
,即两个变量一起出现的联合概率。 It looks like a,b
are the individual probabilities for events a and b respectively.看起来
a,b
分别是事件 a 和 b 的个体概率。
You have other problems with your posted code (mentioned in the comments):您发布的代码有其他问题(在评论中提到):
a=["1","2"]
is not the same as a=[1,2]
.a=["1","2"]
是不一样的a=[1,2]
One is a string, the other is a number.P(x,y)
.P(x,y)
。Here is an idea:这是一个想法:
import numpy as np
from scipy import stats
a = np.array(['1','2','3','0'])
b = np.array(['3','4','1','5'])
aa = [int(x) for x in a]
bb = [int(x) for x in b]
je = stats.entropy(aa,bb)
print("joint entropy : ",je)
output: 0.9083449242695364
输出:0.9083449242695364
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.