View a markdown version of this page

教程:开始使用 适用于 Python (Boto3) 的 AWS SDK - Amazon Fraud Detector

自2025年11月7日起,亚马逊欺诈探测器不再向新客户开放。要了解与亚马逊欺诈检测器类似的功能,请浏览亚马逊 SageMaker AutoGluon、和 AWS WAF。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

教程:开始使用 适用于 Python (Boto3) 的 AWS SDK

本教程介绍如何构建和训练 Amazon Fraud Detector 模型,然后使用该模型使用生成实时欺诈预测 适用于 Python (Boto3) 的 AWS SDK。使用您上传到 Amazon S3 存储桶的账户注册示例数据文件对模型进行训练。

在本教程结束时,您完成了以下操作:

  • 构建和训练亚马逊欺诈探测器模型

  • 生成实时欺诈预测

先决条件

以下是本教程的先决步骤。

开始使用

Boto 是适用于 Python 的 Amazon Web Services(AWS)SDK。您可以使用它来创建、配置和管理 AWS 服务。有关如何安装 Boto3 的说明,请参阅适用于 Python 的 AWS 开发工具包 (Boto3)。

安装后 适用于 Python (Boto3) 的 AWS SDK,运行以下 Python 示例命令以确认您的环境配置正确。如果您的环境配置正确,则响应中包含探测器列表。如果未创建任何探测器,则列表为空。

import boto3 fraudDetector = boto3.client('frauddetector') response = fraudDetector.get_detectors() print(response)

在此步骤中,您将创建用于定义模型、事件和规则的资源。

创建变量

变量是数据集中的一个数据元素,您要使用它来创建事件类型、模型和规则。

在以下示例中,CreateVariableAPI 用于创建两个变量。变量是email_address和ip_address。将它们分配给相应的变量类型:EMAIL_ADDRESS和IP_ADDRESS。这些变量是您上传的示例数据集的一部分。当您指定变量类型时,Amazon Fraud Detector 会在模型训练和获得预测时解释变量。只有具有关联变量类型的变量才能用于模型训练。

import boto3 fraudDetector = boto3.client('frauddetector') #Create variable email_address fraudDetector.create_variable( name = 'email_address', variableType = 'EMAIL_ADDRESS', dataSource = 'EVENT', dataType = 'STRING', defaultValue = '<unknown>' ) #Create variable ip_address fraudDetector.create_variable( name = 'ip_address', variableType = 'IP_ADDRESS', dataSource = 'EVENT', dataType = 'STRING', defaultValue = '<unknown>' )

创建实体类型

实体代表谁在执行活动,实体类型对该实体进行分类。示例分类包括客户、卖家或账户。

在以下示例中,PutEntityTypeAPI 用于创建sample_customer实体类型。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.put_entity_type( name = 'sample_customer', description = 'sample customer entity type' )

创建标签

标签将事件归类为欺诈性或合法事件,并用于训练欺诈检测模型。该模型学习使用这些标签值对事件进行分类。

在以下示例中,Putlabel API 用于创建两个标签,fraud和legit。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.put_label( name = 'fraud', description = 'label for fraud events' ) fraudDetector.put_label( name = 'legit', description = 'label for legitimate events' )

使用 Amazon Fraud Detector,您可以构建模型来评估风险并为个别事件生成欺诈预测。事件类型定义单个事件的结构。

在以下示例中,PutEventTypeAPI 用于创建事件类型sample_registration。您可以通过指定在上一步中创建的变量 (email_address、ip_addresssample_customer)、实体类型 () 和标签 (fraud,legit) 来定义事件类型。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.put_event_type ( name = 'sample_registration', eventVariables = ['ip_address', 'email_address'], labels = ['legit', 'fraud'], entityTypes = ['sample_customer'])

亚马逊欺诈探测器训练模型学会检测特定事件类型的欺诈行为。在上一步中,您创建了事件类型。在此步骤中,您将为事件类型创建和训练模型。该模型充当模型版本的容器。每次训练模型时,都会创建一个新版本。

使用以下示例代码创建和训练在线欺诈洞察模型。这个模型被称为sample_fraud_detection_model。它适用于事件类型,sample_registration使用您上传到 Amazon S3 的账户注册示例数据集。

有关亚马逊欺诈探测器支持的不同模型类型的更多信息,请参阅选择模型类型。

创建模型

在以下示例中,CreateModelAPI 用于创建模型。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.create_model ( modelId = 'sample_fraud_detection_model', eventTypeName = 'sample_registration', modelType = 'ONLINE_FRAUD_INSIGHTS')

训练模型

在以下示例中,CreateModelVersionAPI 用于训练模型。'EXTERNAL_EVENTS'为trainingDataSource和指定您存储示例数据集的 Amazon S3 位置以及 Amazon S3 存储桶RoleArn的存储位置externalEventsDetail。对于trainingDataSchema参数,指定亚马逊欺诈检测器如何解释示例数据。更具体地说,指定要包含哪些变量以及如何对事件标签进行分类。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.create_model_version ( modelId = 'sample_fraud_detection_model', modelType = 'ONLINE_FRAUD_INSIGHTS', trainingDataSource = 'EXTERNAL_EVENTS', trainingDataSchema = { 'modelVariables' : ['ip_address', 'email_address'], 'labelSchema' : { 'labelMapper' : { 'FRAUD' : ['fraud'], 'LEGIT' : ['legit'] } } }, externalEventsDetail = { 'dataLocation' : 's3://amzn-s3-demo-bucket/your-example-data-filename.csv', 'dataAccessRoleArn' : 'role_arn' } )

您可以多次训练模型。每次训练模型时,都会创建一个新版本。模型训练完成后,模型版本状态更新为TRAINING_COMPLETE。您可以查看模型性能分数和其他模型性能指标。

查看模型性能

使用 Amazon Fraud Detector 的一个重要步骤是使用模型分数和性能指标评估模型的准确性。模型训练完成后,Amazon Fraud Detector 使用未用于训练模型的 15% 的数据来验证模型性能。它生成模型性能分数和其他性能指标。

使用 DescribeModelVersions API 来查看模型性能。查看模型性能总分以及亚马逊欺诈检测器为此模型生成的所有其他指标。

要了解有关模型性能分数和性能指标的更多信息,请参阅模型分数和模型性能指标。

您可以期望所有经过训练的 Amazon Fraud Detector 模型都具有真实的欺诈检测性能指标,这些指标与本教程中的指标类似。

部署模型

在您查看了经过训练的模型的性能指标后,部署该模型并将其提供给 Amazon Fraud Detector 以生成欺诈预测。要部署经过训练的模型,请使用 UpdateModelVersionStatus API。在以下示例中,它用于将模型版本状态更新为 ACTIVE。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.update_model_version_status ( modelId = 'sample_fraud_detection_model', modelType = 'ONLINE_FRAUD_INSIGHTS', modelVersionNumber = '1.00', status = 'ACTIVE' )

探测器包含检测逻辑,例如模型和规则。此逻辑适用于您要评估是否存在欺诈的特定事件。规则是您指定的一项条件,用于告诉 Amazon Fraud Detector 如何在预测期间解释变量值。结果是欺诈预测的结果。探测器可以有多个版本,每个版本的状态为 “草稿” 、“活动” 或 “非活动” 。探测器版本必须至少有一条与之相关的规则。

使用以下示例代码创建探测器、规则、结果并发布检测器。

创建探测器

在以下示例中,PutDetectorAPI 用于为sample_registration事件类型创建sample_detector检测器。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.put_detector ( detectorId = 'sample_detector', eventTypeName = 'sample_registration' )

创造成果

为每个可能的欺诈预测结果创建结果。在以下示例中,PutOutcomeAPI 用于创建三个结果-verify_customer review、和approve。这些结果随后分配给规则。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.put_outcome( name = 'verify_customer', description = 'this outcome initiates a verification workflow' ) fraudDetector.put_outcome( name = 'review', description = 'this outcome sidelines event for review' ) fraudDetector.put_outcome( name = 'approve', description = 'this outcome approves the event' )

创建规则

规则由数据集中的一个或多个变量、逻辑表达式以及一个或多个结果组成。

