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



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

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)

谢谢

使用以下修改解决了问题。

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]

最新更新