指定具有未知系数的函数以及数据并在python中查找系数

时间:2018-04-28 06:27:20

标签: python scipy curve-fitting minimize mse

我有一个函数:f(theta)= a + b * cos(theta -c)以及采样数据。我想找到最小化均方误差的系数a,b和c。知道在python中是否有一种有效的方法吗?

编辑:

import numpy as np
from scipy.optimize import curve_fit

#definition of the function 
def myfunc(x, a, b, c):
    return a + b * np.cos(x - c)

#sample data
x_data = [0, 60, 120, 180, 240, 300]
y_data = [25, 40, 70, 30, 10, 15]

#the actual curve fitting procedure, a, b, c are stored in popt
popt, _pcov = curve_fit(myfunc, x_data, y_data)
print(popt)
print(np.degrees(popt[2]))

#the rest is just a graphic representation of the data points and the fitted curve
from matplotlib import pyplot as plt

#x_fit = np.linspace(-1, 6, 1000)
y_fit = myfunc(x_data, *popt)

plt.plot(x_data, y_data, "ro")
plt.plot(x_data, y_fit, "b")
plt.xlabel(r'$\theta$ (degrees)');
plt.ylabel(r'$f(\theta)$');

plt.legend()
plt.show()

这是一张图片,显示曲线如何真正适合这些点。似乎幅度应该更高。当地的分钟和最大值似乎在正确的位置。

enter image description here

1 个答案:

答案 0 :(得分:0)

scipy.optimize.curve_fit使数据点非常容易适应您的自定义函数:

import numpy as np
from scipy.optimize import curve_fit

#definition of the function 
def myfunc(x, a, b, c):
    return a + b * np.cos(x - c)

#sample data
x_data = np.arange(5)
y_data = 2.34 + 1.23 * np.cos(x_data + .23)

#the actual curve fitting procedure, a, b, c are stored in popt
popt, _pcov = curve_fit(myfunc, x_data, y_data)
print(popt)

#the rest is just a graphic representation of the data points and the fitted curve
from matplotlib import pyplot as plt

x_fit = np.linspace(-1, 6, 1000)
y_fit = myfunc(x_fit, *popt)

plt.plot(x_data, y_data, "ro", label = "data points")
plt.plot(x_fit, y_fit, "b", label = "fitted curve\na = {}\nb = {}\nc = {}".format(*popt))

plt.legend()
plt.show()

输出:

[ 2.34  1.23 -0.23]

修改:

您的问题更新引入了几个问题。首先,您的x值是度,而np.cos期望值以弧度表示。因此,我们最好使用np.deg2rad转换值。反向函数为np.rad2deg 其次,适合不同频率也是一个好主意,让我们为此引入一个额外的参数 第三,拟合通常对初始猜测非常敏感。您可以在p0中为参数scipy提供参数 第四,您将拟合曲线的分辨率更改为数据点的低分辨率,因此它看起来欠采样。如果我们解决所有这些问题:

import numpy as np
from scipy.optimize import curve_fit

#sample data
x_data = [0, 60, 120, 180, 240, 300]
y_data = [25, 40, 70, 30, 10, 15]

#definition of the function with additional frequency value d
def myfunc(x, a, b, c, d):
    return a + b * np.cos(d * np.deg2rad(x) - c)
#initial guess of parameters a, b, c, d
p_initial = [np.average(y_data), np.average(y_data), 0, 1]

#the actual curve fitting procedure, a, b, c, d are stored in popt
popt, _pcov = curve_fit(myfunc, x_data, y_data, p0 = p_initial)
print(popt)
#we have to convert the phase shift back into degrees
print(np.rad2deg(popt[2]))

#graphic representation of the data points and the fitted curve
from matplotlib import pyplot as plt
#define x_values for a smooth curve representation
x_fit = np.linspace(np.min(x_data), np.max(x_data), 1000)
y_fit = myfunc(x_fit, *popt)

plt.plot(x_data, y_data, "ro", label = "data")
plt.plot(x_fit, y_fit, "b", label = "fit")
plt.xlabel(r'$\theta$ (degrees)');
plt.ylabel(r'$f(\theta)$');

plt.legend()
plt.show()

我们得到这个输出:

[34.31293761 26.92479369  2.20852009  1.18144319]
126.53888003953764

enter image description here