我有两个输入X1和X2,以及对应的标签Y。我想使用SkLearn的train_test_split将数据分割成训练和验证集。我的X1的形状是(1920,12),X2的形状是(1920,51,5)。我使用的代码是:
from sklearn.model_selection import train_test_splitX1 = np.load('x_train.npy')X2 = np.load('oneHot.npy')y_train = np.load('y_train.npy')X = np.array(list(zip(X1, X2))) ### 将两个输入打包。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)。我在这里做错了什么?欢迎提供见解。
回答:
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,))