How to detect fraud in transactions with Azure ML: step by step
This tutorial shows how to detect fraud in transactions with Azure Machine Learning, useful to reduce losses and automate anomaly handling in payments. You will learn why to use features, training and deployment to obtain real-time predictions.
Prerequisites
- Azure account with an active subscription and permissions to create resources.
- Azure Machine Learning workspace created.
- Python 3.8+ installed locally and pip; packages: azure-ai-ml, azure-ml, scikit-learn, pandas.
- Transaction dataset in CSV (columns: transaction_id, amount, timestamp, merchant, customer_id, label).
- Basic familiarity with ML (features, training, evaluation).
Step 1: Prepare the environment and load the dataset
Create a Python environment and install the libraries. Then connect to the workspace and load the CSV into a DataFrame. It is important to validate types and values in fields such as amount and timestamp.
pip install azure-ai-ml azure-identity scikit-learn pandas joblib
from azure.ai.ml import MLClient
from azure.identity import DefaultAzureCredential
import pandas as pd
ws = MLClient(DefaultAzureCredential(), subscription_id="", resource_group_name="", workspace_name="")
df = pd.read_csv('transactions.csv')
df['timestamp'] = pd.to_datetime(df['timestamp'])
print(df.head())
Step 2: Preprocessing and feature engineering
Explain why: well-constructed features improve fraud detection (for example, customer frequency, average amount, time of day). Create columns such as hour, txn_per_customer, avg_amount_customer. Handle missing values and normalize amount.
# Exemplo simples de features
import numpy as np
df['hour'] = df['timestamp'].dt.hour
cust_stats = df.groupby('customer_id')['amount'].agg(['count','mean']).rename(columns={'count':'txn_count','mean':'avg_amount'})
df = df.merge(cust_stats, on='customer_id')
df['amount_norm'] = (df['amount'] - df['avg_amount']) / (df['amount'].std() + 1e-9)
features = ['amount_norm','hour','txn_count']
X = df[features].fillna(0)
y = df['label'] # 1 = fraud, 0 = legit
Step 3: Train a classification model
Why: tree-based models work well with tabular features. Here we use RandomForest for a quick example. Save the model with joblib for deployment.
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import joblib
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
model = RandomForestClassifier(n_estimators=100, class_weight='balanced', random_state=42)
model.fit(X_train, y_train)
preds = model.predict(X_test)
print(classification_report(y_test, preds))
joblib.dump(model, 'fraud_rf.joblib')
Step 4: Register and package the model in Azure Machine Learning
Register the model file in the workspace for version management. Create a runtime environment (conda) and a simple scoring script that loads the model and predicts the label.
from azure.ai.ml.entities import Model
model_aml = Model(path='fraud_rf.joblib', name='fraud-rf-model', description='RandomForest for fraud detection')
registered = ws.models.create_or_update(model_aml)
print('Model registered:', registered.name)
Step 5: Create a container image / inference endpoint
Why: an endpoint enables real-time predictions. Define the scoring script that processes JSON with the same features and returns a probability. Then create a deployment as a Managed Online Endpoint or compute that supports the container.
# scoring.py (simplified)
import json
import joblib
import numpy as np
def init():
global model
model = joblib.load('fraud_rf.joblib')
def run(raw_data):
data = json.loads(raw_data)
X = np.array([data['amount_norm'], data['hour'], data['txn_count']]).reshape(1,-1)
pred = model.predict_proba(X)[0,1]
return {'fraud_probability': float(pred)}
Step 6: Test locally and deploy to Azure
Test the scoring script locally with a JSON example. Then create the Managed Online Endpoint in Azure ML and deploy the container with the image that includes scoring.py and the registered model.
# Test local (example)
import requests, json
payload = {'amount_norm':0.5,'hour':2,'txn_count':10}
print(run(json.dumps(payload)))
# In Azure: create endpoint and deployment via Azure ML CLI/SDK (summary):
# az ml online-endpoint create -n fraud-endpoint -f endpoint.yml
# az ml online-deployment create -n deployment1 --endpoint fraud-endpoint -f deployment.yml
Verify the result
Confirm that the endpoint responds and that precision/recall are acceptable. Send real examples and check probabilities for known frauds. Monitor metrics (TP, FP) and latency in the Azure Machine Learning dashboard.
Conclusion
You have created a complete pipeline for fraud detection in transactions with Azure Machine Learning: preparation, training, registration and deployment of an inference endpoint. Next steps: tune features, try XGBoost or LightGBM, and integrate alerts. Tip: validate with recent data and implement dynamic thresholds to reduce false positives.