如何将单个列转换为正态分布或高斯分布找到95%和99%的CI

2024-06-15 11:30:27 发布

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

我有一个列名为df[‘空气温度’](数据类型-float64)

我想将此列转换为正态分布,以便使用Imperic规则查找95,99%CI。 或任何其他方法也可以找到95%,995%的CI

enter image description here

zi=df['Air_temperature'] 
from sklearn.preprocessing import MinMaxScaler
min_max=MinMaxScaler()
df_minmax=pd.DataFrame(min_max.fit_transform(zi))
df_minmax.head()

我尝试了这段代码,但我得到了[Expected 2D array,Get 1D array It:error]即使我应用了重塑操作,我仍然得到了错误。请向我推荐任何将数据转换为正态分布或正态分布的方法&;查找CI


Tags: 方法cidfmin温度arraymax空气
1条回答
网友
1楼 · 发布于 2024-06-15 11:30:27

我将使用类似This的答案将高斯(正态分布)曲线拟合到数据,然后使用生成的分布和scipy.stats方法.interval(0.95){a2}给出包含95%CDF的端点

例如:

import pandas as pd
from scipy.stats import norm
import numpy as np
from matplotlib import pyplot as plt

normal = np.random.normal(size=1000)
noise = np.random.uniform(size=500, low=-2, high=2)
data = np.concatenate([normal, noise])   # some dummy data
# make it a DataFrame
df = pd.DataFrame(data=data, index=range(len(data)), columns=["data"])  
df.plot(kind="density")

########### YOU ARE HERE ###################

data = df.to_numpy()                              # Numpy arrays are easier for 1D data
mu, std = norm.fit(data)                          # Fit a normal distribution
print("Mu and Std: ", mu, std)

CI_95 = norm.interval(0.95, loc=mu, scale=std)    # Find the 95% CI endpoints
print("Confidence Interval: ", CI_95)

plt.vlines(CI_95, ymin=0, ymax=0.4)               # plotting stuff
x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
plt.plot(x, norm.pdf(x, mu, sigma))
plt.show()

输出:

Mu and Std:  -0.014830093874393395 1.0238114937847707
Confidence Interval:  (-2.0214637486506972, 1.9918035609019102)

Plot

相关问题 更多 >