网格搜索用来寻找模型最佳的超参数
环境
- python==3.7
- scikit-learn==0.24.1
使用方式
import numpy as np
from sklearn import datasets
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
iris = datasets.load_iris()
X = iris["data"][:, (2, 3)]
y = (iris["target"] == 2).astype(np.float64)
poly_svm = Pipeline([
("preprocess", StandardScaler()),
("svm_clf", SVC(kernel="poly"))
])
para = {"svm_clf__degree": [3, 10],
"svm_clf__coef0": [1],
"svm_clf__C": [5, 10]}
gs = GridSearchCV(poly_svm, para, cv=5)
gs.fit(X, y)
print(gs.best_params_)
输出为: {‘svm_clf__C’: 10, ‘svm_clf__coef0’: 1, ‘svm_clf__degree’: 10}
这里用到了Pipeline, 对模型网格搜索时,需要在超参数名前面加模型名字+两个下划线(svm_clf__)