最近在进行多分类问题中,用到了OneVsRestClassifier
模型,但是遇到了一个比较困惑的问题,那就是如何有效的调参?
一、简单模型网格调参
param = {"criterion": ["gini", "entropy"], "max_depth": np.arange(1,20,1), 'n_estimators': np.arange(50, 501, 50)}
rf_gs = GridSearchCV(estimator = RandomForestClassifier(), param_grid = param, cv = 5, scoring = "f1", n_jobs = -1, verbose = 10)
rf_gs.fit(train_x, train_y)
print(rf_gs.best_params_)
y_hat = rf_gs.best_estimator_.predict(test_x)
print(classification_report(test_y, y_hat))
二、使用OneVsRestClassifier的调参
此时我们需要在每个参数面前加上estimator__
param = {"estimator__max_depth": np.arange(1,20,1), 'estimator__n_estimators': np.arange(100, 501, 50)}
model = OneVsRestClassifier(RandomForestClassifier(random_state = 10))
clf = GridSearchCV(model,param_grid = param, scoring='f1', n_jobs = -1, cv = 5, verbose = 2)
clf.fit(X_train, y_train)
print(clf.best_params_)
y_hat = clf.best_estimator_.predict(X_test)
print(classification_report(y_test, y_hat))