Python、Sklearn:如何反转Sklearn的train_test_split?

问题描述 投票:0回答:2

如果我有一个数据集

X
及其标签
Y
,那么我将其分为训练集和测试集,scle为0.2,并使用随机种子进行洗牌:11

>>>X.shape
(10000, 50,50)

train_data, test_data, train_label, test_label = train_test_split(X, Y, test_size=0.2, random_state=11, shuffle=True)

如何知道分割后的数据中某个样本的原始索引是多少,即反转随机洗牌?

例如,

X[?]
对应的
train_data[123]
是什么?

python scikit-learn
2个回答
1
投票

根据数据类型,您可能能够轻松获取也可能无法获取。如果它们是训练数据中唯一且不重复的行,则可以将 X 中的每个元素字符串化,然后使用迭代器的索引函数来识别位置。

例如。

X =  ['i like wanda', 'i dont like anything', 'does this matter', 'this is choice test', 'how are you useful',  'are you mattering', 'this is a random test', 'this is my test', 'i dont like math', 'how can anything matter', 'who does matter', 'i like water', 'this is someone test', 'how does it matter', 'what is horrible',  'i dont like you', 'this is a valid test', 'this is a sample test', 'i like everything', 'i like ice cream', 'how can anything be useful', 'how is this useful', 'this is horrible', 'i dont like jokes']


Y = ['0', '0', '1', '0', '1', '1', '0', '0', '0', '1', '1', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '1', '0', '0']
train_data, test_data, train_label, test_label = train_test_split(X, Y, test_size=0.2, random_state=11, shuffle=True)
for each in train_data:
     print X.index(each)

上面会给我 X 中的原始索引。但在这种情况下这是可能的,因为 X 具有不同的元素并且是字符串类型。对于更复杂的数据类型,您可能需要进行更多处理。


0
投票

另一种方法是在进行训练测试分割之前分配索引,这样你也可以分割索引: X_train,X_test,y_train,y_test,indices_train,indices_test = train_test_split(X,y,索引,test_size = 0.2,random_state = 42)

© www.soinside.com 2019 - 2024. All rights reserved.