【发布时间】:2019-06-21 11:27:49
【问题描述】:
我找到了关于如何enter link description here 的说明 我需要根据 Kolmogorov-Smirnov 检验在我的样本和每个分布之间比较我的分布以使其适合。但我不知道如何解释结果并根据此测试选择最佳分布? 此代码未实现 Kolmogorov-Smirnov 测试。所以 1 - 如何实现 kolmogorov-smirnov 测试? 2 - 如何选择最佳分布?
def best_fit_distribution(data, bins=200, ax=None):
"""Model data by finding best fit distribution to data"""
# Get histogram of original data
y, x = np.histogram(data, bins=bins, density=True)
x = (x + np.roll(x, -1))[:-1] / 2.0
# Distributions to check
DISTRIBUTIONS = [st.alpha, st.anglit]
# Best holders
best_distribution = st.norm
best_params = (0.0, 1.0)
best_sse = np.inf
runs = []
# Estimate distribution parameters from data
for distribution in DISTRIBUTIONS:
# Try to fit the distribution
try:
# Ignore warnings from data that can't be fit
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
# fit dist to data
params = distribution.fit(data)
print(params)
# Separate parts of parameters
arg = params[:-2]
print(arg)
loc = params[-2]
print(loc)
scale = params[-1]
print(scale)
# Calculate fitted PDF and error with fit in distribution
pdf = distribution.pdf(x, loc=loc, scale=scale, *arg)
sse = np.sum(np.power(y - pdf, 2.0))
# if axis pass in add to plot
try:
if ax:
pd.Series(pdf, x).plot(ax=ax)
end
except Exception:
pass
runs.append([distribution.name, sse])
# identify if this distribution is better
if best_sse > sse > 0:
best_distribution = distribution
best_params = params
best_sse = sse
except Exception:
pass
print(runs)
return (best_distribution.name, best_params)
【问题讨论】:
-
首先,非常感谢您的帮助。其次,我在这里找到了代码stackoverflow.com/questions/6620471/…,因为我关注的是同一个论坛。另外,我需要用 python 计算这个测试
-
@pjs 我编辑我的问题。我很抱歉。我不小心。
标签: python machine-learning scipy statistics distribution