在Scikit-learn中,可以使用GridSearchCV或RandomizedSearchCV来实现模型的自适应调整。
from sklearn.model_selection import GridSearchCV
param_grid = {
'C': [0.1, 1, 10],
'kernel': ['linear', 'rbf']
}
grid_search = GridSearchCV(SVC(), param_grid, cv=5)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_
best_model = grid_search.best_estimator_
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import uniform
param_dist = {
'C': uniform(loc=0, scale=10),
'kernel': ['linear', 'rbf']
}
random_search = RandomizedSearchCV(SVC(), param_dist, n_iter=10, cv=5)
random_search.fit(X_train, y_train)
best_params = random_search.best_params_
best_model = random_search.best_estimator_
通过GridSearchCV或RandomizedSearchCV来实现模型自适应调整,可以帮助我们快速找到最佳的超参数组合,从而提高模型的性能和泛化能力。