使用弹性类型plt.hist无法执行reduce [英] cannot perform reduce with flexible type plt.hist
本文介绍了使用弹性类型plt.hist无法执行reduce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含1000个元素及其各自频率的数据集.我需要绘制出现的前10个元素的直方图.
我做到了:
I have a dataset with 1000s of elements and their respective frquencies. i need to plot a histogram of the top 10 occurring elements.
i did:
top_words = Counter(my_data).most_common()
top_words_10 = top_words[:10]
plt.hist(top_words_10,label='True')
并收到此错误:
TypeError
Traceback (most recent call last)
<ipython-input-29-ff974b3a2354> in <module>()
5 print top_words[:10]
6
----> 7 plt.hist(top_words_10)
C:\Anaconda\lib\site-packages\numpy\core\_methods.pyc in _amin(a, axis, out, keepdims)
12 def _amin(a, axis=None, out=None, keepdims=False):
13 return um.minimum.reduce(a, axis=axis,
---> 14 out=out, keepdims=keepdims)
15
16 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):
TypeError: cannot perform reduce with flexible type
有什么主意吗?我的数据如下:
Any idea?? my data looks like this :
[(' whitefield', 65299), (' bellandur', 57061), (' kundalahalli', 51769), (' marathahalli', 50639), (' electronic city', 44041), (' sarjapur road junction', 34164), (' indiranagar 2nd stage', 32459), (' malleswaram', 32171), (' yelahanka main road', 28901), (' domlur', 28869)]
推荐答案
出现此错误是因为您需要将数据转换为数字类型.您的数组包含字符串.
You get this error because you need to convert your data to a numeric type. Your array contains strings.
import matplotlib.pyplot as plt
import numpy as np
data = [(' whitefield', 65299), (' bellandur', 57061), (' kundalahalli', 51769), (' marathahalli', 50639),
(' electronic city', 44041), (' sarjapur road junction', 34164), (' indiranagar 2nd stage', 32459),
(' malleswaram', 32171), (' yelahanka main road', 28901), (' domlur', 28869)]
freequency = []
words = []
for line in data:
freequency.append(line[1])
words.append(line[0])
y_axis = np.arange(1, len(words) + 1, 1)
plt.barh(y_axis, freequency, align='center')
plt.yticks(y_axis, words)
plt.show()
这篇关于使用弹性类型plt.hist无法执行reduce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文