Tzikos
Tzikos

Reputation: 111

pandas dataframe to tensorflow input

i want to use a pandas dataset as an input to a neural net.

my neural net model is:

def build_model():
    model = Sequential()
    model.add(Dense(128, activation = "relu"))
    model.add(Dropout(0.2))
    model.add(Dense(64, activation = "relu"))
    model.add(Dropout(0.1))
    model.add(Dense(32, activation = "softmax"))

    model.compile(
        optimizer='adam',
        loss=['binary_crossentropy'],
        metrics=['accuracy']
    )
    return model

tensorboard = TensorBoard(log_dir=f"logs/{time.time()}", histogram_freq=1)

model = build_model()

history = model.fit(
    x_train,
    y_train,
    epochs=5,
    batch_size=32,
    validation_data=(
        x_val,
        y_val
    ),
    callbacks=[
        tensorboard
    ]
)

and i pass my dataframe as input as such:

y_val, x_val, y_train, x_train = test_data.drop(['gender', 
       'comorbidities_count', 'comorbidities_significant_count',
       'medication_count'],axis=1),test_data.drop(['fried'],axis=1),training_data.drop([ 'gender', 'comorbidities_count', 'comorbidities_significant_count',
       'medication_count'],axis=1),training_data.drop(['fried'],axis=1)

but i get this error:

ValueError: Please provide as model inputs either a single array or a list of arrays.

Does anyone know hot to turn this dataframe into an array so i can feed it? Or is there some other issue i am not in knowledge of?

Upvotes: 0

Views: 955

Answers (1)

SdahlSean
SdahlSean

Reputation: 583

Use

y_val, x_val, y_train, x_train = test_data.drop(['gender', 
       'comorbidities_count', 'comorbidities_significant_count',
       'medication_count'],axis=1).to_numpy().astype(np.float32) ,test_data.drop(['fried'],axis=1).to_numpy().astype(np.float32) ,training_data.drop([ 'gender', 'comorbidities_count', 'comorbidities_significant_count',
       'medication_count'],axis=1).to_numpy().astype(np.float32) ,training_data.drop(['fried'],axis=1).to_numpy().astype(np.float32) 

The .to_numpy() function of a pd dataframe turns it into a numpy array.

Upvotes: 1

Related Questions