【发布时间】:2021-08-05 16:35:11
【问题描述】:
在调整模型的超参数时,我正在使用训练数据集(即 X_train、y_train)。我需要使用测试数据集(即 X_test、y_test)作为最终检查,以确保我的模型没有偏差。 我写了
folds = 4
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=(1/folds), random_state=38, stratify=y)
clf_logreg = Pipeline(steps=[('preprocessor', preprocessing),
('model', LogisticRegression(solver='lbfgs', max_iter=100))])
cv = KFold(n_splits=(folds - 1))
scores_logreg = cross_val_score(clf_logreg, X_train, y_train, cv = cv)
并且,要获得 f1 分数,
cross_val_score(clf_logreg, X_train, y_train, scoring=make_scorer(f1_score, average='weighted'),
cv=cv)
返回
scores_logreg: [0.94422311, 0.99335548, 0.97209302]
对于 f1:[0.97201365, 0.9926906 , 0.98925453]
为了检查测试,写对吗
cross_val_score(clf_logreg, X_test, y_test, scoring=make_scorer(f1_score, average='weighted'), cv=cv) # not sure if it is ok to let cv
或许
predicted_logreg= clf_logreg.predict(X_test)
f1 = f1_score(y_test, predicted_logreg)
返回的值不同。
【问题讨论】:
-
你试过在sklearn.metrics中使用classification_report吗?
标签: python scikit-learn pipeline cross-validation