【发布时间】:2018-02-01 08:26:25
【问题描述】:
我试图在 python 中为我的数据df 计算正态分布下的概率。我对python或编程没有经验。我从本网站抓取的以下用户定义函数有效,scipy 函数无效...
UDF:
def normal(x,mu,sigma):
return ( 2.*np.pi*sigma**2. )**-.5 * np.exp( -.5 * (x-mu)**2. / sigma**2. )
df["normprob"] = normal(df["return"],df["meanreturn"],df["sdreturn"])
这个 scipy 函数不起作用:
df["normdistprob"] = scip.norm.sf(df["return"],df["meanreturn"],df["sdreturn"])
它会返回以下错误
C:\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:1815: RuntimeWarning: invalid value encountered in true_divide
x = np.asarray((x - loc)/scale, dtype=dtyp)
C:\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:1816: RuntimeWarning: invalid value encountered in greater
cond0 = self._argcheck(*args) & (scale > 0)
C:\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in greater
return (self.a < x) & (x < self.b)
C:\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:879: RuntimeWarning: invalid value encountered in less
return (self.a < x) & (x < self.b)
C:\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:1817: RuntimeWarning: invalid value encountered in greater
cond1 = self._open_support_mask(x) & (scale > 0)
C:\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:1818: RuntimeWarning: invalid value encountered in less_equal
cond2 = cond0 & (x <= self.a)
感谢任何建议。还要注意,前 20 个单元格的
df["meanreturn"]
不适用,不确定是否会影响它。
【问题讨论】:
-
是的,在任何数学计算中使用 NA 都会使其崩溃
-
如果平均值为 NA,您计算概率的预期方法是什么?
-
好吧,我想即使它是前 20 个单元格,也不会影响数据集的其余部分,并且 'df["normdist"]' 的前 20 个单元格只是 NaN以及。另外,从这个链接stackoverflow.com/questions/25039328/…,似乎 NaN 单元格无关紧要?