【问题标题】:mean of large list with np.mean带有 np.mean 的大列表的平均值
【发布时间】:2019-11-01 02:34:19
【问题描述】:

我有一个列表,其中包含超过 10000 个数字,np.mean() 不适用于这么多(直到 2000 个数字) 我该怎么办?

我把这个代码:np.sum(myList])

我得到这个错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-217-50e215357de3> in <module>
----> 1 np.sum(cleanedList[1:20000])

~\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in sum(a, axis, dtype, out, keepdims, initial)
   2074 
   2075     return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
-> 2076                           initial=initial)
   2077 
   2078 

~\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     84                 return reduction(axis=axis, out=out, **passkwargs)
     85 
---> 86     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     87 
     88 

TypeError: cannot perform reduce with flexible type

【问题讨论】:

  • 不是吗:myList.sum()/len(myList)
  • “np.mean 不适用于这么多人”。我可以立即在 2000 万个号码上拨打 np.meannp.random.random(20_000_000).mean(),所以我怀疑您的问题出在其他地方。
  • 什么是type(cleanedList[n]),您应该在哪里找到n,通过二分搜索找出它何时停止工作?我猜你里面有一些不是数字的东西。
  • 没有总和也不适用于这么多人

标签: python numpy statistics


【解决方案1】:

鉴于该错误,您的输入列表很可能是恰好代表数字的字符串列表。实际上将您的输入转换为浮点数列表,然后numpy.mean() 就可以正常工作了。

【讨论】:

  • 我无法将其转换为浮点数,我得到“无法将字符串转换为浮点数:”错误
  • 如果您不能将条目转换为数字,您如何期望能够使用它来取平均值?
猜你喜欢
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
  • 2017-12-15
相关资源
最近更新 更多