Pandas 中的熵返回 '-inf'(无穷大)

Entropy in Pandas returning '-inf' (infinity)

我正在计算数据帧的熵 "default of credit card clients"。我从 UCI 机器学习库中获取它。 https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients

此处提供 CSV 文件 - https://www.kaggle.com/uciml/default-of-credit-card-clients-dataset

我使用了下面的代码

from scipy.stats import entropy
entropy(df)

这是我的输出:

array([10.11582214, 10.01808774, 10.25940955, 10.22181775, 10.25018627,
       10.27641471,        -inf,        -inf,        -inf,        -inf,
              -inf,        -inf,        -inf,        -inf,        -inf,
              -inf,        -inf,        -inf,  9.15542383,  8.98160775,
        8.97607359,  8.94236069,  8.94899185,  8.81999977,  8.80026465])

根据定义,熵可以变得无穷大吗?我用谷歌搜索 https://www.researchgate.net/post/How_can_I_handle_entropy_with_infinite_value

但是,我需要专家的建议来解释为什么熵是无限的以及 如何在 pandas 中纠正这个问题。

注意 - 至少应使用“0”代替“-inf”。

看来你的 df 可能包含负数。

import pandas as pd
from scipy.stats import entropy

df = pd.DataFrame({'a':[1,2,3],'b':[4,5,-6]})

entropy(df)
>>> array([1.01140426,       -inf])

entropy(df[:2])
>>> array([0.63651417, 0.68696158])