Tensorflow: How to get x_train/y_train and x_test/y_test from train_data and test_data

Viewed 438

I want to get x_train/train and x_test/ytest from my train_ds and test_ds in tensorflow.

How do I convert?

I want to implement HParams, so I need the following structure:

 model.fit(x_train, y_train, epochs=1) # Run with 1 epoch to speed things up for demo purposes
 _, accuracy = model.evaluate(x_test, y_test)
 return accuracy

1 Answers

Assuming your train_ds is a pandas dataframe (I'll call it train_df) you have a few steps to get there:

  • Split out your y or your labels (the thing you are trying to predict).
y = list(train_df['my_label'])
  • You can also list the columns/features you'll use for training
training_features = ['customer_type', 'age', 'gender', 'country', 'state', ... ]
  • Use train_test_split to split your dataset nicely
from sklearn.model_selection import train_test_split
...
x_train, x_test, y_train, y_test = train_test_split(train_df[training_features], y, test_size=0.1, random_state=42)

Or some variation thereof.

Related