老师,您好!
我之前按照网上的例子,将训练集和验证集捆绑在一起,然后调参、训练模型。请问这样对吗?有人说验证集不能参与到模型的构建当中。谢谢!
下图是网页的例子https://www.jianshu.com/p/55b9f2ea283b:
这是我的代码:
# 网格搜索
from sklearn.model_selection import GridSearchCV
X_train, X_test, y_train, y_test = train_test_split(f_v, l_v, random_state=9, test_size=0.2) #f_v代表特征全部样本数据
clf = RandomForestClassifier(random_state=80,oob_score=True)
grid_values = {'n_estimators':[10, 100, 200, 300], 'max_features':[None, 'auto']}
grid_clf_acc = GridSearchCV(clf, param_grid = grid_values, scoring = 'roc_auc', cv=5)
grid_clf_acc.fit(X_train, y_train)
# 新参数预测值
y_pred_acc = grid_clf_acc.predict(X_test)
fpr,tpr,threshold = roc_curve(y_test, y_pred_acc)
# 模型评估指标
print('Accuracy Score : ' + str(accuracy_score(y_test,y_pred)))
print('Recall Score : ' + str(recall_score(y_test,y_pred)))
print('F1 Score : ' + str(f1_score(y_test,y_pred)))
print('AUC : ', auc(fpr,tpr))
# 逻辑回归(网格搜索)混淆矩阵
confusion_matrix(y_test,y_pred_acc)
谢谢!