Python、Sklearn : How to reverse train_test_split of Sklearn?

标签 python scikit-learn

如果我有一个数据集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)

如何知道分割数据中样本的原始索引是什么,这意味着反转随机洗牌?

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

最佳答案

根据数据的类型,您可能能够轻松获取也可能无法获取。如果训练数据中是唯一且不重复的行,则可以将 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 具有不同的元素并且是字符串类型。对于更复杂的数据类型,您可能需要进行更多处理。

关于Python、Sklearn : How to reverse train_test_split of Sklearn?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49776897/

相关文章:

python - 为什么在sklearn线性回归中intercept_是一个数组?

python - 将文件头解析为 Python/pandas 中的日期对象

python - 扩展描述一组数字的字符串,这些数字被标记为数字和/或范围列表

python - 如何提高 Sklearn GMM predict() 性能速度?

python - 如何进行groupKfold验证并获得平衡的数据?

scikit-learn - Scikit 学习多标签分类,从 MultiLabelBinarizer 获取标签

python - 删除每列(和相应行)中的异常值

python - 让 libsass-python 使用系统 libsass 库而不是编译它

python - 如何使python中的if-elif-else语句更节省空间?

scikit-learn - NNS : Is there a way to omit certain features in a prediction?