2 回答
TA贡献1836条经验 获得超3个赞
statistics.mean将输出转换为与输入相同的类型。如果输入值都为 ,例如numpy.int64,则结果将转换为numpy.int64。这是statistics.meanPython 3.6.7 中的源代码:
def mean(data):
"""Return the sample arithmetic mean of data.
>>> mean([1, 2, 3, 4, 4])
2.8
>>> from fractions import Fraction as F
>>> mean([F(3, 7), F(1, 21), F(5, 3), F(1, 3)])
Fraction(13, 21)
>>> from decimal import Decimal as D
>>> mean([D("0.5"), D("0.75"), D("0.625"), D("0.375")])
Decimal('0.5625')
If ``data`` is empty, StatisticsError will be raised.
"""
if iter(data) is data:
data = list(data)
n = len(data)
if n < 1:
raise StatisticsError('mean requires at least one data point')
T, total, count = _sum(data)
assert count == n
return _convert(total/n, T)
请注意,total/n在返回之前转换为输入类型。
为避免这种情况,您可以将输入转换为浮点数,然后再将其传递给statistics.mean.
添加回答
举报