我有一个段落列表,我想在它们的组合上运行 zipf 分发。
我的代码如下:
from itertools import *
from pylab import *
from collections import Counter
import matplotlib.pyplot as plt
paragraphs = " ".join(targeted_paragraphs)
for paragraph in paragraphs:
frequency = Counter(paragraph.split())
counts = array(frequency.values())
tokens = frequency.keys()
ranks = arange(1, len(counts)+1)
indices = argsort(-counts)
frequencies = counts[indices]
loglog(ranks, frequencies, marker=".")
title("Zipf plot for Combined Article Paragraphs")
xlabel("Frequency Rank of Token")
ylabel("Absolute Frequency of Token")
grid(True)
for n in list(logspace(-0.5, log10(len(counts)-1), 20).astype(int)):
dummy = text(ranks[n], frequencies[n], " " + tokens[indices[n]],
verticalalignment="bottom",
horizontalalignment="left")
目的我尝试在此图中绘制“一条拟合线”,并将其值分配给一个变量。但我不知道如何添加。对于这两个问题,我们将不胜感激。
最佳答案
我知道自从提出这个问题以来已经有一段时间了。然而,我在 scipy site 找到了这个问题的可能解决方案。 .
我想我会在这里发帖以防其他人需要。
我没有段落信息,所以这里有一个名为频率
的快速构建的dict
,它以段落出现次数作为其值。
然后我们获取它的值并转换为 numpy 数组。定义 zipf 分布参数
,该参数必须 >1。
最后显示样本的直方图,以及概率密度函数
工作代码:
import random
import matplotlib.pyplot as plt
from scipy import special
import numpy as np
#Generate sample dict with random value to simulate paragraph data
frequency = {}
for i,j in enumerate(range(50)):
frequency[i]=random.randint(1,50)
counts = frequency.values()
tokens = frequency.keys()
#Convert counts of values to numpy array
s = np.array(counts)
#define zipf distribution parameter. Has to be >1
a = 2.
# Display the histogram of the samples,
#along with the probability density function
count, bins, ignored = plt.hist(s, 50, normed=True)
plt.title("Zipf plot for Combined Article Paragraphs")
x = np.arange(1., 50.)
plt.xlabel("Frequency Rank of Token")
y = x**(-a) / special.zetac(a)
plt.ylabel("Absolute Frequency of Token")
plt.plot(x, y/max(y), linewidth=2, color='r')
plt.show()
关于python - 使用 matplotlib、FITTED-LINE 构建 Zipf 分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39114402/