计算数组的第三个标准差。

16

假设我有一个数组:

import numpy as np

x = np.array([0, 1, 2, 5, 6, 7, 8, 8, 8, 10, 29, 32, 45])
如何计算第三个标准差,以便我可以得到如下图片中所示的+3sigma的值?通常,我使用std = np.std(x),但说实话,我不知道它返回的是1sigma值还是2sigma值或其他值。感谢你的帮助。提前致谢。

6
你查看过文档了吗:http://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html,我的理解是简单地使用`x.mean() + 3 * x.std()`就可以得到你想要的结果。 - EdChum
2个回答

24

NumPy的std函数返回标准差,通常用“sigma”表示。要获取2-sigma或3-sigma范围,只需将sigma乘以2或3即可:

print [x.mean() - 3 * x.std(), x.mean() + 3 * x.std()]

输出:

[-27.545797458510656, 52.315028227741429]

如需更详细的信息,您可以参考文档,其中写道:

标准差是平均值与每个数值偏差的平方和的平均值的平方根,即 std = sqrt(mean(abs(x - x.mean())**2))。

http://docs.scipy.org/doc/numpy/reference/generated/numpy.std.html


0

对于任何偶然发现此内容的人,请使用类似以下的代码——它会给你一个圆形柱,其中标准差基于另一列的值:

# get percentile and which standard deviation for daily decline pct change
def which_std_dev(row,df,col):
    std_1 = round(df[col].mean() + 1 * df[col].std(),0)
    std_2 = round(df[col].mean() + 2 * df[col].std(),0)
    std_3 = round(df[col].mean() + 3 * df[col].std(),0)
    std_4 = round(df[col].mean() + 4 * df[col].std(),0)
    std_5 = round(df[col].mean() + 5 * df[col].std(),0)
    std_6 = round(df[col].mean() + 6 * df[col].std(),0)
    std_7 = round(df[col].mean() + 7 * df[col].std(),0)
    std_8 = round(df[col].mean() + 8 * df[col].std(),0)
    std_9 = round(df[col].mean() + 9 * df[col].std(),0)
    std_10 = round(df[col].mean() + 10 * df[col].std(),0)

    if row[col] <= std_1:
        return 1
    elif row[col] > std_1 and row[col] < std_2:
        return 2
    elif row[col] >= std_2 and row[col] < std_3:
        return 3
    elif row[col] >= std_3 and row[col] < std_4:
        return 4
    elif row[col] >= std_4 and row[col] < std_5:
        return 5
    elif row[col] >= std_6 and row[col] < std_6:
        return 6
    elif row[col] >= std_7 and row[col] < std_7:
        return 7
    elif row[col] >= std_8 and row[col] < std_8:
        return 8
    elif row[col] >= std_9 and row[col] < std_9:
        return 9
    else:
        return 10

df_day['percentile'] = round(df_day['daily_decline_pct_change'].rank(pct=True),3)
df_day['which_std_dev'] = df_day.apply(lambda row: which_std_dev(row,df_day,'daily_decline_pct_change'), axis = 1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接