我想应用左一对交叉验证(LPOCV)的二进制分类问题。对于每一对被选择为保留/测试对的样本,它应该是来自每个二进制类的一个样本。
我的代码是:
from sklearn.model_selection import LeavePOut
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8],[9,10]])
y = np.array([0,1,1,0,0])
lpo = LeavePOut(2)
print(lpo.get_n_splits(X))
print(lpo)
LeavePOut(p=2)
for train_index, test_index in lpo.split(X):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]输出如下:
LeavePOut(p=2)
TRAIN: [2 3 4] TEST: [0 1]
TRAIN: [1 3 4] TEST: [0 2]
TRAIN: [1 2 4] TEST: [0 3]
TRAIN: [1 2 3] TEST: [0 4]
TRAIN: [0 3 4] TEST: [1 2]
TRAIN: [0 2 4] TEST: [1 3]
TRAIN: [0 2 3] TEST: [1 4]
TRAIN: [0 1 4] TEST: [2 3]
TRAIN: [0 1 3] TEST: [2 4]
TRAIN: [0 1 2] TEST: [3 4]测试对0 3和0 4属于同一类。他们是否有办法将X数据分割成由0和1类的样本组成的测试对?
发布于 2020-09-03 07:02:25
我认为您可以调整代码,以便省略测试集只包含一个类的索引(例如,类、)的所有折叠:
from sklearn.model_selection import LeavePOut
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8],[9,10]])
y = np.array([0,1,1,0,0])
lpo = LeavePOut(2)
print(lpo.get_n_splits(X))
print(lpo)
LeavePOut(p=2)
for train_index, test_index in lpo.split(X):
for x in range(0,len(test_index)):
for z in range(1,len(test_index)):
if(y[test_index[x]] != y[test_index[z]]):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]然后输出是:
LeavePOut(p=2)
TRAIN: [2 3 4] TEST: [0 1]
TRAIN: [1 3 4] TEST: [0 2]
TRAIN: [0 2 4] TEST: [1 3]
TRAIN: [0 2 3] TEST: [1 4]
TRAIN: [0 1 4] TEST: [2 3]
TRAIN: [0 1 3] TEST: [2 4]去掉的褶皱仅代表一个类,即对0 3、0 4、1 2和3 4。
https://stackoverflow.com/questions/63705004
复制相似问题