Amazon Fraud Detector — First Hands On Experience

Amazon Fraud Detector

Amazon Fraud Detector is a fully managed service that helps you detect suspicious online activities such as the creation of fake accounts and online payment fraud. This service was launched during the recent AWS Summit in 2019. With Amazon Fraud Detector, you can create a fraud detection ML model with just a few clicks and use it to evaluate online activities in milliseconds.

Working with Amazon Fraud Detector

The steps for creating a model, building a detector, and getting fraud predictions includes:

1.Gather historical fraud data (training data)
2.Create a model version (trained model)
3.Review model performance and deploy the model
4.Create a detector that includes the deployed model version , decision rules , variables and outcomes.
5.Finally , Send events to Amazon Fraud Detector and get a fraud prediction

Getting Started

PS: At the time of writing this article , the default boto3 supported in tensorflow_p36 kernel ( in Sagemaker) was <1.10.39 and to ensure FraudDetector methods are included we may need to update the conda packages.
 client = boto3.client('frauddetector', region_name='us-east-1')

Step 1 : Define Model Details

The method put_model would create / update a new model. And the following parameters can be configured

1. Model template to Use ( Currently 'Online Fraud Insights' is the only enabled option)
2. Data Location & IAM Role to Access data
3. Model Variables in the sample data
4. Fraud Label Schema Mapping.

Step 1a : Create & Activate Model Version

Once the model is created , lets create a new model version. By creating the model version , the model gets trained and ready to be consumed once the status changes to TRAINING_COMPLETE

response = client.create_model_version(
modelId=_modelId,
modelType=_modelType,
description='creating a new version of the given model'
)
response = client.update_model_version(
modelId=_modelId,
modelType=_modelType,
modelVersionNumber='1.0',
description='Activate the model',
status='ACTIVE'
)

Review the Trained Model’s Performance

Now that the model is trained and Activated , let’s quickly check the performance metrics

Step 2 : Create a Detector

We use a detector to house your fraud prediction configurations

response = client.put_detector(
detectorId=_detectorId,
description='AWS fraud detector - Creating new detector'
)
print(response)
client.put_outcome( name='verify_fraudulent_customer',description='Need a verification for this transaction.')
client.put_outcome( name='approve_customer',description='Low risk customer , approve the transaction')
client.put_outcome( name='review_customer',description='review this transaction')
response = client.create_detector_version(
detectorId = _detectorId,
description = 'Creating new detector version for' + _detectorId ,
rules=[
{
"detectorId": _detectorId,
"ruleId": "high_risk",
"ruleVersion": "1.0"
},
{
"detectorId": _detectorId,
"ruleId": "medium_risk",
"ruleVersion": "1.0"
},
{
"detectorId": _detectorId,
"ruleId": "low_risk",
"ruleVersion": "1.0"
}
],
modelVersions = [
{
'modelId' : _modelId,
'modelType' : _modelType,
'modelVersionNumber' : '1.0'
},
]
)
print(json.dumps(response, indent=2))
response = client.get_prediction(
detectorId=_detectorId,
detectorVersionId='2.0',
eventId = "b453a710-fcec-4c7d-9ce4-2ef2daccd615",
eventAttributes = {"billing_name":"null",
"billing_address_1":"null",
"billing_city":"null",
"shipping_address_1":"null",
"shipping_city":"null",
"shipping_state":"null",
"shipping_postal":"null",
"order_amt":"585.05",
"ip_address":"192.3.33.20",
"email_address":"null",
"user_agent":"null",
"avs_code":"K",
"phone_number":"null",
"billing_state":"null",
"billing_postal":"null",
"event_timestamp":"12/20/2018 20:03",
"transaction_id":"712-36182",
"payment_instrument":"null"}
)
print(json.dumps(response, indent=2))
{
"outcomes": [
"verify_fraudulent_customer"
],
"modelScores": [
{
"modelVersion": {
"modelId": "aws_frauddetector_demo",
"modelType": "ONLINE_FRAUD_INSIGHTS",
"modelVersionNumber": "1.0"
},
"scores": {
"aws_frauddetector_demo_insightscore": 892.0
}
}
],
"ResponseMetadata": {
"RequestId": "167af50c-c286-449b-ba2a-0783c8a8e0f5",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"content-type": "application/x-amz-json-1.1",
"date": "Tue, 07 Jan 2020 04:47:48 GMT",
"x-amzn-requestid": "167af50c-c286-449b-ba2a-0783c8a8e0f5",
"content-length": "231",
"connection": "keep-alive"
},
"RetryAttempts": 0
}
}
response = client.get_prediction(
detectorId=_detectorId,
detectorVersionId='2.0',
eventId = "b453a710-fcec-4c7d-9ce4-2ef2daccd619",
eventAttributes = {"billing_name":"null",
"billing_address_1":"null",
"billing_city":"null",
"shipping_address_1":"null",
"shipping_city":"null",
"shipping_state":"null",
"shipping_postal":"null",
"order_amt":"100",
"ip_address":"192.168.1.1",
"email_address":"null",
"user_agent":"null",
"avs_code":"K",
"phone_number":"null",
"billing_state":"null",
"billing_postal":"null",
"event_timestamp":"null",
"transaction_id":"2342342",
"payment_instrument":"null"}
)
print(json.dumps(response, indent=2))
{
"outcomes": [
"verify_fraudulent_customer"
],
"modelScores": [
{
"modelVersion": {
"modelId": "aws_frauddetector_demo",
"modelType": "ONLINE_FRAUD_INSIGHTS",
"modelVersionNumber": "1.0"
},
"scores": {
"aws_frauddetector_demo_insightscore": 999.0
}
}
],
"ResponseMetadata": {
"RequestId": "988b2e86-51e5-441c-84b0-08dcda336dbb",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"content-type": "application/x-amz-json-1.1",
"date": "Tue, 07 Jan 2020 04:49:51 GMT",
"x-amzn-requestid": "988b2e86-51e5-441c-84b0-08dcda336dbb",
"content-length": "231",
"connection": "keep-alive"
},
"RetryAttempts": 0
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store