【问题标题】:Python, Sklearn: How to reverse train_test_split of Sklearn?Python,Sklearn:如何反转 Sklearn 的 train_test_split?
【发布时间】:2018-04-11 13:50:06
【问题描述】:

如果我有一个数据集 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[?]是什么?

【问题讨论】:

  • 如果你加了shuffle=True,你也可以忘记它。
  • 在 split/shuffle 之前添加一个带有索引数字索引的列?

标签: python scikit-learn


【解决方案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 具有不同的元素并且是字符串类型。对于更复杂的数据类型,您可能需要进行更多处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2019-03-25
    • 2017-10-05
    • 2021-06-03
    • 2020-04-24
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多