python - SageMaker 模型中的自定义特征工程
问题描述
我在 SageMaker 中使用 Batch Transform 来调用存储的 XGBoost 模型并对存储在 S3 中的数据进行评分。但是,在调用模型之前,我必须对列进行几次转换。
以下是用于批量转换的代码:
batch_input = 's3://{}/{}/batch/test_data_Batch.csv'.format(bucket,prefix) # test data used for prediction
batch_output = 's3://{}/{}/batch/batch-inference/test_data_Batch.csv.out'.format(bucket,prefix)
Modelname = '<your_model_name_here>' # the model name we already have
transformJobName = 'DEMO-xgboost-churn-call-batch'+ strftime("%Y-%m-%d-%H-%M-%S", gmtime())
client = boto3.client('sagemaker')
create_batch = client.create_transform_job(
TransformJobName=transformJobName,
ModelName=Modelname,
MaxConcurrentTransforms=0,
MaxPayloadInMB=6,
BatchStrategy='MultiRecord',
TransformInput={
'DataSource': {
'S3DataSource': {
'S3DataType': 'S3Prefix',
'S3Uri': batch_input
}
},
'ContentType': 'text/csv',
'CompressionType': 'None',
'SplitType': 'Line'
},
TransformOutput={
'S3OutputPath': batch_output,
'AssembleWith': 'Line'
},
TransformResources={
'InstanceType': 'ml.m4.xlarge',
'InstanceCount': 1
}
)
在使用 XGBoost 模型运行批量转换之前,如何调用特征工程 Python 函数来转换数据?一些指针会有所帮助。谢谢你。
解决方案
为迟到的回应道歉。
我相信您可以采取一些方法在 SageMaker 中结合 Batch 进行一些预处理。
我假设您使用的是 SageMaker 提供的 XGBoost Image:https ://github.com/aws/sagemaker-xgboost-container 。
- 如果您使用的是开源 SageMaker XGBoost 容器,则可以修改您的模型图像脚本来处理您的预处理和后处理。这将要求您扩展或构建 XGBoost 映像并将其作为 ECR 映像托管在您的 AWS 账户中。
有关扩展框架图像的示例,请参见:https ://docs.aws.amazon.com/sagemaker/latest/dg/prebuilt-containers-extend.html
扩展图像后,您可能需要修改用户脚本来处理您的预处理。
XGBoost 容器中的用户脚本示例:https ://github.com/aws/sagemaker-xgboost-container/blob/master/test/resources/boston/single_machine_customer_script.py
这可能需要一些时间,我建议使用 Python SDK 和本地模式来加快迭代速度:https ://github.com/aws-samples/amazon-sagemaker-local-mode 。
- 在使用 SageMaker 批处理之前,利用 SageMaker 推理管道拥有一个单独的图像来处理数据预处理。
与上面的方法类似,但是在这种情况下,我们将生成一个单独的图像来单独处理我们的数据,然后将该数据传递给我们的原始模型。
根据您的方法,这可能需要扩展现有容器或创建一个完全具有所需依赖项的新容器。
https://docs.aws.amazon.com/sagemaker/latest/dg/inference-pipeline-batch.html
- 还有 SageMaker 处理
推荐阅读
- autodesk-forge - 有没有办法在 Autodesk Forge Reality Capture API 上检查每次重建 (photoscene_id) 消耗的云积分?
- sapui5 - 使用聚合增长表
- android - 需要思考?我是否正确清理了我的 ViewModel 类?
- c# - NullReferenceException:如何找到空值
- css - 如何使用 CSS 覆盖禁用的 mat-menu-item 的行为?
- python - 如何快速将大量数据从二进制文件转换为 1 和 0 字符串
- ubuntu - Ubuntu上的npm安装错误:拒绝将rxjs安装为自身的依赖项
- c# - 从私钥计算公钥不正确
- google-maps - 如何将数据绑定到使用 this.$createElement 创建的元素
- r - 在 R 中的 Probit 模型中计算 α 和 β