我试图得到我所拥有的一组值的最佳拟合分布。我想出了下面的函数来实现这个功能。
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)
谢谢
发布于 2018-08-08 08:12:31
通过以下修改解决问题。
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]
https://stackoverflow.com/questions/51740306
复制相似问题