java生成zipf分布,Zipf分布:如何测量Zipf分布

How do I measure or find the Zipf distribution ? For example, I have a corpus of english words. How do I find the Zipf distribution ? I need to find the Zipf ditribution and then plot a graph of it. But I am stuck in the first step which is to find the Zipf distribution.

Edit: From the frequency count of each word, it is clear that it obeys the Zipf law. But my aim is to plot a zipf distribution graph. I have no idea about how to calculate the data for the distribution graph

解决方案

I don't pretend to understand statistics. However, based upon reading from scipy site, here is a naive attempt in python.

Build Data

First we get our data. For example we download data from National Library of Medicine MeSH (Medical Subject Heading) ASCII file d2016.bin (28 MB).

Next, we open file, convert to string.

open_file = open('d2016.bin', 'r')

file_to_string = open_file.read()

Next we locate individual words in the file and separate out words.

words = re.findall(r'(\b[A-Za-z][a-z]{2,9}\b)', file_to_string)

Finally we prepare a dict with unique words as key and word count as values.

for word in words:

count = frequency.get(word,0)

frequency[word] = count + 1

Build zipf distribution data

For speed purpose we limit data to 1000 words.

n = 1000

frequency = {key:value for key,value in frequency.items()[0:n]}

After that we get frequency of values , convert to numpy array and use numpy.random.zipf function to draw samples from a zipf distribution.

Distribution parameter a =2. as a sample as it needs to be greater than 1.

For visibility purpose we limit data to 50 sample points.

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)

And finally plot the data.

Putting All Together

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()

Plot

avYo7.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值