熊猫数据框到张量流输入

时间:2019-12-28 15:32:10

标签: python pandas tensorflow

我想使用熊猫数据集作为神经网络的输入。

我的神经网络模型是:

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
    ]
)

然后我将数据框作为输入传递:

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)

但是我得到这个错误:

ValueError:请提供单个数组或数组列表作为模型输入。

有人知道热将这个数据帧转换成数组以便我可以喂它吗?还是我不了解其他问题?

1 个答案:

答案 0 :(得分:0)

使用

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) 

pd数据帧的.to_numpy()函数将其转换为numpy数组。