交叉验证后如何获取数据?
How can I get data after cross-validation?
我正在尝试使用 Xception 的迁移学习为 7 类 制作图像分类器。现在我正在尝试实施交叉验证。我知道 KFold return 索引,但如何获取数据值。
from sklearn.model_selection import KFold
import numpy as np
sample = np.array(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'])
kf = KFold(n_splits=3, shuffle=True)
for train_index, test_index in kf.split(sample):
print("TRAIN:", train_index, "TEST:", test_index)
它return
TRAIN: [1 2 3 4 6 7] TEST: [0 5 8]
TRAIN: [0 1 2 4 5 8] TEST: [3 6 7]
TRAIN: [0 3 5 6 7 8] TEST: [1 2 4]
但我想要的是
TRAIN: ['B', 'C', 'D', 'E', 'G', 'H'] TEST: ['A', 'F', 'I']
TRAIN: ['A', 'B', 'C', 'E', 'F', 'I'] TEST: ['D', 'G', 'H']
TRAIN: ['A', 'D', 'F', 'G', 'H', 'I'] TEST: ['B', 'C', 'E']
我该怎么办?
kf.split
returns 指数,而不是实际样本。您只需更改为:
for train_index, test_index in kf.split(sample):
print("TRAIN:", sample[train_index], "TEST:", sample[test_index])
结果:
TRAIN: ['A' 'B' 'C' 'E' 'F' 'H'] TEST: ['D' 'G' 'I']
TRAIN: ['A' 'D' 'F' 'G' 'H' 'I'] TEST: ['B' 'C' 'E']
TRAIN: ['B' 'C' 'D' 'E' 'G' 'I'] TEST: ['A' 'F' 'H']
我正在尝试使用 Xception 的迁移学习为 7 类 制作图像分类器。现在我正在尝试实施交叉验证。我知道 KFold return 索引,但如何获取数据值。
from sklearn.model_selection import KFold
import numpy as np
sample = np.array(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'])
kf = KFold(n_splits=3, shuffle=True)
for train_index, test_index in kf.split(sample):
print("TRAIN:", train_index, "TEST:", test_index)
它return
TRAIN: [1 2 3 4 6 7] TEST: [0 5 8]
TRAIN: [0 1 2 4 5 8] TEST: [3 6 7]
TRAIN: [0 3 5 6 7 8] TEST: [1 2 4]
但我想要的是
TRAIN: ['B', 'C', 'D', 'E', 'G', 'H'] TEST: ['A', 'F', 'I']
TRAIN: ['A', 'B', 'C', 'E', 'F', 'I'] TEST: ['D', 'G', 'H']
TRAIN: ['A', 'D', 'F', 'G', 'H', 'I'] TEST: ['B', 'C', 'E']
我该怎么办?
kf.split
returns 指数,而不是实际样本。您只需更改为:
for train_index, test_index in kf.split(sample):
print("TRAIN:", sample[train_index], "TEST:", sample[test_index])
结果:
TRAIN: ['A' 'B' 'C' 'E' 'F' 'H'] TEST: ['D' 'G' 'I']
TRAIN: ['A' 'D' 'F' 'G' 'H' 'I'] TEST: ['B' 'C' 'E']
TRAIN: ['B' 'C' 'D' 'E' 'G' 'I'] TEST: ['A' 'F' 'H']