python中计算标准法线时的scipy stats错误

2024-06-07 03:49:48 发布

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

我试图在python中计算数据df在正态分布下的概率。我对python和编程没有经验。我从这个网站上抓取的以下用户定义函数有效,scipy函数不工作。。。在

自定义项:

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不起作用:

^{pr2}$

它返回以下错误

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"]

不知道这是否会影响它。在


Tags: pyselfdfvaluelibpackagesstatssite
1条回答
网友
1楼 · 发布于 2024-06-07 03:49:48

不确定生存函数是否是你需要的。我相信你要找的是scipy的pdf函数,特别是普通随机变量的pdf。我用你用的自定义函数测试了它。在

>>> from scipy.stats import norm
>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame({'x': [0.6, 0.5, 0.13], 'mu': [0, 1, 1], 'std': [1, 2, 1]})
>>> norm.pdf(df['x'], df['mu'], df['std'])
array([ 0.3332246 ,  0.19333406,  0.27324443])
>>> def normal(x,mu,sigma):
...     return ( 2.*np.pi*sigma**2. )**-.5 * np.exp( -.5 * (x-mu)**2. / sigma**2. )
...
>>> normal(df['x'], df['mu'], df['std'])
0    0.333225
1    0.193334
2    0.273244
dtype: float64

请注意,如果您的mustd列是np.nan,那么您将得到运行时警告,但仍然会得到一个输出,类似于自定义函数。在

^{pr2}$

如果将np.nan值设置为None,则可以避免出现警告:

>>> df = pd.DataFrame({'x': [0.6, 0.5, 0.13], 'mu': [None, 1, 1], 'std': [None, 2, None]})
>>> normal(df['x'], df['mu'], df['std'])
0         NaN
1    0.193334
2         NaN
dtype: float64
>>> norm.pdf(df['x'], df['mu'], df['std'])
array([        nan,  0.19333406,         nan])

注意,我要么删除meanreturnsdreturn值为NaN的行。否则,我将假设您正在寻找x的概率,假设一个标准正态分布,然后您必须将meanreturnNaN值设置为0,sdreturnNaN值设为1。在

最后一点要补充的一点是,如果数据帧的所有行都采用标准正态分布来计算pdf中的概率,那么就不需要传递mu列和{}列。norm.pdf已经假定一个标准法线。在这种情况下,您可以这样运行代码:

>>> norm.pdf(df['x'])
array([ 0.3332246 ,  0.35206533,  0.39558542])

相关问题 更多 >