为什么是python统计.平均值()返回int类型,n

2024-06-16 09:10:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用统计.平均值()根据抽样分布计算平均值。但是,在下面的代码中,从下面的值返回的值是四舍五入的整数。如果我使用numpy.平均值()将获得正确的浮点类型的结果。这是怎么回事?在

import statistics
from scipy import stats

posterior_sample = stats.beta.rvs(3, 19, size = 1000)
predictive_sample = stats.binom.rvs(100, posterior_sample, size = 1000)
print(statistics.mean(predictive_sample))
print(statistics.mean([(data >= 15).astype(int) for data in predictive_sample]))

Tags: sample代码importnumpydatasizestats整数
1条回答
网友
1楼 · 发布于 2024-06-16 09:10:56

statistics.mean不支持numpy.int64数据类型。在

来自statisticsdocs

Unless explicitly noted otherwise, these functions support int, float, decimal.Decimal and fractions.Fraction. Behaviour with other types (whether in the numeric tower or not) is currently unsupported. Mixed types are also undefined and implementation-dependent. If your input data consists of mixed types, you may be able to use map() to ensure a consistent result, e.g. map(float, input_data).

要解决这个问题,您可以按照建议进行操作,并在传递到statistics.mean()之前将数据转换为float。在

print(statistics.mean(map(float, predictive_sample)))

现在来看看这种行为背后的深层原因:

statistics.meansource code末尾,有一个对statistics._convert的调用,这意味着将返回值转换为适当的类型(即,如果输入是分数,float如果输入是int等),则将返回值转换为适当的类型。在

_convert中的single line用于捕获其他数据类型,并确保返回值与提供的数据一致(T是每个输入值的数据类型,value是计算的平均值):

^{pr2}$

如果您的输入是numpy.int64,那么_convert函数将尝试将计算的平均值转换为numpy.int64数据类型。NumPy很乐意将浮点转换为int(我想是四舍五入)。因此,mean函数返回四舍五入到最接近整数的平均值,编码为numpy.int64。在

如果您的输入数据是numpy.float64,那么就不会有这个问题了。在

相关问题 更多 >