首页 > 解决方案 > 如何使用对字符串列正确的tensorflow数据集映射函数

问题描述

m 使用 tensorflow 数据集 api

我有一个带有字符串列的数据,它可以代表一个二进制选项

(类似于(“是”或“否”)

我想知道我是否将其分别转换为 1 和 0(整数值),并保持其他列不变

我的骨架功能是:

def mapper(features,target):
    #features["str_col"] TODO "MAP this when yes to 1 when no to 0"

    #return features with x transformed # TODO

你能帮忙吗?

标签: pythontensorflowtensorflow2.0tensorflow-datasets

解决方案


您可以将 bool 转换为 int:

y = tf.equal(features["str_col"], 'YES')
y = tf.cast(y, tf.int32)

推荐阅读