[英]How to do model evaluation with train, validation, test?
通常,如果给定一个数据集,我们使用
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
y_pred = lr.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(accuracy_score(y_test,y_pred))
print(classification_report(y_test,y_pred))
如果我们在训练数据集上进行验证
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.3, random_state=0)
如果训练和测试数据集都在单独的数据集中给出,我在代码中的哪里使用测试数据集?
通常我会将可用数据拆分 2 次,如下所示:
#first split: 80% (green+blue) and 20% (orange)
X_model, X_test, y_model, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
#second split: 75% (green) and 25% (orange)
X_train, X_val, y_train, y_val = train_test_split(X_model, y_model, test_size=0.25, random_state=0)
注意第一次拆分是 80:20 的比例,第二次拆分是 75:25 的比例,以便在整个数据集中获得 60:20:20 的比例。 如果您已经为 model train/val 和 model 测试提供了单独的数据集,则可以跳过第一个拆分。
在此之后,您可以继续训练和评估 model(使用_train
和_val
):
lr = some_model()
lr.fit(X_train, y_train)
y_pred = lr.predict(X_val)
print(confusion_matrix(y_val, y_pred))
print(accuracy_score(y_val, y_pred)) #accuracy score (TRAIN)
print(classification_report(y_val, y_pred))
使用各种模型重复此操作,并在 model 调整中,直到找到性能最佳的 model 和超参数。 强烈建议在此处进行交叉验证,以确定更好、更健壮的 model。
找到获胜的 model 后,对保留数据进行测试(使用_test
):
y_pred = lr.predict(X_test)
print(confusion_matrix(y_test, y_pred))
print(accuracy_score(y_test, y_pred)) #accuracy score (TEST)
print(classification_report(y_test, y_pred))
现在您可以比较 2 个准确度分数(TRAIN 和 TEST)来查看 model 是过拟合还是欠拟合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.