Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 打印条件频率分布时以百分比格式显示y轴_Python_Matplotlib_Nltk - Fatal编程技术网

Python 打印条件频率分布时以百分比格式显示y轴

Python 打印条件频率分布时以百分比格式显示y轴,python,matplotlib,nltk,Python,Matplotlib,Nltk,在为文本语料库中的某组单词绘制条件频率分布时,y轴显示为计数,而不是百分比 我遵循Steven Bird、Ewan Klein和Edward Loper在《Python自然语言处理》中概述的代码,在Jupyter笔记本中显示UDHR不同语言的单词频率分布 from nltk.corpus import udhr languages = ['Chickasaw', 'English', 'German_Deutsch', 'Greenlandic_Inuktikut', 'Hungarian_Ma

在为文本语料库中的某组单词绘制条件频率分布时,y轴显示为计数,而不是百分比

我遵循Steven Bird、Ewan Klein和Edward Loper在《Python自然语言处理》中概述的代码,在Jupyter笔记本中显示UDHR不同语言的单词频率分布

from nltk.corpus import udhr
languages = ['Chickasaw', 'English', 'German_Deutsch', 'Greenlandic_Inuktikut', 'Hungarian_Magyar', 'Ibibio_Efik']
cfd = nltk.ConditionalFreqDist((lang, len(word)) for lang in languages\
                                                 for word in udhr.words(lang + '-Latin1'))
cfd.plot(cumulative = True)
我希望y轴显示累计百分比(如书中所示),但y轴显示的是累计计数。
请建议如何将y轴转换为累计百分比。

以下是一个解决方案,它将提供您所需的输出:

inltk.download('udhr')
import pandas as pd
from nltk.corpus import udhr

languages = ['Chickasaw', 'English', 'German_Deutsch', 'Greenlandic_Inuktikut', 'Hungarian_Magyar', 'Ibibio_Efik']

cfd = nltk.ConditionalFreqDist(
    (lang, len(word))
    for lang in languages
    for word in udhr.words(lang + '-Latin1'))

def plot_freq(lang):
    max_length = max([len(word) for word in udhr.words(lang + '-Latin1')])
    eng_freq_dist = {}

    for i in range(max_length + 1):
        eng_freq_dist[i] = cfd[lang].freq(i)

    ed = pd.Series(eng_freq_dist, name=lang)

    ed.cumsum().plot(legend=True, title='Cumulative Distribution of Word Lengths')
然后,我们可以使用此新函数绘制示例中提供的所有语言:

for lang in languages:
plot_freq(lang)
在这篇文章中,我们将讨论第2章中的例子