Rah*_*hra 3 python statistics numpy scipy zipf
如何测量或找到 Zipf 分布?例如,我有一个英语单词语料库。我如何找到 Zipf 分布?我需要找到 Zipf 分布,然后绘制它的图形。但我陷入了第一步,即找到 Zipf 分布。
编辑:从每个单词的频率计数来看,很明显它遵守 Zipf 定律。但我的目标是绘制一个 zipf 分布图。我不知道如何计算分布图的数据
我不假装懂统计学。但是,基于从scipy 站点的阅读,这是python
.
构建数据
首先我们得到我们的数据。例如,我们从 National Library of Medicine MeSH (Medical Subject Heading) ASCII 文件d2016.bin (28 MB)下载数据 。
接下来,我们打开文件,转换为字符串。
open_file = open('d2016.bin', 'r')
file_to_string = open_file.read()
Run Code Online (Sandbox Code Playgroud)
接下来,我们在文件中定位单个单词并分离出单词。
words = re.findall(r'(\b[A-Za-z][a-z]{2,9}\b)', file_to_string)
Run Code Online (Sandbox Code Playgroud)
最后,我们准备一个以唯一词作为关键字和词数作为值的字典。
for word in words:
count = frequency.get(word,0)
frequency[word] = count + 1
Run Code Online (Sandbox Code Playgroud)
构建 zipf 分布数据
出于速度目的,我们将数据限制为 1000 字。
n = 1000
frequency = {key:value for key,value in frequency.items()[0:n]}
Run Code Online (Sandbox Code Playgroud)
之后我们得到值的频率,转换为numpy
数组并使用numpy.random.zipf
函数从zipf
分布中抽取样本。
分布参数a =2.
作为样本,因为它需要大于 1。出于可见性目的,我们将数据限制为 50 个样本点。
s = frequency.values()
s = np.array(s)
count, bins, ignored = plt.hist(s[s<50], 50, normed=True)
x = np.arange(1., 50.)
y = x**(-a) / special.zetac(a)
Run Code Online (Sandbox Code Playgroud)
最后绘制数据。
放在一起
import re
from operator import itemgetter
import matplotlib.pyplot as plt
from scipy import special
import numpy as np
#Get our corpus of medical words
frequency = {}
open_file = open('d2016.bin', 'r')
file_to_string = open_file.read()
words = re.findall(r'(\b[A-Za-z][a-z]{2,9}\b)', file_to_string)
#build dict of words based on frequency
for word in words:
count = frequency.get(word,0)
frequency[word] = count + 1
#limit words to 1000
n = 1000
frequency = {key:value for key,value in frequency.items()[0:n]}
#convert value of frequency to numpy array
s = frequency.values()
s = np.array(s)
#Calculate zipf and plot the data
a = 2. # distribution parameter
count, bins, ignored = plt.hist(s[s<50], 50, normed=True)
x = np.arange(1., 50.)
y = x**(-a) / special.zetac(a)
plt.plot(x, y/max(y), linewidth=2, color='r')
plt.show()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6583 次 |
最近记录: |