【问题标题】:Sklearn's train_test_split not working on multiple inputsSklearn 的 train_test_split 不适用于多个输入
【发布时间】:2021-07-08 22:06:21
【问题描述】:

我有两个输入 X1、X2 和相应的标签 Y。我想使用 SkLearn 的 train_test_split 将数据拆分为训练和验证。我的 X1 的形状为 (1920,12),而 X2 的形状为 (1920,51,5)。我使用的代码是:

from sklearn.model_selection import train_test_split

X1 = np.load('x_train.npy')
X2 = np.load('oneHot.npy')
y_train = np.load('y_train.npy')

X = np.array(list(zip(X1, X2))) ### To zip the two inputs.

X_train, X_valid, y_train, y_valid = train_test_split(X, y_train,test_size=0.2)

X1_train, oneHot_train = X_train[:, 0], X_train[:, 1]

但是,当我检查 X1_train 和 oneHot_train 的形状时,它是 (1536,),而 X1_train 应该是 (1536,12) 而 oneHot_train 应该是 (1536,51,5)。我在这里做错了什么?我们将不胜感激。

【问题讨论】:

    标签: python-3.x machine-learning scikit-learn


    【解决方案1】:

    train_test_split 可以占用任意数量的迭代器进行拆分。因此,您可以直接输入 x1 和 x2 - 如下所示:

    x1 = np.random.rand(1920,12)
    
    x2 = np.random.rand(1920,51,5)
    
    y = np.random.choice([0,1], 1920)
    
    x1_train, x1_test, x2_train, x2_test, y_train, y_test = train_test_split(\
         x1, x2, y ,test_size=0.2)
    
    x1_train.shape, x1_test.shape
     # ((1536, 12), (384, 12))
    
    x2_train.shape, x2_test.shape
     # ((1536, 51, 5), (384, 51, 5))
    
    y_train.shape, y_test.shape
     # ((1536,), (384,))
    
    

    【讨论】:

    • 谢谢@Venkatchalam。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2016-12-30
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多