训练测试 根据组变量拆分 sklearn



我的X如下: 编辑1:

Unique ID.   Exp start date.   Value.    Status.
001          01/01/2020.       4000.     Closed
001          12/01/2019        4000.     Archived
002          01/01/2020.       5000.     Closed
002          12/01/2019        5000.     Archived

我想确保测试中不包含训练中的唯一 ID。我正在使用 sklearn 测试列车拆分。这可能吗?

我相信你需要GroupShuffleSplit(文档在这里(。

import numpy as np
from sklearn.model_selection import GroupShuffleSplit
X = np.ones(shape=(8, 2))
y = np.ones(shape=(8, 1))
groups = np.array([1, 1, 2, 2, 2, 3, 3, 3])
print(groups.shape)
gss = GroupShuffleSplit(n_splits=2, train_size=.7, random_state=42)
for train_idx, test_idx in gss.split(X, y, groups):
print("TRAIN:", train_idx, "TEST:", test_idx)
TRAIN: [2 3 4 5 6 7] TEST: [0 1]
TRAIN: [0 1 5 6 7] TEST: [2 3 4]

从上面可以看出,训练/测试索引是基于groups变量创建的。

在您的情况下,Unique ID.应用作组。

最新更新