在以下示例中,CreateRuleAPI 用于创建三种不同的规则:high_riskmedium_risk、和low_risk。创建规则表达式,将模型性能得分sample_fraud_detection_model_insightscore值与各种阈值进行比较。这是为了确定事件的风险级别并分配在上一步中定义的结果。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.create_rule( ruleId = 'high_fraud_risk', detectorId = 'sample_detector', expression = '$sample_fraud_detection_model_insightscore > 900', language = 'DETECTORPL', outcomes = ['verify_customer'] ) fraudDetector.create_rule( ruleId = 'medium_fraud_risk', detectorId = 'sample_detector', expression = '$sample_fraud_detection_model_insightscore <= 900 and $sample_fraud_detection_model_insightscore > 700', language = 'DETECTORPL', outcomes = ['review'] ) fraudDetector.create_rule( ruleId = 'low_fraud_risk', detectorId = 'sample_detector', expression = '$sample_fraud_detection_model_insightscore <= 700', language = 'DETECTORPL', outcomes = ['approve'] )

创建探测器版本

探测器版本定义了用于预测欺诈的模型和规则。

在以下示例中,CreateDetectorVersionAPI 用于创建探测器版本。它通过提供模型版本详细信息、规则和规则执行模式 FIRST_MATCHED 来实现此目的。规则执行模式指定了评估规则的顺序。规则执行模式 FIRST_MATCHED 指定按顺序对规则进行评估,从第一个匹配的规则开始计算。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.create_detector_version( detectorId = 'sample_detector', rules = [{ 'detectorId' : 'sample_detector', 'ruleId' : 'high_fraud_risk', 'ruleVersion' : '1' }, { 'detectorId' : 'sample_detector', 'ruleId' : 'medium_fraud_risk', 'ruleVersion' : '1' }, { 'detectorId' : 'sample_detector', 'ruleId' : 'low_fraud_risk', 'ruleVersion' : '1' } ], modelVersions = [{ 'modelId' : 'sample_fraud_detection_model', 'modelType': 'ONLINE_FRAUD_INSIGHTS', 'modelVersionNumber' : '1.00' } ], ruleExecutionMode = 'FIRST_MATCHED' )

本教程的最后一步使用在上一步中sample_detector创建的检测器实时生成sample_registration事件类型的欺诈预测。检测器评估上传到 Amazon S3 的示例数据。响应包括模型性能分数以及与匹配规则相关的任何结果。

在以下示例中,GetEventPredictionAPI 用于为每个请求提供来自单个账户注册的数据。在本教程中,从账户注册示例数据文件中获取数据(电子邮件地址和 ip_address)。顶部标题行之后的每一行(行)代表来自单个账户注册事件的数据。

import boto3 fraudDetector = boto3.client('frauddetector') fraudDetector.get_event_prediction( detectorId = 'sample_detector', eventId = '802454d3-f7d8-482d-97e8-c4b6db9a0428', eventTypeName = 'sample_registration', eventTimestamp = '2020-07-13T23:18:21Z', entities = [{'entityType':'sample_customer', 'entityId':'12345'}], eventVariables = { 'email_address': 'johndoe@exampledomain.com', 'ip_address': '1.2.3.4' } )

完成本教程后,您完成了以下操作:

  • 已将示例事件数据集上传到 Amazon S3。

  • 创建了用于创建和训练模型的变量、实体和标签。

  • 使用示例数据集创建和训练模型。

  • 查看了 Amazon Fraud Detector 生成的模型性能分数和其他性能指标。

  • 部署了欺诈检测模型。

  • 创建了探测器并添加了已部署的模型。

  • 为检测器添加了规则、规则执行顺序和结果。

  • 创建了探测器版本。

  • 通过提供不同的输入并检查规则和规则执行顺序是否按预期运行来测试探测器。

(可选)使用 Jupyter (IPython) 笔记本探索亚马逊欺诈探测器 API

有关如何使用亚马逊欺诈检测器 API 的更多示例,请参阅 aws-fraud-detector-s GitHub amples 存储库。笔记本涵盖的主题包括使用 Amazon Fraud Detector API 构建模型和检测器,以及使用 GetEventPrediction API 提出批量欺诈预测请求。