将最佳网格搜索超参数分配到 Python Bagging Classifier 中的最终模型
Assigning best grid searched hyperparameters into final model in Python Bagging Classifier
我正在训练逻辑回归并使用套袋。我想使用 gridsearch CV 来找到最好的超参数。我使用'__'来表示基本估计器的超参数:
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import BaggingClassifier
param_grid = {
'base_estimator__C': [1e-15, 1e-10, 1e-8, 1e-4, 1e-3, 1e-2, 1, 5, 10, 20, 50, 100, 1000], # lambdas for regularization
'max_samples': [0.05, 0.1, 0.2, 0.5], # for bootstrap sampling
'max_features': [0.3,0.5,0.7,0.9]}
clf = GridSearchCV(BaggingClassifier(LogisticRegression(penalty='l2'),
n_estimators = 100),
param_grid, cv=cv, scoring='f1', return_train_score=True)
clf.fit(x,y)
best_hyperparams = clf.best_params_
best_hyperparams
Results:
{'base_estimator__C': 10, 'max_features': 0.3, 'max_samples': 0.1}
既然已经得到了最好的参数,那么如何将其重新放入bagging分类器呢?使用 **best_hyperparams
不起作用,因为 Bagging 分类器无法识别 base_estimator__C
应该进入基础估计器,逻辑回归
best_clf = BaggingClassifier(LogisticRegression(penalty='l2'), n_estimators = 100, **best_hyperparams) # train model with best hyperparams
初始化bagging分类器后即可使用set_params()
best_clf = BaggingClassifier(LogisticRegression(penalty='l2'), n_estimators = 100)
best_clf.set_params(**best_hyperparams)
我正在训练逻辑回归并使用套袋。我想使用 gridsearch CV 来找到最好的超参数。我使用'__'来表示基本估计器的超参数:
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import BaggingClassifier
param_grid = {
'base_estimator__C': [1e-15, 1e-10, 1e-8, 1e-4, 1e-3, 1e-2, 1, 5, 10, 20, 50, 100, 1000], # lambdas for regularization
'max_samples': [0.05, 0.1, 0.2, 0.5], # for bootstrap sampling
'max_features': [0.3,0.5,0.7,0.9]}
clf = GridSearchCV(BaggingClassifier(LogisticRegression(penalty='l2'),
n_estimators = 100),
param_grid, cv=cv, scoring='f1', return_train_score=True)
clf.fit(x,y)
best_hyperparams = clf.best_params_
best_hyperparams
Results:
{'base_estimator__C': 10, 'max_features': 0.3, 'max_samples': 0.1}
既然已经得到了最好的参数,那么如何将其重新放入bagging分类器呢?使用 **best_hyperparams
不起作用,因为 Bagging 分类器无法识别 base_estimator__C
应该进入基础估计器,逻辑回归
best_clf = BaggingClassifier(LogisticRegression(penalty='l2'), n_estimators = 100, **best_hyperparams) # train model with best hyperparams
初始化bagging分类器后即可使用set_params()
best_clf = BaggingClassifier(LogisticRegression(penalty='l2'), n_estimators = 100)
best_clf.set_params(**best_hyperparams)