You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Amazon Personalize构造批量推荐JSON输入数据

解决方案

要生成Amazon Personalize批量推荐所需的每行一个JSON对象格式(JSON Lines),可以通过以下步骤实现:

  1. 将userId转换为字符串类型:确保输出的JSON中userId值带引号,匹配目标格式要求。
  2. 利用Pandas参数生成单行JSON对象:通过lines=True参数让每个JSON对象单独占一行,替代默认的数组包裹格式。

完整代码示例

import pandas as pd

# 创建DataFrame并将userId转为字符串类型
batch_prediction_user_id = pd.DataFrame({'userId': range(0,5)}).astype({'userId': str})
# 生成每行一个JSON对象的字符串
result = batch_prediction_user_id.to_json(orient="records", lines=True)

print(result)

输出结果

{"userId":"0"}
{"userId":"1"}
{"userId":"2"}
{"userId":"3"}
{"userId":"4"}

直接写入文件(可选)

如果需要直接生成符合要求的批量输入文件,无需先生成字符串,可以直接指定文件路径:

batch_prediction_user_id.astype({'userId': str}).to_json('batch_input.json', orient="records", lines=True)

内容的提问来源于stack exchange,提问作者Learning

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 20:48:08