2016-08-24 8 views
0

私はzipfディストリビューションをその組み合わせで実行する段落のリストを持っています。matplotlib、FITTED-LINEで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") 

PURPOSE私はこのグラフでは「フィットライン」を引くしようとすると、変数に値を割り当てます。しかし、私はそれをどのように追加するのか分かりません。どんな助けもこれらの問題の両方で高く評価されるでしょう。

答えて

1

この質問は尋ねられてからしばらくありました。しかし、この問題の解決策は、scipy siteです。
他の誰かが必要な場合に私がここに投稿すると思った。

私は段落情報を持っていなかったので、ここで段落の出現を値として持つというdictと呼びます。

次に、値を取得してnumpy配列に変換します。定義されたzipf distribution parameterは> 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() 

プロット enter image description here

関連する問題