iT邦幫忙

0

[Python] show distribution


import seaborn as sns; sns.set()
from scipy import stats
from scipy.stats import norm ,skew

def my_boxcox(x):
    global boxcox_lambda
    xx,boxcox_lambda = stats.boxcox(x+0.0001)
  
    return xx

def my_boxcox_inv(x):
    global boxcox_lambda
    xx = special.inv_boxcox(x,boxcox_lambda)
  
    return xx
    
def show_dis(x):
    x_log = np.log1p(x )
    x_boxcox = my_boxcox(x)
        
    skew_x = skew(x)
    skew_log = skew(x_log)
    skew_boxcox = skew(x_boxcox)   
    
    plt.figure(figsize=(15,15))
    
    ax1= plt.subplot(631)
    ax1.set_title('Normal')
    sns.distplot(x, kde=True, fit=stats.norm ,ax = ax1)
    ax2= plt.subplot(632)
    ax2.set_title('Log Normal')
    sns.distplot(x, kde=True, fit=stats.lognorm ,ax = ax2)
    ax3 = plt.subplot(633)
    ax3.set_title('Johnson SU')
    sns.distplot(x, kde=True, fit=stats.johnsonsu ,ax = ax3)
    

    print("{}".format(x_log.isna().sum()))
    ax4= plt.subplot(634)
    ax4.set_title('Normal (x_log)')
    sns.distplot(x_log, kde=True, fit=stats.norm ,ax = ax4)
    ax5= plt.subplot(635)
    ax5.set_title('Log Normal (x_log)')
    sns.distplot(x_log, kde=True, fit=stats.lognorm ,ax = ax5)
    ax6 = plt.subplot(636)
    ax6.set_title('Johnson SU (x_log)')
    sns.distplot(x_log, kde=True, fit=stats.johnsonsu ,ax = ax6)
        

#    print("{}".format(x_boxcox.isnan().sum()))
    ax7= plt.subplot(637)
    ax7.set_title('Normal (x_boxcox)')
    sns.distplot(x_boxcox, kde=True, fit=stats.norm ,ax = ax7)
    ax8= plt.subplot(638)
    ax8.set_title('Log Normal (x_boxcox)')
    sns.distplot(x_boxcox, kde=True, fit=stats.lognorm ,ax = ax8)
    ax9 = plt.subplot(639)
    ax9.set_title('Johnson SU (x_boxcox)')
    sns.distplot(x_boxcox, kde=True, fit=stats.johnsonsu ,ax = ax9)

    (mu, sigma) = norm.fit(x)
  
    print('\nskew(x) ={} \nskew(x_log) = {}\nskew(x_boxcox) = {}'.format(skew_x,
                                                                         skew_log,
                                                                         skew_boxcox))
    #Get also the QQ-plot
    ax10= plt.subplot(614)
    res = stats.probplot(x, plot=ax10)
    
    ax11= plt.subplot(615)
    res = stats.probplot(x_log, plot=ax11)  
    
    ax12= plt.subplot(616)
    res = stats.probplot(x_boxcox, plot=ax12)  
    
    plt.tight_layout()

尚未有邦友留言

立即登入留言