gpt4 book ai didi

numpy - 为什么我的 Series.prod() 为 0?

转载 作者:行者123 更新时间:2023-12-04 04:43:44 24 4
gpt4 key购买 nike

我有一个由正数或nan组成的系列。但是当我计算乘积时,我得到 0。

示例输出:

    In [14]: pricerelatives.mean()
Out[14]: 0.99110019490541013
In [15]: pricerelatives.prod()
Out[15]: 0.0
In [16]: len(pricerelatives)
Out[16]: 362698
In [17]: (pricerelatives>0).sum()
Out[17]: 223522
In [18]: (pricerelatives.isnull()).sum()
Out[18]: 139176
In [19]: 223522+139176
Out[19]: 362698

为什么我得到 0 pricerelatives.prod() ?

更新:
感谢您的快速回复。不幸的是,它不起作用:
    In [32]: import operator
In [33]: from functools import reduce
In [34]: lst = list(pricerelatives.fillna(1))
In [35]: the_prod = reduce(operator.mul, lst)
In [36]: the_prod
Out[36]: 0.0

显式摆脱空值也失败:
    In [37]: pricerelatives[pricerelatives.notnull()].prod()
Out[37]: 0.0

更新 2:
确实,这正是我刚刚做的并且将要添加的。
    In [39]: pricerelatives.describe()
Out[39]:
count 223522.000000
mean 0.991100
std 0.088478
min 0.116398
25% 1.000000
50% 1.000000
75% 1.000000
max 11.062591
dtype: float64

更新 3:对我来说仍然很奇怪。所以更详细的信息:
    In [46]: pricerelatives[pricerelatives<1].describe()
Out[46]:
count 50160.000000
mean 0.922993
std 0.083865
min 0.116398
25% 0.894997
50% 0.951488
75% 0.982058
max 1.000000
dtype: float64

更新 4:该比率正好在您示例的 0 和 > 0 之间的截止值附近,但我的数字比统一的 0,1 和统一的 1,2 更集中在 1 附近。
    In [52]: 50160./223522
Out[52]: 0.2244074408783028
In [53]: pricerelatives[pricerelatives>=1].describe()
Out[53]:
count 173362.000000
mean 1.010806
std 0.079548
min 1.000000
25% 1.000000
50% 1.000000
75% 1.000000
max 11.062591
dtype: float64
In [54]: pricerelatives[pricerelatives<1].prod()
Out[54]: 0.0

最佳答案

这看起来像是 numpy 中的“错误” ;见 here .溢出时它不会升高。

以下是一些示例:

In [26]: prod(poisson(10, size=30))
Out[26]: -2043494819862020096

In [46]: prod(randn(10000))
Out[46]: 0.0

您必须使用 long (Python 2) 或 int (Python 3) 输入并使用 reduce 减少它/ functools.reduce :
import operator
from functools import reduce

lst = list(pricerelatives.dropna())
the_prod = reduce(operator.mul, lst)

编辑:删除所有 NaN 会更快s 然后计算乘积而不是先将它们设置为 1。

非常非正式地,您仍然得到零的原因是乘积会更快地接近零,因为 [0, 1) 中的值数量的比率值 >= 1 增长。
def nnz_ratio(ratio, size=1000):
n1 = ratio * size
n2 = size - n1
s1 = uniform(1, 2, size=n1)
s2 = uniform(0, 1, size=n2)
return Series(hstack((s1, s2)))

ratios = linspace(0.01, 1, 25)
ss = empty(len(ratios))

for i, ratio in enumerate(ratios):
ss[i] = nnz_ratio(ratio).prod()

ss

给出:
array([  0.0000e+000,   0.0000e+000,   0.0000e+000,   0.0000e+000,
0.0000e+000, 3.6846e-296, 2.6969e-280, 1.2799e-233,
2.0497e-237, 4.9666e-209, 6.5059e-181, 9.8479e-171,
7.7879e-125, 8.2696e-109, 9.3416e-087, 4.1574e-064,
3.9266e-036, 4.1065e+004, 6.6814e+018, 7.1501e+040,
6.2192e+070, 1.3523e+093, 1.0739e+110, 1.5646e+144,
8.6361e+163])

编辑 #2:

如果您正在计算几何平均值,请使用
from scipy.stats import gmean

gm = gmean(pricerelatives.dropna())

关于numpy - 为什么我的 Series.prod() 为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18513071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com