在整个数据集上进行K折交叉验证

我想知道我当前的程序是否正确,或者我是否可能存在数据泄漏。在导入数据集后,我按80/20的比例进行了分割。

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0, stratify=y)

然后,在定义了CatBoostClassifier之后,我对训练集进行了带有交叉验证的网格搜索。

clf = CatBoostClassifier(leaf_estimation_iterations=1, border_count=254, scale_pos_weight=1.67)grid = {'learning_rate': [0.001, 0.003, 0.006,0.01, 0.03, 0.06, 0.1, 0.3, 0.6, 0.9],     'depth': [1, 2,3,4,5, 6,7,8,9, 10],     'l2_leaf_reg': [1, 3, 5, 7, 9,11,13,15],      'iterations': [50,150,250,350,450,600, 800,1000]}clf.grid_search(grid,             X=X_train,             y=y_train, cv=10)

现在我想评估我的模型。我可以使用整个数据集进行k折交叉验证来评估模型吗?(如以下代码所示)

kf = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=0)scoring = ['accuracy', 'f1', 'roc_auc', 'recall', 'precision']scores = cross_validate(    clf, X, y, scoring=scoring, cv=kf, return_train_score=True)print("Accuracy TEST: %0.2f (+/- %0.2f) Accuracy TRAIN: %0.2f (+/- %0.2f)" %      (scores['test_accuracy'].mean(), scores['test_accuracy'].std() * 2, scores['train_accuracy'].mean(), scores['train_accuracy'].std() * 2))print("F1 TEST: %0.2f (+/- %0.2f) F1 TRAIN : %0.2f (+/- %0.2f) " %      (scores['test_f1'].mean(), scores['test_f1'].std() * 2, scores['train_f1'].mean(), scores['train_f1'].std() * 2))print("AUROC TEST: %0.2f (+/- %0.2f) AUROC TRAIN : %0.2f (+/- %0.2f)" %      (scores['test_roc_auc'].mean(), scores['test_roc_auc'].std() * 2, scores['train_roc_auc'].mean(), scores['train_roc_auc'].std() * 2))print("recall TEST: %0.2f (+/- %0.2f) recall TRAIN: %0.2f (+/- %0.2f)" %      (scores['test_recall'].mean(), scores['test_recall'].std() * 2, scores['train_recall'].mean(), scores['train_recall'].std() * 2))print("Precision TEST: %0.2f (+/- %0.2f) Precision TRAIN: %0.2f (+/- %0.2f)" %      (scores['test_precision'].mean(), scores['test_precision'].std() * 2, scores['train_precision'].mean(), scores['train_precision'].std() * 2))

还是我应该只在训练集上进行k折交叉验证?


回答:

你通常会将交叉验证作为训练过程的一部分。它旨在找到模型的良好参数。只有在最后,你才应该在测试集上评估你的模型——这些数据在之前的模型训练和交叉验证过程中都没有被使用过。这样你就不会泄露任何数据。

所以,是的,你应该只在训练集上进行交叉验证。并只使用测试集进行最终评估。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注