nltk.probability.FreqDist
是一个子类collections.Counter
.
来自docs https://github.com/nltk/nltk/blob/develop/nltk/probability.py#L61:
实验结果的频率分布。 A
频率分布记录了某个结果的出现次数
实验发生了。例如,频率分布可以
用于记录文档中每种单词类型的频率。
形式上,频率分布可以定义为函数
从每个样本到该样本发生的次数的映射
作为结果。
从代码中可以清楚地看出继承关系 https://github.com/nltk/nltk/blob/develop/nltk/probability.py#L61本质上,在如何Counter
and FreqDist
已初始化,请参阅https://github.com/nltk/nltk/blob/develop/nltk/probability.py#L106 https://github.com/nltk/nltk/blob/develop/nltk/probability.py#L106
所以从速度上来说,创建一个Counter
and FreqDist
应该是一样的。速度差异应该微不足道,但值得注意的是,开销可能是:
- 在解释器中定义类时的编译
- 鸭子打字的成本
.__init__()
主要区别在于不同的功能FreqDist
提供统计/概率自然语言处理(NLP),例如寻找半轴 https://github.com/nltk/nltk/blob/develop/nltk/probability.py#L130。完整的函数列表FreqDist
延伸Counter
如下:
>>> from collections import Counter
>>> from nltk import FreqDist
>>> x = FreqDist()
>>> y = Counter()
>>> set(dir(x)).difference(set(dir(y)))
set(['plot', 'hapaxes', '_cumulative_frequencies', 'r_Nr', 'pprint', 'N', 'unicode_repr', 'B', 'tabulate', 'pformat', 'max', 'Nr', 'freq', '__unicode__'])
当谈到使用时FreqDist.most_common()
,它实际上使用的是父函数Counter
所以检索排序的速度most_common
两种类型的列表相同。
就个人而言,当我只想检索计数时,我使用collections.Counter
。但是当我需要进行一些统计操作时,我要么使用nltk.FreqDist
或者我会转储Counter
into a pandas.DataFrame
(see 将 Counter 对象转换为 Pandas DataFrame https://stackoverflow.com/questions/31111032/transform-a-counter-object-into-a-pandas-dataframe).