Shuffle Data from Dictionary for Test and Train Data

Viewed 330

I want to split the data I have from a dictionary and a separate array into train and test data. I have tried various ways but am not getting there. I need to keep the features as a dictionary initially due to how these are preprocessed in my pipeline. Anyone in the community have any suggestions for this?

Dictionary (Feature Values):

{'input1': array([42., 50., 68., ..., 60., 46., 60.]),
 'input2': array([[-2.00370455, -2.35689664, -1.96147382, ...,  2.11014128,
          2.59383321,  1.24209607],
        [-1.97130549, -2.19063663, -2.02996445, ...,  2.32125568,
          2.27316046,  1.48600614],
        [-2.01526666, -2.40440917, -1.94321752, ...,  2.15266657,
          2.68460488,  1.23534095],
        ...,
        [-2.1359458 , -2.52428007, -1.75701785, ...,  2.25480819,
          2.68114281,  1.75468981],
        [-1.95868206, -2.23297167, -1.96401751, ...,  2.07427239,
          2.60306072,  1.28556955],
        [-1.80507278, -2.62199521, -2.08697271, ...,  2.34080577,
          2.48254585,  1.52028871]])}

Target Values

y = array([0.83, 0.4 , 0.53, ..., 0.  , 0.94, 1. ])
Shape: (3000,)

Creating the Dictionary

#Dictionary Values
input1 = embeddings.numpy()
input2 = df['feature'].values
y = df['target'].values

full_model_inputs = [input1 , embeddings]
original_model_inputs = dict(input1 = input1 , input2 = input2 )

Splitting the Data

x_train, x_test, y_train, y_test = train_test_split([original_model_inputs['input1'], 
                                                     original_model_inputs['input2']], y, test_size = 0.2, random_state = 6)

or

x_train, x_test, y_train, y_test = train_test_split(original_model_inputs, y, test_size = 0.2, random_state = 6)

Error Message

ValueError: Found input variables with inconsistent numbers of samples: [2, 3000]

Input1:

[55., 46., 46., ..., 60., 60., 45.]

Shape: (3000,)

Input2:

[[-2.00370455, -2.35689664, -1.96147382, ...,  2.11014128,
         2.59383321,  1.24209607],
       [-1.97130549, -2.19063663, -2.02996445, ...,  2.32125568,
         2.27316046,  1.48600614],
       [-2.01526666, -2.40440917, -1.94321752, ...,  2.15266657,
         2.68460488,  1.23534095],
       ...,
       [-2.1359458 , -2.52428007, -1.75701785, ...,  2.25480819,
         2.68114281,  1.75468981],
       [-1.95868206, -2.23297167, -1.96401751, ...,  2.07427239,
         2.60306072,  1.28556955],
       [-1.80507278, -2.62199521, -2.08697271, ...,  2.34080577,
         2.48254585,  1.52028871]]

Shape: (3000, 3840)

Model Build

input1= Input(shape = (1, ))
input2= Input(shape = (3840, ))

# The first branch operates on the first input
x = Dense(units = 128, activation="relu")(input1)
x = BatchNormalization()(x)
x = Dense(units = 128, activation="relu")(x)
x = BatchNormalization()(x)
x = Model(inputs=input1, outputs=x)

# The second branch operates on the second input (Embeddings)
y = Dense(units = 128, activation="relu")(input2)
y = BatchNormalization()(y)
y = Dense(units = 128, activation="relu")(y)
y = BatchNormalization()(y)  
y = Model(inputs=input2, outputs=y)

# combine the output of the two branches
combined = Concatenate()([x.output, y.output])

out = Dense(128, activation='relu')(combined)
out = Dropout(0.5)(out)
out = Dense(1)(out)

# The model will accept the inputs of the two branches and then output a single value
model = Model(inputs = [x.input, y.input], outputs = out)
model.compile(loss='mse', optimizer = Adam(lr = 0.001), metrics = ['mse'])

model.fit([X1,X2], Y, epochs=3)
2 Answers

Put your dictionary into a pandas df that will preserve data dimensionality and split as you desire:

df = pd.DataFrame({"input1":original_model_inputs["input1"],  
                   "input2":list(original_model_inputs["input2"])})
X_train, X_test, y_train, y_test = train_test_split(df,y)

Convert back to original format:

X_train = X_train.to_dict("list")
X_test = X_test.to_dict("list")

Edit

For your pipeline to stay functional you may need to add these 2 lines:

X_train = {k:np.array(v) for k,v in X_train.items()}
X_test = {k:np.array(v) for k,v in X_test.items()}

You're feeding a nested list as X when calling train_test_split, which raises the error. Instead you could build a 2D array of features from the dictionary, and then split into train and test. Take for instance:

d = {'input1': np.random.random((10,)),
     'input2': np.random.random((10,3))}
y = np.random.choice([0,1],10)

We can just add an axis if one of the arrays in the dictionary has a single dimension, and then concatenate the result into a 2D array:

X = [a[:,None] if len(a.shape)==1 else a for a in d.values()]
X_train, X_test, y_train, y_test = train_test_split(np.concatenate(X, axis=1), y)
Related