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

问题描述

我有一个列名 df['Air temperature'] (datatype-float64)

我想将此列转换为正态分布,以便我可以使用英制规则来找到 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()

我尝试了这段代码,但我得到了 [预期的二维数组,而是得到了 1D 数组:错误] 即使我应用了重塑操作,我仍然收到错误。请建议我任何将数据转换为正态分布或标准正态分布的方法并找到 CI

解决方法

我会使用类似 This 的答案来拟合数据的高斯(正态分布)曲线,然后使用生成的分布与 scipy.stats 方法 .interval(0.95) (here) 来给出包含 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,sigma))
plt.show()

输出:

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

Plot