Scikit learn 用GridSearchCV拟合三次多项式系数
我有一个三次多项式,系数a,b,c&d已知 我想写一个算法,根据曲线上提供的一组x和相应的y坐标来查找这些系数 这似乎是一个非常简单的参数调整案例。我想指定一系列可能的值,让GridSearchCV迭代所有组合,对结果进行评分,并推荐得分最高(或成本函数最低)的值组合 这是我的密码:Scikit learn 用GridSearchCV拟合三次多项式系数,scikit-learn,curve-fitting,hyperparameters,model-fitting,gridsearchcv,Scikit Learn,Curve Fitting,Hyperparameters,Model Fitting,Gridsearchcv,我有一个三次多项式,系数a,b,c&d已知 我想写一个算法,根据曲线上提供的一组x和相应的y坐标来查找这些系数 这似乎是一个非常简单的参数调整案例。我想指定一系列可能的值,让GridSearchCV迭代所有组合,对结果进行评分,并推荐得分最高(或成本函数最低)的值组合 这是我的密码: import numpy as np from sklearn.model_selection import GridSearchCV def poly(x,a=1,b=1,c=1,d=1):
import numpy as np
from sklearn.model_selection import GridSearchCV
def poly(x,a=1,b=1,c=1,d=1):
"""A polynomial function that calculates a polynomial function for an array of x values
a, b, c & d are the coefficients of the polynomial"""
y = a*x**3 + b*x**2 + c*x + d
return y
# Regular values along the x axis
x_range = np.arange(-10,11,1)
# The 'true' coefficients
a_t = 0.1; b_t = -0.5; c_t = 0.8; d_t = 10
# Y values of the 'true' polynomial along the x axis
y_true = poly(x_range, a_t,b_t,c_t,d_t)
######## Fit the parameters ########
model = poly(x=x_range)
# define a grid of parameters to sort through
param_grid={
'a' : [0,0.1,0.2],
'b' : [-1,-0.5,0,0.5,1],
'c' : [1,2],
'd' : [0,10,20,30]}
grid = GridSearchCV(model, param_grid=param_grid, scoring='neg_mean_squared_error')
grid.fit(x_range, y_true)
我得到了错误:“TypeError:estimator应该是一个实现‘拟合’方法的估计器,数组([-909,-656,-455,-300,-185,-104,-51,-20,-5,0,1,
4、15、40、85、156、259、400、585、820、1111)获得通过。”
我想知道我需要向grid.fit函数提供什么,以使它在param_网格中迭代,并根据提供的y_真值找到得分最高的参数组合。结果是GridSearchCV不是适合我要求的正确工具。感谢balleveryday的深刻评论 我应该使用scipy.optimize库。以下是使用optimize.curve_fit和optimize.Minimize模块的两种解决方案: 这两方面的基本代码:
import numpy as np
import math
def poly(x,a,b,c,d):
"""A polynomial function that calculates y values for an array of x values.
a, b, c & d are the coefficients of the polynomial"""
y = a*x**3 + b*x**2 + c*x + d
return y
# Regular values along the x axis
x_range = np.arange(-10,11,1)
# The 'true' coefficients
a_t = 0.1; b_t = -0.5; c_t = math.pi/3; d_t = math.pi*2
# Y values of the 'true' polynomial along the x axis
y_true = poly(x_range, a_t,b_t,c_t,d_t)
bounds = np.array( [(0,1), (-2,2), (-3,3), (0,20)] )
initial_guess = np.array([0.5,-1, 1.2, 10])
曲线拟合
解决方案:
from scipy.optimize import curve_fit
popt, pcov = curve_fit(poly, x_range, y_true, p0 = initial_guess, bounds = bounds.T)
print(popt)
最小化
两种不同最小化方法的解决方案:
from scipy.optimize import minimize
def cost_fn(guess_array,true_array):
diff = guess_array - true_array
min_quad = np.mean(diff**4)
return(min_quad)
def calculate_cost(params):
y_trial = poly(x_range, a=params[0], b= params[1], c=params[2], d=params[3])
cost = cost_fn(y_trial,y_true)
return(cost)
for method in ['Powell', 'L-BFGS-B']:
res = minimize(fun = calculate_cost,
x0 = initial_guess,
bounds = bounds,
method = method,
)
print(res.x)
错误是因为您正在将数组(poly的输出)传递到
GridSearchCV
,而它需要一个scikit估计器,如sklearn.svm.SVC()
。是否可以澄清为什么要使用GridSearchCV
来获取系数而不是回归模型?在您使用的设置中,没有任何用于训练估计器的自由参数。如果你只需要估计4个系数,那么回归就足够了,除非你的问题是你正在研究的更大模型的一部分?