【发布时间】:2013-09-02 00:17:50
【问题描述】:
我有一个由正数或 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
为什么pricerelatives.prod() 得到 0?
更新: 感谢您及时的回复。不幸的是,它不起作用:
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
【问题讨论】:
-
你能显示
pricerelatives.describe()吗? -
您能否在更新 3 中显示
>=1而不是>1? -
另外,计算值
-
为什么更新 4 中的第 54 行没有回答您的问题?
-
我想是的。非常感谢!顺便说一句,我找到了一种计算方法。我正在计算产品,以便获得几何平均值。与其先拿产品,不如先做**1/n解决问题。
标签: numpy pandas precision multiplication series