【问题标题】:howto get fit parameters from seaborn distplot fit=?如何从 seaborn distplot fit= 获取拟合参数?
【发布时间】:2015-10-03 12:31:11
【问题描述】:

我正在使用 seaborn distplot (data, fit=stats.gamma)

如何获取返回的拟合参数?

这是一个例子:

import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
df = pd.read_csv ('RequestSize.csv')
import matplotlib.pyplot as plt
reqs = df['12 web pages']
reqs = reqs.dropna()
reqs = reqs[np.logical_and (reqs > np.percentile (reqs, 0), reqs < np.percentile (reqs, 95))]
dist = sns.distplot (reqs, fit=stats.gamma)

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    我确认以上是真的 - sns.distplot fit 方法等效于 scipy.stats 中的 fit 方法,因此您可以从那里获取参数,例如:

    from scipy import stats
    
    ax = sns.distplot(e_t_hat, bins=20, kde=False, fit=stats.norm);
    plt.title('Distribution of Cointegrating Spread for Brent and Gasoil')
    
    # Get the fitted parameters used by sns
    (mu, sigma) = stats.norm.fit(e_t_hat)
    print "mu={0}, sigma={1}".format(mu, sigma)
    
    # Legend and labels 
    plt.legend(["normal dist. fit ($\mu=${0:.2g}, $\sigma=${1:.2f})".format(mu, sigma)])
    plt.ylabel('Frequency')
    
    # Cross-check this is indeed the case - should be overlaid over black curve
    x_dummy = np.linspace(stats.norm.ppf(0.01), stats.norm.ppf(0.99), 100)
    ax.plot(x_dummy, stats.norm.pdf(x_dummy, mu, sigma))
    plt.legend(["normal dist. fit ($\mu=${0:.2g}, $\sigma=${1:.2f})".format(mu, sigma),
               "cross-check"])
    

    【讨论】:

    • 这是一个正态分布。 OP正在使用伽玛。你知道如何解释 stats.gamma.fit() 返回的三个值吗?
    • @ChrisJ.Vargo : 形状、位置、比例 = stats.gamma.fit(data)
    【解决方案2】:

    使用你传递给distplot的对象:

    stats.gamma.fit(reqs)
    

    【讨论】:

      猜你喜欢
      • 2021-05-24
      • 2018-05-30
      • 1970-01-01
      • 2019-08-03
      • 2020-06-16
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      相关资源
      最近更新 更多