python数据分布_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 importshapiro

shapiro(pd.Series(x.tolist()))

from scipy.stats importanderson

anderson(pd.Series(x.tolist()), dist ='norm')

from scipy.stats importnormaltest

normaltest(pd.Series(x.tolist()),axis=0)

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

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

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

7.1线性变化

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

计算平均值

计算标准偏差

对于每个 x,使用以下方法计算 Z:

1620

7.2使用Boxcox变换

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

scipy.stats.boxcox(x, lmbda=None, alpha=None)

importpandas as pd

importnumpy as np

importseaborn as sns

df = pd.read_excel(r"非正态数据集.xlsx")

sns.distplot(df["price"],color = "#D86457")

from scipy importstats

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值