通过在 python 中更改模型函数来拟合通用曲线
generic curve fitting by changing model function in python
我正在使用 SciPy.optimize.curve_fit https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
为了得到曲线拟合函数的系数,SciPy函数将模型函数作为它的第一个参数
所以如果我想进行线性曲线拟合,我将以下函数传递给它:
def objective(x, a, b):
return a * x + b
如果我想要二阶多项式曲线拟合,我通过以下:
def objective(x, a, b, c):
return a * x + b * x**2 + c
等等,我想实现的是让这个模型功能通用
例如,如果用户想通过输入 5 将曲线拟合为 5 次多项式,则应更改为
def objective(x, a, b, c, d, e, f):
return (a * x) + (b * x**2) + (c * x**3) + (d * x**4) + (e * x**5) + f
而密码是运行
这可能吗?
如果无法使用 SciPy 因为它需要更改功能,是否有其他方法可以实现我想要的?
如果你真的想自己实现它,你可以使用可变数量的系数*coeffs
:
def objective(x, *coeffs):
result = coeffs[-1]
for i, coeff in enumerate(coeffs[:-1]):
result += coeff * x**(i+1)
return result
或使用np.polyval
:
import numpy as np
def objective(x, *coeffs):
return np.polyval(x, coeffs)
但是请注意,没有必要使用 curve_fit
。你可以直接用np.polyfit
做一个最小二乘多项式拟合。
任务可以通过几种方式完成。如果你想使用scipy,你可以简单地创建一个字典来使用用户输入的数字来引用特定的函数:
import scipy.optimize as optimization
polnum = 2 # suppose this is input from user
def objective1(x, a, b):
return a * x + b
def objective2(x, a, b, c):
return a * x + b * x**2 + c
# Include some more functions
# Do not include round brackets in the dictionary
object_dict = {
1: objective1,
2: objective2
# Include the numbers with corresponding functions
}
opt = optimization.curve_fit(object_dict[polnum], x, y) # Curve fitted
print(opt[0]) # Returns parameters
但是,我建议您采用一种更好的方法,您不必定义每个函数:
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LinearRegression
polnum = 2 # suppose this is input from user
# Creates a Polynomial of polnum degree
model = make_pipeline(PolynomialFeatures(polnum), LinearRegression)
# You can change the estimator Linear Regression to any other offered in sklearn.linear_model
# Make sure that x and y data are of the same shape
model.fit(x, y)
# Now you can use the model to check some other values
y_predict = model.predict(x_predict[:, np.newaxis])
我正在使用 SciPy.optimize.curve_fit https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html 为了得到曲线拟合函数的系数,SciPy函数将模型函数作为它的第一个参数 所以如果我想进行线性曲线拟合,我将以下函数传递给它:
def objective(x, a, b):
return a * x + b
如果我想要二阶多项式曲线拟合,我通过以下:
def objective(x, a, b, c):
return a * x + b * x**2 + c
等等,我想实现的是让这个模型功能通用 例如,如果用户想通过输入 5 将曲线拟合为 5 次多项式,则应更改为
def objective(x, a, b, c, d, e, f):
return (a * x) + (b * x**2) + (c * x**3) + (d * x**4) + (e * x**5) + f
而密码是运行 这可能吗? 如果无法使用 SciPy 因为它需要更改功能,是否有其他方法可以实现我想要的?
如果你真的想自己实现它,你可以使用可变数量的系数*coeffs
:
def objective(x, *coeffs):
result = coeffs[-1]
for i, coeff in enumerate(coeffs[:-1]):
result += coeff * x**(i+1)
return result
或使用np.polyval
:
import numpy as np
def objective(x, *coeffs):
return np.polyval(x, coeffs)
但是请注意,没有必要使用 curve_fit
。你可以直接用np.polyfit
做一个最小二乘多项式拟合。
任务可以通过几种方式完成。如果你想使用scipy,你可以简单地创建一个字典来使用用户输入的数字来引用特定的函数:
import scipy.optimize as optimization
polnum = 2 # suppose this is input from user
def objective1(x, a, b):
return a * x + b
def objective2(x, a, b, c):
return a * x + b * x**2 + c
# Include some more functions
# Do not include round brackets in the dictionary
object_dict = {
1: objective1,
2: objective2
# Include the numbers with corresponding functions
}
opt = optimization.curve_fit(object_dict[polnum], x, y) # Curve fitted
print(opt[0]) # Returns parameters
但是,我建议您采用一种更好的方法,您不必定义每个函数:
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LinearRegression
polnum = 2 # suppose this is input from user
# Creates a Polynomial of polnum degree
model = make_pipeline(PolynomialFeatures(polnum), LinearRegression)
# You can change the estimator Linear Regression to any other offered in sklearn.linear_model
# Make sure that x and y data are of the same shape
model.fit(x, y)
# Now you can use the model to check some other values
y_predict = model.predict(x_predict[:, np.newaxis])