如何通过留下一对交叉验证(LeavePOut)来拆分数据以进行二进制分类?
How to split data with leave one pair out cross validation (LeavePOut) for binary classification?
我想对二进制 class化问题应用留下一对交叉验证 (LPOCV)。对于选择为 holdout/test 对的每一对样本,它应该是来自每个二进制 class.
的一个样本
我的代码是这样的:
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]属于同一个class 0。他们有什么方法可以将 X 数据与包含来自 0 和 1 class 的样本的测试对分开吗?
我想你可以调整你的代码,使测试集的所有折叠只有一个 class 的索引(例如 class 0)被省略:
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]
并且删除的折叠是那些,其中测试集只代表一个 class,即 [0 3]、 对[0 4]、[1 2] 和 [3 4]
我想对二进制 class化问题应用留下一对交叉验证 (LPOCV)。对于选择为 holdout/test 对的每一对样本,它应该是来自每个二进制 class.
的一个样本我的代码是这样的:
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]属于同一个class 0。他们有什么方法可以将 X 数据与包含来自 0 和 1 class 的样本的测试对分开吗?
我想你可以调整你的代码,使测试集的所有折叠只有一个 class 的索引(例如 class 0)被省略:
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]
并且删除的折叠是那些,其中测试集只代表一个 class,即 [0 3]、 对[0 4]、[1 2] 和 [3 4]