【问题标题】:Tensorflow tf.data.Dataset error when using map function | KeyErrorTensorflow tf.data.Dataset 使用 map 函数时出错 |键错误
【发布时间】:2020-12-17 06:01:44
【问题描述】:

我正在做我的顶点项目。基本上,我正在尝试为亚马逊美容产品建立一个推荐系统。该数据集是 TensorFlow 数据集

一些可以正常工作的源代码

 data=tfds.load('amazon_us_reviews/Beauty_v1_00', split='train')

 type: tensorflow.python.data.ops.dataset_ops.PrefetchDataset
  • 显示有关功能的一些信息:

    for sample in data.take(1).as_numpy_iterator():
    
    pprint.pprint(sample)
    
  • 输出

      {'data': {'customer_id': b'18239070',
           'helpful_votes': 0,
           'marketplace': b'US',
           'product_category': b'Beauty',
           'product_id': b'B00LJ86MAY',
           'product_parent': b'823234087',
           'product_title': b'The Original Curly Tee Towel - T-Shirt Hair Dryi'
                        b'ng Towel Wrap (Extra Long)',
            'review_body': b'Great product, quick ship and packaged nicely with a'
                      b'ttention to detail. Thank you!',
            'review_date': b'2014-10-04',
            'review_headline': b'Very pleased!',
            'review_id': b'R24WHRN0BMM2K7',
            'star_rating': 5,
            'total_votes': 0,
            'verified_purchase': 1,
            'vine': 1}}
    

错误

我尝试使用地图功能仅选择部分列

       data = data.map(lambda x: {
               "customer_id": x["customer_id"],
               "product_id": x["product_id"],
              "star_rating": x["star_rating"]
              })

KeyError:在用户代码中:

       KeyError: 'customer_id'

本教程中的代码可以正常工作,但当我尝试这样做时却无法正常工作。我一直在谷歌上搜索,找不到答案。

你有什么建议吗? 从现在开始感谢您的时间。

【问题讨论】:

  • 这似乎是由您访问data 的方式引起的 在您遵循的教程中是否以这种方式访问​​数据?我的意思是使用: data = data.map(lambda x: { "customer_id": x["customer_id"], "product_id": x["product_id"], "star_rating": x["star_rating"] })跨度>

标签: python tensorflow dictionary tensorflow-datasets keyerror


【解决方案1】:

您在访问字典时缺少“数据”键。

这应该可以解决它:

data = data.map(lambda x: {
        "customer_id": x["data"]["customer_id"],
        "product_id": x["data"]["product_id"],
        "star_rating": x["data"]["star_rating"]
       })

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2016-06-21
    相关资源
    最近更新 更多