我正在使用 sklearn GridSearch 使用预定义的验证集查找随机森林分类的最佳参数。GridSearch 返回的最佳估计器的分数与通过训练具有相同参数的单独分类器获得的分数不匹配。
数据拆分定义
X = pd.concat([X_train, X_devel])
y = pd.concat([y_train, y_devel])
test_fold = -X.index.str.contains('train').astype(int)
ps = PredefinedSplit(test_fold)
GridSearch 定义
n_estimators = [10]
max_depth = [4]
grid = {'n_estimators': n_estimators, 'max_depth': max_depth}
rf = RandomForestClassifier(random_state=0)
rf_grid = GridSearchCV(estimator = rf, param_grid = grid, cv = ps, scoring='recall_macro')
rf_grid.fit(X, y)
分类器定义
clf = RandomForestClassifier(n_estimators=10, max_depth=4, random_state=0)
clf.fit(X_train, y_train)
召回率是使用 sklearn.metrics.recall_score 明确计算的
y_pred_train = clf.predict(X_train)
y_pred_devel = clf.predict(X_devel)
uar_train = recall_score(y_train, y_pred_train, average='macro')
uar_devel = recall_score(y_devel, y_pred_devel, average='macro')
网格搜索
uar train: 0.32189884516029466
uar devel: 0.3328299259976279
随机森林:
uar train: 0.483040291148839
uar devel: 0.40706644557392435
这种不匹配的原因是什么?
慕田峪4524236
慕村9548890
相关分类