Zipf’s Law

Let f(w) be the frequency of a word w in free text. Suppose that all the words of a text are ranked according to their frequency, with the most frequent word first. Zipf’s Law states that the frequency of a word type is inversely proportional to its rank (i.e., f × r = k, for some constant k). For example, the 50th most common word type should occur three times as frequently as the 150th most common word type.
a. Write a function to process a large text and plot word frequency against word rank using pylab.plot. Do you confirm Zipf’s law? (Hint: it helps to use a logarithmic scale.) What is going on at the extreme ends of the plotted line?
b. Generate random text, e.g., using random.choice("abcdefg "), taking care to include the space character. You will need to import random first. Use the string concatenation operator to accumulate characters into a (very) long string. Then tokenize this string, generate the Zipf plot as before, and compare the two plots. What do you make of Zipf’s Law in the light of this?

 1 from nltk.corpus import gutenberg as gb
 2
 3 def validate_zipf(text,ranklimit):
 4     fdist=nltk.FreqDist([w for w in text if w.isalpha()])
 5     x=range(ranklimit)
 6     freq=[]
 7     for key in fdist.keys():
 8        freq.append(fdist[key])
 9     y=sorted(freq,reverse=True)[:ranklimit]
10     pylab.plot(x,y)
11
12 def test():
13     text=gb.words(fileids=[‘shakespeare-hamlet.txt‘])
14     validate_zipf(text,150)
15     

运行的结果为:

时间: 2024-08-28 12:44:17

Zipf’s Law的相关文章

Zipf's law

w https://www.bing.com/knows/search?q=马太效应&mkt=zh-cn&FORM=BKACAI 马太效应(Matthew Effect),指强者愈强.弱者愈弱的现象,广泛应用于社会心理学.教育.金融以及科学领域.马太效应,是社会学家和经济学家们常用的术语,反映的社会现象是两极分化,富的更富,穷的更穷.名字来自圣经<新约·马太福音>一则寓言:"凡有的,还要加倍给他叫他多余:没有的,连他所有的也要夺过来"."马太效应&

漫谈 Clustering (2): k-medoids

上一次我们了解了一个最基本的 clustering 办法 k-means ,这次要说的 k-medoids 算法,其实从名字上就可以看出来,和 k-means 肯定是非常相似的.事实也确实如此,k-medoids 可以算是 k-means 的一个变种. k-medoids 和 k-means 不一样的地方在于中心点的选取,在 k-means 中,我们将中心点取为当前 cluster 中所有数据点的平均值: Rough Collie 并且我们已经证明在固定了各个数据点的 assignment 的情

[IR] Information Extraction

阶段性总结 Boolean retrieval 单词搜索 [Qword1 and Qword2]               O(x+y) [Qword1 and Qword2]- 改进: Galloping Search   O(2a*log2(b/a)) [Qword1 and not Qword2]        O(m*log2n)  [Qword1 or not Qword2]           O(m+n) [Qword1 and Qword2 and Qword3 and ...

自然语言处理第二讲:单词计数

自然语言处理:单词计数 这一讲主要内容(Today): 1.语料库及其性质: 2.Zipf 法则: 3.标注语料库例子: 4.分词算法: 一. 语料库及其性质: a) 什么是语料库(Corpora) i. 一个语料库就是一份自然发生的语言文本的载体,以机器可读形式存储: ii. 一种平衡语料库尝试在语言或者其他领域具有代表性: b) 译者注:平行语料库与平衡语料库的特点与区别 i. 平行语料库通常是由双语或多语的对应语料构成,常常是翻译文本构成.例如:Babel English-Chinese

聚类算法学习-kmeans,kmedoids,GMM

GMM参考这篇文章:Link 简单地说,k-means 的结果是每个数据点被 assign 到其中某一个 cluster 了,而 GMM 则给出这些数据点被 assign 到每个 cluster 的概率,又称作 soft assignment . 通常单个点的概率都很小,许多很小的数字相乘起来在计算机里很容易造成浮点数下溢,因此我们通常会对其取对数,把乘积变成加和 ,得到 log-likelihood function . 因此也有和 K-means 同样的问题──并不能保证总是能取到全局最优,

机器学习和深度学习资料合集

机器学习和深度学习资料合集 注:机器学习资料篇目一共500条,篇目二开始更新 希望转载的朋友,你可以不用联系我.但是一定要保留原文链接,因为这个项目还在继续也在不定期更新.希望看到文章的朋友能够学到更多.此外:某些资料在中国访问需要梯子. <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.Deep Learning. <Deep Learning in

分布式机器学习的故事

王益博士,称得上机器学习领域的资深从业者,本人之前有幸拜读过王益博士的一些paper和slides,对其从事的"分布式机器学习"方向尤感兴趣. 王益博士之前写过一篇<分布式机器学习的故事>,总结了自己从业多年的经验和感悟.可惜的是,这篇原始博文已经删除了,现在能找到的是原始的六篇讲稿素材:A New Era:Infrequent itemset mining:Application Driven:Implement Your MapReduce:Deep Learning:

[转]机器学习和深度学习资料汇总【01】

本文转自:http://blog.csdn.net/sinat_34707539/article/details/52105681 <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.Deep Learning. <Deep Learning in Neural Networks: An Overview> 介绍:这是瑞士人工智能实验室Jurgen

关键字提取算法之TF-IDF扫盲

TF-IDF(term frequency–inverse document frequency)是一种用于资讯检索与资讯探勘的常用加权技术.TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库中的其中一份文件的重要程度.字词的重要性随著它在文件中出现的次数成正比增加,但同时会随著它在语料库中出现的频率成反比下降.TF-IDF加权的各种形式常被搜寻引擎应用,作为文件与用户查询之间相关 ... TF/IDF算法可能并不是百度的重要方法,google适用:百度个人认为是向量空间模型,