我有一个数据集,我分裂成80%-20%的训练和测试集。在训练集上,我做k折交叉验证,得到准确性的平均值。但是,我不清楚该如何将这个结果应用于我的原始测试集?
#Splitting Training & Test dataset
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
#Standartisation scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train[:,:] = sc.fit_transform(X_train[:,:])
X_test[:,:] = sc.transform(X_test[:,:])
#Trainign the SVM model on the Training set
from sklearn.svm import SVC
classifier = SVC(kernel='rbf', random_state=0)
classifier.fit(X_train, y_train)
#Making the Confusion Matrix of SVM model
from sklearn.metrics import confusion_matrix, accuracy_score
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
print("SVM Model: ")
print(cm)
print('Accuracy of the test set:'+ str(accuracy_score(y_test, y_pred)))
#applying k-Fold cross validation
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score(estimator=classifier, X=X_train, y=y_train, cv=15)
print('Accuracy of K-Fold Validation: {:.2f} %'.format(accuracies.mean()*100))
print('Standard Deviation of K-Fold Validatio: {:.2f} %'.format(accuracies.std()*100))

发布于 2022-09-29 11:56:53
这里有两个问题:
如果您正在使用交叉验证,请不要首先缩放数据。相反,您需要向管道中添加一个定标器,并使用该管道进行交叉验证。这是因为在交叉验证期间,每个培训步骤只需要将数据缩放到当前的培训集,而不是整个数据集。在交叉验证方面,
sklearn将在交叉验证循环中为您训练它。一旦您选择了要使用的模型,那么您将对所有数据进行培训。因此,有一种方法可以用来完成你的任务:
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.model_selection import cross_val_score, cross_val_predict, cross_validate
X, y = make_classification(n_features=2, n_redundant=0, n_informative=2, random_state=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
sc = StandardScaler()
clf = SVC(kernel='rbf', random_state=0)
# Make a pipeline model that scales and estimates.
pipe = make_pipeline(sc, clf)
# Make predictions during cross-validation.
y_pred = cross_val_predict(pipe, X_train, y_train, cv=10, n_jobs=-1)
cm = confusion_matrix(y_train, y_pred)
score = accuracy_score(y_train, y_pred)
print("SVM Model: ")
print(cm)
print('Validation accuracy on training set: {:.2f}%'.format(score*100))
# Alternatively, use cross_val_score.
accuracies = cross_val_score(estimator=pipe, X=X_train, y=y_train, cv=10)
print()
print('Accuracy of k-fold validation: {:.2f}%'.format(accuracies.mean()*100))
print('Standard deviation of k-fold validation: {:.2f}%'.format(accuracies.std()*100))这导致:
SVM Model:
[[41 0]
[ 3 36]]
Validation accuracy on training set: 96.25%
Accuracy of k-fold validation: 96.25%
Standard deviation of k-fold validation: 5.73%cross_val_predict的关键之处在于它在交叉验证期间进行预测,当数据的每一次折叠都是验证集时。然后,它将所有预测放在一起,为整个数据集提供验证预测,因此您可以使用sklearn的各种评分工具。
关于cross_val_score的好处是,你可以看到所有的分数从每一个折叠。注意,它们平均为与整个数据集相同的内容: 96.25%。但是现在您也可以获得方差,这是很重要的,因为这会告诉您一些您可以从模型中预测的未来数据的可能的预测差异。
测试数据呢?
在模型选择工作流结束之前,您不应该使用X_test和y_test测试您的模型。只有当您已经设置了模型中的所有超参数(例如,该算法中的C、kernel和gamma超参数)时,才应该检查该模型如何处理测试数据。换句话说:不要将测试用于模型优化,而只用于性能评估。
发布于 2022-09-29 10:51:05
您可以保存每个折叠的所有模型,例如,如果您有5个折叠,那么您就有5个模型。通过每个模型运行您的测试集,并集成预测。
或
您可能一直在使用k折叠来确定最佳的超参数。在这种情况下,使用这些最好的超参数对整个数据集(训练和验证)进行重新训练,然后在外部测试集上进行评估。如果你做的是训练/测试分裂或训练/验证/测试,那么你的问题就不清楚了,所以要小心,不要重叠这些集合。
https://stackoverflow.com/questions/73893276
复制相似问题