给定分布的名称和参数,求出均值和标准差

时间:2019-07-15 04:45:51

标签: python python-3.x scipy distribution

我使用代码

scipy.stats的虹膜数据集中生成了以下内容
import scipy.stats as st
def get_best_distribution(data):
    dist_names = ["norm", "exponweib", "weibull_max", "weibull_min", "pareto", "genextreme"]
    dist_results = []
    params = {}
    for dist_name in dist_names:
        dist = getattr(st, dist_name)
        param = dist.fit(data)

        params[dist_name] = param
        # Applying the Kolmogorov-Smirnov test
        D, p = st.kstest(data, dist_name, args=param)
        print("p value for "+dist_name+" = "+str(p))
        dist_results.append((dist_name, p))

    # select the best fitted distribution
    best_dist, best_p = (max(dist_results, key=lambda item: item[1]))
    # store the name of the best fit and its p value

    print("Best fitting distribution: "+str(best_dist))
    print("Best p value: "+ str(best_p))
    print("Parameters for the best fit: "+ str(params[best_dist]))

    return best_dist, best_p, params[best_dist]

How to find probability distribution and parameters for real data? (Python 3)获得:

Best fitting distribution: invgauss
Best p value: 0.8268700800511397
Parameters for the best fit: (0.016421213754032188, 1.5064355144322001, 309.4166651914064)

best_result = {"virginica": {"distribution": "invgauss", "parameters": [0.016421213754032188, 1.5064355144322001, 309.4166651914064]}}

我现在想从best_result获得平均值和标准偏差(分别为方差)。在Distribution mean and standard deviation using scipy.stats处查找了类似的内容,但无法弄清楚如何使用SciPy进行处理。

一些见解将不胜感激!

1 个答案:

答案 0 :(得分:1)

保存分发对象,而不是保存分发的名称。为此,请更改

        dist_results.append((dist_name, p))

        dist_results.append((dist, p))

然后将函数中的三个打印语句和return语句更改为

    print("Best fitting distribution:", best_dist.name)
    print("Best p value: "+ str(best_p))
    print("Parameters for the best fit:", params[best_dist.name])

    return best_dist, best_p, params[best_dist.name]

然后您可以执行以下操作:

dist, p, par = get_best_distribution(data)

print("mean:", dist.mean(*par))
print("std: ", dist.std(*par))