【问题标题】:How to remove single feature from tensorflow dataset, how to use apply on single feture?如何从张量流数据集中删除单个特征,如何对单个特征使用应用?
【发布时间】:2022-02-17 16:55:14
【问题描述】:

我使用 dataset = tf.data.experimental.make_csv_dataset() 函数从 csv 文件创建数据集,但我的数据集具有分类和数字特征。

dataset=
color  price weight
red    120    1.2
blue    80     2.0
green   90     3

问题 1: 问题是我怎样才能只修改单个特征,例如权重+2,以:

dataset=
color  price weight
red    120    3.2
blue    80     4.0
green   90     5

我尝试做类似的事情:

dataset = dataset.apply(lambda x: x['weight']+2)

但错误是:“TypeError: 'FilterDataset' object is not subscriptable”

文档https://www.tensorflow.org/api_docs/python/tf/data/Dataset#apply 中的示例未显示。

问题 2: 如何删除单个功能?有没有相当于pandas drop column的?

【问题讨论】:

    标签: python tensorflow dictionary dataset tensorflow-datasets


    【解决方案1】:

    您可以通过仅过滤您想要的功能来移除功能。这样您就可以只修改一项功能:

    import tensorflow as tf
    import pandas as pd
    
    df = pd.DataFrame(data={'color': ['red', 'blue','green'], 'price': [120, 80, 90], 'weight': [3.2, 4.0, 5]})
    df.to_csv('data.csv', index=False)
    
    dataset = tf.data.experimental.make_csv_dataset('/content/data.csv', batch_size=1, num_epochs = 1, shuffle=False)
    dataset = dataset.map(lambda x: (x['color'], x['price'], x['weight']+2))
    
    for x in dataset:
      print(x[0], x[1], x[2])
    
    tf.Tensor([b'red'], shape=(1,), dtype=string) tf.Tensor([120], shape=(1,), dtype=int32) tf.Tensor([5.2], shape=(1,), dtype=float32)
    tf.Tensor([b'blue'], shape=(1,), dtype=string) tf.Tensor([80], shape=(1,), dtype=int32) tf.Tensor([6.], shape=(1,), dtype=float32)
    tf.Tensor([b'green'], shape=(1,), dtype=string) tf.Tensor([90], shape=(1,), dtype=int32) tf.Tensor([7.], shape=(1,), dtype=float32)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 2021-01-03
      • 2023-01-24
      • 2018-11-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      相关资源
      最近更新 更多