python实现抽样分布描述

本次使用木东居士提供数据案例,验证数据分布等内容,参考链接:https://www.jianshu.com/p/6522cd0f4278,先感谢上面两位。

只贴了代码。。。结果图片没得了

#数据读取
df = pd.read_excel('C://Users//zxy//Desktop//data.xlsx',usecols = [1,2,3])


1.按照港口分类,计算各类港口数据 年龄、车票价格的统计量。
df1 = df.groupby(['Embarked'])
df1.describe()

或
# 变异系数 = 标准差/平均值
def cv(data):
    return data.std()/data.var()
df2 = df.groupby(['Embarked']).agg(['count','min','max','median','mean','var','std',cv])
df2 = df2.apply(lambda x:round(x,2))
df2_age = df2['Age']
df2_fare = df2['Fare']

# 2、画出价格的分布图像,验证数据服从何种分布
# 2.1 船票直方图:
plt.hist(df['Fare'],20,normed=1,alpha=0.75)
plt.title('Fare')
plt.grid(True)

#分别用kstest、shapiro、normaltest来验证分布系数
ks_test = stats.kstest(df['Fare'], 'norm')
shapiro_test = stats.shapiro(df['Fare'])
normaltest_test = stats.normaltest(df['Fare'],axis=0) 
#以上三种检测结果表明 p<5%,因此 船票数据不符合正态分布。


# 绘制拟合正态分布曲线:
fare = df['Fare']

plt.figure()
fare.plot(kind = 'kde')      #原始数据的正态分布

M_S = stats.norm.fit(fare)   #正态分布拟合的平均值loc,标准差 scale
normalDistribution = stats.norm(M_S[0], M_S[1])    # 绘制拟合的正态分布图
x = np.linspace(normalDistribution.ppf(0.01), normalDistribution.ppf(0.99), 100)
plt.plot(x, normalDistribution.pdf(x), c='orange')
plt.xlabel('Fare about Titanic')
plt.title('Titanic[Fare] on NormalDistribution', size=20)
plt.legend(['Origin', 'NormDistribution'])


# 验证是否符合T分布
T_S = stats.t.fit(fare)
df = T_S[0] 
loc = T_S[1] 
scale = T_S[2] 
x2 = stats.t.rvs(df=df, loc=loc, scale=scale, size=len(fare))
D, p = stats.ks_2samp(fare, x2)
#p < alpha,拒绝原假设,价格数据不符合t分布。

# 对票价数据进行T分布拟合:
plt.figure()
fare.plot(kind = 'kde') 
TDistribution = stats.t(T_S[0], T_S[1],T_S[2])    # 绘制拟合的T分布图
x = np.linspace(TDistribution.ppf(0.01), TDistribution.ppf(0.99), 100)
plt.plot(x, TDistribution.pdf(x), c='orange')
plt.xlabel('Fare about Titanic')
plt.title('Titanic[Fare] on TDistribution', size=20)
plt.legend(['Origin', 'TDistribution'])

# 验证是否符合卡方分布?
chi_S = stats.chi2.fit(fare)
df_chi = chi_S[0] 
loc_chi = chi_S[1] 
scale_chi = chi_S[2] 
x2 = stats.chi2.rvs(df=df_chi, loc=loc_chi, scale=scale_chi, size=len(fare))
Dk, pk = stats.ks_2samp(fare, x2)#不符合

#对票价数据进行卡方分布拟合
plt.figure()
fare.plot(kind = 'kde') 
chiDistribution = stats.chi2(chi_S[0], chi_S[1],chi_S[2])    # 绘制拟合的正态分布图
x = np.linspace(chiDistribution.ppf(0.01), chiDistribution.ppf(0.99), 100)
plt.plot(x, chiDistribution.pdf(x), c='orange')
plt.xlabel('Fare about Titanic')
plt.title('Titanic[Fare] on chi-square_Distribution', size=20)
plt.legend(['Origin', 'chi-square_Distribution'])

# 按照港口分类,验证S与Q两个港口间的价格之差是否服从某种分布
S_fare = df[df['Embarked'] == 'S']['Fare']
Q_fare = df[df['Embarked'] =='Q']['Fare']
C_fare = df[df['Embarked'] =='C']['Fare']
S_fare.describe()

# 按照港口分类后,S港口样本数<=554,Q港口样本数<=28,C港口样本数<=130。
# 总体不服从正态分布,所以需要当n比较大时,一般要求n>=30,两个样本均值之差的抽样分布可近似为正态分布。
# X2的总体容量为28,其样本容量不可能超过30,故其S港和Q港两个样本均值之差(E(X1)-E(X2))的抽样分布不服从正态分布。
# S港和C港两个样本均值之差(E(X1)-E(X3))的抽样分布近似服从正态分布,
# 其均值和方差分别为E(E(X1) - E(X3)) = E(E(X1)) - E(E(X3)) = μ1 - μ3;D(E(X1) + E(X3)) = D(E(X1)) + D(E(X3)) = σ1²/n1 + σ3²/n3 。

miu = np.mean(S_fare) - np.mean(C_fare)
sig = np.sqrt(np.var(S_fare, ddof=1)/len(S_fare) + np.var(C_fare, ddof=1)/len(C_fare))

x = np.arange(- 110, 50)
y = stats.norm.pdf(x, miu, sig)
plt.plot(x, y)
plt.xlabel("S_Fare - C_Fare")
plt.ylabel("Density")
plt.title('Fare difference between S and C')
plt.show()

  


猜你喜欢

转载自www.cnblogs.com/zym-yc/p/11444065.html