找到python列表的平均值
我正在尝试找到python列表的平均值。当我打印列表时,如下所示:
[array([0.33345667, 0.33523274, 0.33222637, 0.3341597 , 0.33329942,
0.3330034 , 0.33197044, 0.33394078, 0.33458831, 0.33147091])]
预期结果,没有零值。
当我尝试statistics.mean(efceracy_list)
(这是我过去所做的事情以获得均值)时,我会收到此错误
TypeError: can't convert type 'ndarray' to numerator/denominator
吗?
I am trying to find the mean of a Python list. When I print my list it is as follows:
[array([0.33345667, 0.33523274, 0.33222637, 0.3341597 , 0.33329942,
0.3330034 , 0.33197044, 0.33394078, 0.33458831, 0.33147091])]
That outcome is expected, there are no null values.
When I try statistics.mean(accuracy_list)
(which is what I have done in the past to get the mean) I get this error
TypeError: can't convert type 'ndarray' to numerator/denominator
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
statistics.mean()
在香草python列表上工作。它确实可以在上使用numpy.ndarray
s。您应该使用
< /a>而不是。numpy.mean.mean()
另外,它看起来像
efceracy_list
是包含单元素numpy
list的列表。如果是这样,您可以使用列表解开包装:这两个输出:
statistics.mean()
works on vanilla Python lists. It does not work onnumpy.ndarray
s. You should usenumpy.mean()
instead.Also, it looks like
accuracy_list
is a list containing a one-elementnumpy
list. If that is the case, you can use list unpacking:Both of these output:
尝试以下操作:
或者
在python的较旧版本上,您可以执行:
在Python 2上,您需要将
len
转换为float以获取float discrive:Try this:
OR
On older versions of Python you can do:
On Python 2, you need to convert
len
to a float to get float division:我不确定为什么列表中有一个numpy数组,因此您可以提取并计算均值:
I'm not sure why there's a NumPy array in the list, so you could just extract it and compute the mean: