最近,我开始为数据科学学习Probability and Statistics.我试图为下面的分布X绘制Standard Deviation,如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()

This code is generating below plot: Plot

问题:

  1. 从平均值绘制1 std的代码有什么错误?
  2. 我不明白为什么kde号地块上方会有一个小峰?
  3. 如何绘制1-std、2-std和3-std?

推荐答案

  1. 代码中没有错误:平均值为5,标准值为2,所以阴影区域介于5 - 2 = 35 + 2 = 7之间.

  2. kde图中有一个小的峰值,因为它是用X表示的数据分布,实际上,X不是正态分布.您可以使用真实的正态分布来判断这一点:

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

    enter image description here

  3. 您可以使用大于i的for循环绘制其他标准折旧.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()

情节

enter image description here

Python相关问答推荐

比较两个数据帧并并排附加结果(获取性能警告)

Vectorize多个头寸的止盈/止盈回溯测试pythonpandas

使用setuptools pyproject.toml和自定义目录树构建PyPi包

为一个组的每个子组绘制,

Python脚本使用蓝牙运行在Windows 11与raspberry pi4

如何使regex代码只适用于空的目标单元格

Geopandas未返回正确的缓冲区(单位:米)

如何排除prefecture_related中查询集为空的实例?

如何防止Pandas将索引标为周期?

找到相对于列表索引的当前最大值列表""

OpenCV轮廓.很难找到给定图像的所需轮廓

Python Mercury离线安装

如何用FFT确定频变幅值

如何设置nan值为numpy数组多条件

上传文件并使用Panda打开时的Flask 问题

通过对列的其余部分进行采样,在Polars DataFrame中填充_null`?

对包含JSON列的DataFrame进行分组

如何使用Polars从AWS S3读取镶木地板文件

正则表达式反向查找

极地数据帧:ROLING_SUM向前看