离散变量的熵估计

时间:2019-03-28 14:39:50

标签: python-3.x numpy

我正在尝试使用pyitlib软件包为python估计3个变量的条件熵。 但我遇到错误

我的代码:

import numpy as np
from pyitlib import discrete_random_variable as drv

X=[0,1,1,0,1,0,1,0,0,1,0,0]
Y=[0,1,1,0,0,0,1,0,0,1,1,0]
Z=[1,0,0,1,1,0,0,1,1,0,0,1]

a=drv.entropy_conditional('X','Y','Z')
print(a)

错误:

Warning (from warnings module):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\arraysetops.py", line 518
    mask &= (ar1 != a)
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
Traceback (most recent call last):
  File "C:\Users\user\Downloads\testing.py", line 8, in <module>
    a=drv.entropy_conditional('X','Y','Z')
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pyitlib\discrete_random_variable.py", line 3495, in entropy_conditional
    fill_value_Alphabet_Y))
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pyitlib\discrete_random_variable.py", line 4689, in _map_observations_to_integers
    Fill_values = [L.transform(np.atleast_1d(f)) for f in Fill_values]
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pyitlib\discrete_random_variable.py", line 4689, in <listcomp>
    Fill_values = [L.transform(np.atleast_1d(f)) for f in Fill_values]
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\preprocessing\label.py", line 257, in transform
    _, y = _encode(y, uniques=self.classes_, encode=True)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\preprocessing\label.py", line 110, in _encode
    return _encode_numpy(values, uniques, encode)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\preprocessing\label.py", line 53, in _encode_numpy
    % str(diff))
ValueError: y contains previously unseen labels: [-1]

我正在尝试使用以下公式查找X,Y,Z(上面给出的值)之间的条件互信息:

I(X;Y|Z)=H(X,Z)+H(Y,Z)-H(X,Y,Z)-H(Z)

期望的条件互助信息值为= 0.011

2 个答案:

答案 0 :(得分:1)

方法a = drv.entropy_conditional(X,Y,Z) 需要一个数组,而不是字符串,因此会收到错误。您只需将行更改为:

{{1}}

文档可用here

答案 1 :(得分:0)

这个问题似乎包含了几乎重复的内容,有关如何计算条件熵的信息,请参见this repsponse