【问题标题】:cannot perform reduce with flexible type plt.hist无法使用灵活类型 plt.hist 执行 reduce
【发布时间】:2014-02-23 16:27:27
【问题描述】:

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

  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

有什么想法吗??我的数据如下所示:

[(' 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)]

【问题讨论】:

    标签: python text matplotlib word-frequency


    【解决方案1】:

    您收到此错误是因为您需要将数据转换为数字类型。您的数组包含字符串。

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

    【讨论】:

    • thanx a ton ..它工作得很棒..我怎么能互换轴?我想要它以垂直方式..请解释一下for循环..
    • 看到这个答案link。在 for 循环中,我只是按元组解析列表:(' whitefield', 65299),按索引获取元素并将它们存储在列表中。
    • 应该有一些图表将分类值列表作为输入并绘制计数。这应该是开箱即用的东西,它是人们能想象到的最简单的图表。
    【解决方案2】:

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

    你只想使用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
    

    参见this exampledocumentation

    【讨论】:

    • hi..thnx 但我收到一个错误 File "", line 6 ax.set_xticklabels(words) # 这是关于 only 用于 set_xticklabels ^ SyntaxError: 无效语法
    • 哪里不见了??抱歉,但我是新手,所以所有这些都是为第一个 tym 注册的;)
    • 在 set_xticks 中。学习有效调试是学习编程时最重要的技能。语法错误很好,它们会在几行内告诉您它们的位置。
    猜你喜欢
    • 2017-09-12
    • 1970-01-01
    • 2016-09-13
    • 2020-10-03
    • 1970-01-01
    • 2013-12-02
    • 2015-09-12
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多