如何返回每个交叉验证预测的结果
machine-learning 351
原文标题 :How do I return the result of each cross validation prediction
我有一项任务需要我分析模型,但我需要每个交叉验证步骤的输出预测以及交叉验证在该步骤中使用的数据。
这是我的代码:
results= cross_validate(MLPClassifier, X_train, y_train, cv=5,return_estimator = True)
这没有用。还,
results= cross_val_predict(MLPClassifier, X_train, y_train, cv=5)
两者都不起作用,但是第二种方法给了我一组预测,它们是 y_train (标签)的形状。但是,我希望返回一个较小的值,例如 y_train 大小的 10%。此外,我不确定如何获取用于每个交叉验证步骤的数据。
回复
我来回复-
Tim 评论
使用其中一个交叉验证迭代器怎么样?
from sklearn.datasets import make_classification from sklearn.model_selection import ShuffleSplit from sklearn.neural_network import MLPClassifier X, y = make_classification(n_samples=1000, random_state=0) datasets = {} # [(X_train, y_train), (X_test, y_test)] results = {} ss = ShuffleSplit(n_splits=5, test_size=0.25, random_state=0) for idx, (train_index, test_index) in enumerate(ss.split(X)): X_train, y_train = X[train_index], y[train_index] X_test, y_test = X[test_index], y[test_index] datasets[f"train_{idx}"] = X_train, y_train datasets[f"test_{idx}"] = X_test, y_test model = MLPClassifier(random_state=0).fit(X_train, y_train) results[f"accuracy_{idx}"] = model.score(X_test, y_test) results
输出:
{'accuracy_0': 0.968, 'accuracy_1': 0.924, 'accuracy_2': 0.94, 'accuracy_3': 0.944, 'accuracy_4': 0.964}
2年前