假设您想要使用 PCA 和 TruncatedSVD 作为维度缩减步骤。
pca = decomposition.PCA()
svd = decomposition.TruncatedSVD()
svm = SVC()
n_components = [20, 40, 64]
你可以这样做:
pipe = Pipeline(steps=[('reduction', pca), ('svm', svm)])
# Change params_grid -> Instead of dict, make it a list of dict
# In the first element, pass parameters related to pca, and in second related to svd
params_grid = [{
'svm__C': [1, 10, 100, 1000],
'svm__kernel': ['linear', 'rbf'],
'svm__gamma': [0.001, 0.0001],
'reduction':pca,
'reduction__n_components': n_components,
},
{
'svm__C': [1, 10, 100, 1000],
'svm__kernel': ['linear', 'rbf'],
'svm__gamma': [0.001, 0.0001],
'reduction':svd,
'reduction__n_components': n_components,
'reduction__algorithm':['randomized']
}]
现在只需将管道对象传递给 gridsearchCV
grd = GridSearchCV(pipe, param_grid = params_grid)
Calling grd.fit()
将使用以下所有值在 params_grid 列表的两个元素上搜索参数one
一次。
更详细的请看我的另一个回答:使用网格搜索获得最佳模型的“并行”管道 https://stackoverflow.com/questions/42266737/parallel-pipeline-to-get-best-model-using-gridsearch/42271829#42271829