通过MultiOutputRegressor进行GridSearch? [英] GridSearch over MultiOutputRegressor?
问题描述
让我们考虑一个多元回归问题(两个响应变量:纬度和经度).当前,一些机器学习模型实现(例如支持向量回归 sklearn.svm.SVR
)目前尚不提供对多元回归的幼稚支持.因此,可以使用 sklearn.multioutput.MultiOutputRegressor
.
Let's consider a multivariate regression problem (2 response variables: Latitude and Longitude). Currently, a few machine learning model implementations like Support Vector Regression sklearn.svm.SVR
do not currently provide naive support of multivariate regression. For this reason, sklearn.multioutput.MultiOutputRegressor
can be used.
示例:
from sklearn.multioutput import MultiOutputRegressor
svr_multi = MultiOutputRegressor(SVR(),n_jobs=-1)
#Fit the algorithm on the data
svr_multi.fit(X_train, y_train)
y_pred= svr_multi.predict(X_test)
我的目标是通过 sklearn.model_selection.GridSearchCV
调整 SVR
的参数.理想情况下,如果响应是单个变量而不是多个变量,那么我将执行以下操作:
My goal is to tune the parameters of SVR
by sklearn.model_selection.GridSearchCV
. Ideally, if the response was a single variable and not multiple, I would perform an operation as follows:
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
pipe_svr = (Pipeline([('scl', StandardScaler()),
('reg', SVR())]))
grid_param_svr = {
'reg__C': [0.01,0.1,1,10],
'reg__epsilon': [0.1,0.2,0.3],
'degree': [2,3,4]
}
gs_svr = (GridSearchCV(estimator=pipe_svr,
param_grid=grid_param_svr,
cv=10,
scoring = 'neg_mean_squared_error',
n_jobs = -1))
gs_svr = gs_svr.fit(X_train,y_train)
但是,由于我的响应 y_train
是二维的,我需要在 SVR 之上使用 MultiOutputRegressor
.如何修改上面的代码以启用此 GridSearchCV 操作?如果不可能的话,还有更好的选择吗?
However, as my response y_train
is 2-dimensional, I need to use the MultiOutputRegressor
on top of SVR. How can I modify the above code to enable this GridSearchCV operation? If not possible, is there a better alternative?
推荐答案
我刚刚找到了可行的解决方案.对于嵌套的估算器,可以通过 estimator __
访问内部估算器的参数.
I just found a working solution. In the case of nested estimators, the parameters of the inner estimator can be accessed by estimator__
.
from sklearn.multioutput import MultiOutputRegressor
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
pipe_svr = Pipeline([('scl', StandardScaler()),
('reg', MultiOutputRegressor(SVR()))])
grid_param_svr = {
'reg__estimator__C': [0.1,1,10]
}
gs_svr = (GridSearchCV(estimator=pipe_svr,
param_grid=grid_param_svr,
cv=2,
scoring = 'neg_mean_squared_error',
n_jobs = -1))
gs_svr = gs_svr.fit(X_train,y_train)
gs_svr.best_estimator_
Pipeline(steps=[('scl', StandardScaler(copy=True, with_mean=True, with_std=True)),
('reg', MultiOutputRegressor(estimator=SVR(C=10, cache_size=200,
coef0=0.0, degree=3, epsilon=0.1, gamma='auto', kernel='rbf', max_iter=-1,
shrinking=True, tol=0.001, verbose=False), n_jobs=1))])
这篇关于通过MultiOutputRegressor进行GridSearch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!