Scipy KSTest TypeError:_parse_args()从3到5个位置参数,但给出了6个

时间:2018-08-08 06:55:22

标签: python scipy

我正在尝试为我拥有的一组值获得最佳拟合分布。我想出了以下功能来做到这一点。

def get_best_distribution(data):
    dist_names = [st.exponweib, st.weibull_max, st.weibull_min,st.pareto, st.genextreme]
    dist_results = []
    params = {}
    for dist_name in dist_names:
        dist = dist_name
        param = dist.fit(data)

        params[dist_name] = param
        # Applying the Kolmogorov-Smirnov test
        D, p = st.kstest(data, dist_name, args=param)
        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: "+st(best_dist))
    print("Best p value: "+ str(p))
    print("Parameters for the best fit: "+ str(params[best_dist]))

    return best_dist, best_p, params[best_dist]

根据Scipy文档,一切都应该没问题。但这会产生以下错误。

TypeError: _parse_args() takes from 3 to 5 positional arguments but 6 were given

这是什么原因造成的?

以下行会导致此错误。

D, p = st.kstest(data, dist_name, args=param)

谢谢

1 个答案:

答案 0 :(得分:0)

问题通过以下修改得以解决。

def get_best_distribution(data):
    dist_names = ["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(p))
    print("Parameters for the best fit: "+ str(params[best_dist]))

    return best_dist, best_p, params[best_dist]