【发布时间】:2019-07-04 21:47:27
【问题描述】:
我在 S3 中有一些数据,我想创建一个 lambda 函数来使用我部署的 aws sagemaker 端点预测输出,然后我再次将输出放入 S3。在这种情况下是否有必要创建一个像 link 中描述的 API 网关?在 lambda 函数中我必须输入什么。我期望放置(在哪里找到数据,如何调用端点,将数据放在哪里)
import boto3
import io
import json
import csv
import os
client = boto3.client('s3') #low-level functional API
resource = boto3.resource('s3') #high-level object-oriented API
my_bucket = resource.Bucket('demo-scikit-byo-iris') #subsitute this for your s3 bucket name.
obj = client.get_object(Bucket='demo-scikit-byo-iris', Key='foo.csv')
lines= obj['Body'].read().decode('utf-8').splitlines()
reader = csv.reader(lines)
import io
file = io.StringIO(lines)
# grab environment variables
runtime= boto3.client('runtime.sagemaker')
response = runtime.invoke_endpoint(
EndpointName= 'nilm2',
Body = file.getvalue(),
ContentType='*/*',
Accept = 'Accept')
output = response['Body'].read().decode('utf-8')
我的数据是 2 列浮点数的 csv 文件,没有标题,问题是行返回一个字符串列表(每一行都是这个列表的一个元素:['11.55,65.23', '55.68,69.56' ...]) 调用运行良好,但响应也是一个字符串: output = '65.23\n,65.23\n,22.56\n,...'
那么如何将这个输出保存为 csv 文件到 S3
谢谢
【问题讨论】:
-
如下所示,使用 SageMaker 批量转换。它更简单,成本更低。
标签: python amazon-s3 amazon-sagemaker