python数据分布检验

1、读取数据

2、查看数据基本特征

3、绘制图形

在直方图的基础上画一个真正的正态分布的图与绘制QQ图

plt.hist(log_returns.flatten(),bins = 70,normed=True)
x = np.linspace(plt.axis()[0],plt.axis()[1])
plt.plot(x, scs.norm.pdf(x,loc = r/M,scale = sigma/np.sqrt(M)),'r',lw = 2)
plt.show()
sm.qqplot(log_returns.flatten()[::500],line='s')
plt.show()
DataFrame.hist(bins=10)
#Make a histogram of the DataFrame.

5、检验是否符合正态

u = pd.Series(x.tolist()).mean()
std = pd.Series(x.tolist()).std()
kstest(pd.Series(x.tolist()), 'norm',(u,std))
from scipy.stats import shapiro
shapiro(pd.Series(x.tolist()))
from scipy.stats import anderson
anderson(pd.Series(x.tolist()), dist ='norm')
from scipy.stats import normaltest
normaltest(pd.Series(x.tolist()),axis=0)

  这个正态分布的假设检验的零假设当然就是分布是正态分布的。结果我们发现,p-value很大,所以我们不能拒绝原假设。当然,这一块逻辑是存在一定缺陷的,也就是说,我们不能拒绝原假设,但是统计意义是,其实我们并不能直接接受这就是正态分布这一零假设了,所以通常我们都需要配上QQ图来说明。

6、绘制箱形图,观察是否存在异常点

7、将数据转化成正态分布数据

7.1线性变化

  一旦我们收集到变量的样本数据,我们就可以对样本进行线性变化,并计算Z得分:

  1. 计算平均值
  2. 计算标准偏差
  3. 对于每个 x,使用以下方法计算 Z:

7.2使用Boxcox变换

  我们可以使用 SciPy 包将数据转换为正态分布:

scipy.stats.boxcox(x, lmbda=None, alpha=None)
import pandas as pd
import numpy as np
import seaborn as sns
df = pd.read_excel(r"非正态数据集.xlsx")
sns.distplot(df["price"],color = "#D86457")
from scipy import stats
stats.boxcox_normmax(df["price"])
x = stats.boxcox(df["price"],stats.boxcox_normmax(df["price"]))
sns.distplot(x,color = "#D86457")
fig = plt.figure()
ax = fig.add_subplot(111)
stats.boxcox_normplot(df["price"], -20, 20,plot = ax)
plt.axvline(x = stats.boxcox_normmax(df["price"]),color = "#D86457")
plt.show()
 

7.3使用 Yeo-Johnson 变换

sklearn.preprocessing.PowerTransformer(method=’yeojohnson’,standardize=True, copy=True)
原文地址:https://www.cnblogs.com/xxupup/p/13260458.html