Python 无法使用灵活类型 plt.hist 执行 reduce

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21472243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:55:58  来源:igfitidea点击:

cannot perform reduce with flexible type plt.hist

pythontextmatplotlibword-frequency

提问by Hypothetical Ninja

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:

我有一个包含 1000 个元素及其各自频率的数据集。我需要绘制前 10 个出现元素的直方图。
我做到了:

  top_words = Counter(my_data).most_common()  
  top_words_10 = top_words[:10]  
  plt.hist(top_words_10,label='True')    

and got this error :

并收到此错误:

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)]

采纳答案by Vlad Sonkin

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()

回答by tacaswell

The problem is that plt.histtries to use nmupy.histto make a histogram from the data you pass in.

问题是plt.hist试图使用nmupy.hist您传入的数据制作直方图。

You want to just use bar

你只想使用 bar

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
words, counts = zip(*data32)  # unpack pairs into two lists
ax.bar(range(len(counts)), words, align='center')
ax.set_xticks(range(len(counts))
ax.set_xticklabels(words)  # this is about the _only_ use for set_xticklabels
plt.draw

See this exampleand the documentation.

请参阅此示例文档