在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來實現模型自適應調整,可以幫助我們快速找到最佳的超參數組合,從而提高模型的性能和泛化能力。