无法绘制 Zipf 的分布图
Unable to Plot Zipf's Distribution Graph
我是 python 和机器学习的新手。我想为一个文本文件绘制 Zipf 的分布图。但是我的代码出错了。
以下是我的 python 代码
import re
from itertools import islice
#Get our corpus of medical words
frequency = {}
list(frequency)
open_file = open("abp.csv", '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 islice(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()
并且上面的代码给出了以下错误:
计数,分箱,忽略 = plt.hist(s[s<50], 50, normed=True)
TypeError: 'dict_values' 和 'int'
实例之间不支持“<”
numpy数组s
实际上由一个dict_values
对象组成。要将值转换为包含 dict_values
数字的 numpy 数组,请使用
import numpy as np
frequency = {key:value for key,value in islice(frequency.items(), 0, n)}
s = np.fromiter(frequency.values(), dtype=float)
假设,您希望数组包含 float
个。
有关详细信息,请阅读 docs。
我是 python 和机器学习的新手。我想为一个文本文件绘制 Zipf 的分布图。但是我的代码出错了。 以下是我的 python 代码
import re
from itertools import islice
#Get our corpus of medical words
frequency = {}
list(frequency)
open_file = open("abp.csv", '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 islice(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()
并且上面的代码给出了以下错误: 计数,分箱,忽略 = plt.hist(s[s<50], 50, normed=True)
TypeError: 'dict_values' 和 'int'
实例之间不支持“<”numpy数组s
实际上由一个dict_values
对象组成。要将值转换为包含 dict_values
数字的 numpy 数组,请使用
import numpy as np
frequency = {key:value for key,value in islice(frequency.items(), 0, n)}
s = np.fromiter(frequency.values(), dtype=float)
假设,您希望数组包含 float
个。
有关详细信息,请阅读 docs。