简体   繁体   English

如何使用Python nltk的ProbDistI类

[英]How to use Python nltk's ProbDistI class

Suppose I have a list of probability distributions (the sum of this is 1). 假设我有一个概率分布列表(总和为1)。 I want to use that list to create a ProbDistI object. 我想使用该列表来创建ProbDistI对象。 How do I use the NLTK's ProbDistI class (under the probability module) to create an object that contains this distribution? 如何使用NLTK的ProbDistI类(在概率模块下)创建包含此分布的对象? I had looked under the documentation listed here ( Link ) and it looks like all the methods extract some value related to an object that already has a probability distribution. 我查看了此处列出的文档( 链接 ),看起来所有方法都提取了一些与已经具有概率分布的对象相关的值。

Are there any examples that uses ProbDistI? 有没有使用ProbDistI的例子? I've looked all over and had trouble finding any resources online. 我看了一遍,无法在网上找到任何资源。

Thanks! 谢谢!

As far as I understand ProbDistI class is an interface, the other classes implement it. 据我所知,ProbDistI类是一个接口,其他类实现它。 That is each distribution class must have the methods of ProbDistI interface like prob(), max() etc. You could look directly in code for it. 也就是说,每个分发类必须具有ProbDistI接口的方法,如prob(),max()等。您可以直接在代码中查找它。

The reason it was made this way could be that a distribution in general is too complex to describe as an object, whereas the special cases of distributions are easier to describe. 这样做的原因可能是一般的分布太复杂而不能描述为一个对象,而分布的特殊情况更容易描述。 For example, you can initiate UniformProbDist class which implements ProbDistI. 例如,您可以启动实现ProbDistI的UniformProbDist类。

from nltk.probability import UniformProbDist as U
UD=U([1,2,3,4])

Now you have a uniform distribution UD. 现在你有一个统一的分布UD。 with UD.prob(1) you get 0.25 使用UD.prob(1)你得到0.25

Another example of a distribution class that implements ProbDistI is DictionaryProbDist. 实现ProbDistI的分发类的另一个示例是DictionaryProbDist。 You can create the same distribution as in the previous example: 您可以创建与上一示例相同的分发:

from nltk.probability import DictionaryProbDist as D
DD=D({1:0.25,2:0.25,3:0.25,4:0.25})
print D.prob(1)
>>> 0.25

For another ways to create a distribution you could look directly in code searching for lines like this: 对于另一种创建分发的方法,您可以直接在代码中查找如下行:

class DictionaryProbDist(ProbDistI):

that is a class that implements the interface ProbDistI 这是一个实现ProbDistI接口的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM