【发布时间】:2021-03-05 23:17:45
【问题描述】:
我正在尝试在离散分布之间选择最佳拟合,但我得到一个错误,即离散分布没有属性拟合。任何人都知道如何解决此问题或在离散分布之间进行选择的任何替代方法?
import scipy.stats as st
def get_best_distribution(data):
dist_names = ["bernoulli", "poisson", "binom", "geom", "logser", "randint"]
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]
print(get_best_distribution(df)
AttributeError: 'bernoulli_gen' object has no attribute 'fit'
【问题讨论】:
-
你可以构造一个Chi-squared goodness of fit test。
标签: python python-3.x scipy statistics distribution