【问题标题】:pandas dataframe to tensorflow input熊猫数据框到张量流输入
【发布时间】:2020-04-18 02:28:28
【问题描述】:

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

我的神经网络模型是:

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: 请提供单个数组或数组列表作为模型输入。

有谁知道将这个数据帧转换成一个数组以便我可以喂它?还是有其他我不知道的问题?

【问题讨论】:

    标签: python pandas tensorflow


    【解决方案1】:

    使用

    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 数组。

    【讨论】:

    • 现在我明白了:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).
    • 在每个数组之后添加 .astype(np.float32)。这会将数组转换为浮点数组
    • 对不起,但现在我得到:NotFoundError:创建目录失败:logs/1577540868.3478014\train;没有这样的文件或目录 [Op:CreateSummaryFileWriter]
    • 这是由 tensorboard = TensorBoard(log_dir=f"logs/{time.time()}", histogram_freq=1) 行引起的,与此问题无关
    • 尝试使用 log_dir="logs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") 作为您的日志目录。为此,您必须导入日期时间。
    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 2013-02-23
    • 2019-06-28
    相关资源
    最近更新 更多