0

我需要通过模拟的直方图拟合实验直方图(以确定最适合的模拟直方图的几个参数)。我已经尝试了 scipy.optimize 中的 curve_fit,但在这种情况下它不起作用:返回错误“...不是 python 函数”。是否可以在 scipy 或其他 python 模块中自动执行此操作?如果没有,请您提供一些可能的算法的链接来自己调整它们吗?

4

1 回答 1

0

根据您所说,我认为以下内容应该会有所帮助,您似乎试图以错误的方式使用curve_fit:

您需要定义您想要拟合的分布。例如,如果我有一些看起来呈正态分布的数据,并且我想知道效果如何,以及哪些参数最适合,我会执行以下操作:

import numpy as np
import pylab as plt
from scipy.optimize import curve_fit

# Create fake data and run it though `histogram` to get the experimental distribution
experimental = np.random.normal(10.0, 0.4, size=10000)
n, bins = plt.histogram(experimental, bins=100, normed=True)

# This just gives the mid points of the bins, many different (and better) ways exist to
# do this I'm sure
bins_mid_points = (0.5*(bins + np.roll(bins, 1)))[1:]

# Define the normal distribution as a function
def normal(x, sigma, mu):
    return np.exp(-(x - mu)**2 / (2 * sigma**2)) / (sigma * np.sqrt(2*np.pi))

# Fit the experimental data, 
popt, pcov = curve_fit(normal, xdata=bins_mid_points, ydata=n)

# Plot both 
plt.bar(bins[:-1], n, width=np.diff(bins))
plt.plot(bins_mid_points, normal(bins_mid_points, *popt), color="r", lw=3)

红线显示了我们的模拟拟合,如果需要,您还可以将其绘制为直方图。的输出popt给出了一个最适合数据的数组,[sigma, mu]同时pcov可用于确定拟合的好坏。

请注意,我对这里的数据进行了归一化histogram是因为我定义的函数是正态分布

您需要仔细考虑您期望的分布以及您希望从中获得的统计数据。

于 2013-09-10T13:14:03.180 回答