scipy 如何绘制标准差

ybzsozfc  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(175)

最近我开始学习概率和统计学,我试图绘制标准差,如68-95-99.7规则。
生成绘图的代码:

import numpy as np
from scipy.stats import norm

import matplotlib.pyplot as plt
import seaborn as sns

# Line width: Maximum 130 characters in the output, post which it will continue in next line.

np.set_printoptions(linewidth=130)

sns.set_context("paper", font_scale=1.5)

# Distribution

X = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9]

mean = np.mean(X)
var = np.var(X)
std = np.std(X)

print("Mean:", mean)
print("Variance:", var)
print("Standard Deviation:", std)

"""
Mean: 5.0
Variance: 4.0
Standard Deviation: 2.0
"""

plt.figure(figsize=(10, 5))

ax = sns.kdeplot(X, shade=True)

# Plot 1-std

x = np.linspace(mean - std, mean + std)
y = norm.pdf(x, mean, std)
ax.fill_between(x, y, alpha=0.5)

plt.xlabel("Random variable X")
plt.ylabel("Probability Density Function")
plt.xticks(ticks=range(0, 10))
plt.grid()

plt.show()

这段代码生成了下图:

问题:
1.在代码中绘制平均值的1个标准差有什么问题?
1.我不明白为什么在kde图上方有一个小峰?
1.如何绘制1-标准品、2-标准品和3-标准品?

1tu0hz3e

1tu0hz3e1#

1.代码中没有错误:mean是5,std是2,因此您要对5 - 2 = 35 + 2 = 7之间的区域进行着色。
1.在kde图中有一个小峰,因为它是您使用X给予的数据分布的表示,而实际上,X不是正态分布。您可以使用真正态分布来检查这一点:

mean = 5
std = 2
X = np.random.randn(10000)
X = (X - X.mean())/X.std()*std + mean

1.你可以用for循环在i上绘制其他标准差。x1是左侧,x2是中间部分(然后设置为np.nan),最后x3是分布的右侧。然后你必须设置要排除的区域为np.nan(对应于x2):

N = 10
for i in [1, 2, 3]:
    x1 = np.linspace(mean - i*std, mean - (i - 1)*std, N)
    x2 = np.linspace(mean - (i - 1)*std, mean + (i - 1)*std, N)
    x3 = np.linspace(mean + (i - 1)*std, mean + i*std, N)
    x = np.concatenate((x1, x2, x3))
    x = np.where((mean - (i - 1)*std < x) & (x < mean + (i - 1)*std), np.nan, x)
    y = norm.pdf(x, mean, std)
    ax.fill_between(x, y, alpha=0.5)

完整代码

import numpy as np
from scipy.stats import norm

import matplotlib.pyplot as plt
import seaborn as sns

# Line width: Maximum 130 characters in the output, post which it will continue in next line.

np.set_printoptions(linewidth=130)

sns.set_context("paper", font_scale=1.5)

# Distribution

X = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9]

mean = np.mean(X)
var = np.var(X)
std = np.std(X)

print("Mean:", mean)
print("Variance:", var)
print("Standard Deviation:", std)

"""
Mean: 5.0
Variance: 4.0
Standard Deviation: 2.0
"""

plt.figure(figsize=(10, 5))

ax = sns.kdeplot(X, shade=True)

N = 10
for i in [1, 2, 3]:
    x1 = np.linspace(mean - i*std, mean - (i - 1)*std, N)
    x2 = np.linspace(mean - (i - 1)*std, mean + (i - 1)*std, N)
    x3 = np.linspace(mean + (i - 1)*std, mean + i*std, N)
    x = np.concatenate((x1, x2, x3))
    x = np.where((mean - (i - 1)*std < x) & (x < mean + (i - 1)*std), np.nan, x)
    y = norm.pdf(x, mean, std)
    ax.fill_between(x, y, alpha=0.5)

plt.xlabel("Random variable X")
plt.ylabel("Probability Density Function")
plt.xticks(ticks=range(0, 10))
plt.grid()

plt.show()

地块

相关问题