【问题标题】:Python: NLTK ValueError: A Lidstone probability distribution must have at least one bin?Python:NLTK ValueError:Lidstone 概率分布必须至少有一个 bin?
【发布时间】:2019-02-11 09:19:44
【问题描述】:

对于一项任务,我将使用 ConditionalProbDist 并使用 LidstoneProbDist 作为估计器,将 +0.01 添加到每个 bin 的样本计数中。

我认为下面的代码行可以实现这一点,但它会产生一个值错误

fd = nltk.ConditionalProbDist(fd,nltk.probability.LidstoneProbDist,0.01)

我不确定如何在 ConditionalProbDist 中设置参数的格式,并且通过 python 的帮助功能或谷歌找到如何执行此操作的运气并不好,所以如果有人能帮我设置正确,将不胜感激!

【问题讨论】:

    标签: python nltk probability


    【解决方案1】:

    我在 NLTK 网站上发现 the probability tutorial 作为参考非常有用。

    如上面的答案所述,使用 lambda 表达式是个好主意,因为 ConditionalProbDist 会动态生成频率分布 (nltk.FreqDist),并传递给估算器。

    更微妙的一点是,如果您不知道输入样本中最初有多少个 bin,则无法传递 bins 参数!但是,FreqDist 的可用 bin 数量为 FreqDist.B() (docs)。

    改为使用 FreqDist 作为 lambda 的唯一参数:

    from nltk.probability import *
    # ...
    
    # Using the given parameters of one extra bin and a gamma of 0.01
    lidstone_estimator = lambda fd: LidstoneProbDist(fd, 0.01, fd.B() + 1)
    conditional_pd = ConditionalProbDist(conditional_fd, lidstone_estimator)
    

    我知道这个问题现在已经很老了,但我也很难找到文档,所以我在这里记录它,以防其他人遇到类似的问题。

    祝你好运(使用 fnlp)!

    【讨论】:

      【解决方案2】:

      您可能不再需要这个,因为这个问题已经很老了,但您仍然可以在 lambda 的帮助下将 LidstoneProbDist 参数传递给 ConditionalProbDist:

      estimator = lambda fdist, bins: nltk.LidstoneProbDist(fdist, 0.01, bins)
      cpd = nltk.ConditionalProbDist(fd, estimator, bins)
      

      【讨论】:

        猜你喜欢
        • 2015-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-07
        • 2021-05-13
        • 2020-11-06
        • 1970-01-01
        • 2022-01-13
        相关资源
        最近更新 更多