Untitled

Alert users on Slack if a prediction is made which is above a certain threshold, e.g. a fraudulent sales transaction is detected.

This feature provides users with real-time notifications of critical events and allows them to take immediate action. By setting the threshold for the alert, users can customize their notifications based on their specific needs and risk tolerance. With this feature, our users can stay informed and make decisions in a timely manner.

This tutorial is tailored for the Anomaly/Fraud Detection model, but you can modify it to work with other models as well.

Import required modules

from joblib import load # to load the existing saved model
import pandas as pd

Load new records

We will read new records, where the prediction is null. Make sure you add a “Prediction” column first in the spreadsheet view of the table.

my_query = 'select * from transactions where prediction is null limit 5'
dbconn = pq.dbconnect('dw_123')
df = dbconn.fetch('dw_123', query = my_query, df = True)

Data Preprocessing

For predicting, we have to prepare the data in the same format as it was trained on.

# Drop unwanted features
X = df.drop(['Class', 'Prediction'], axis=1)

Note: It's recommended to add a “Try/Except” statement, in case there are no records to update other wise it will throw errors in the future.

Load the model & predict

model = load('/data_app/model_credit_card') # loading the model

# Making prediction
pred = model.predict(X)

#Reshape the prediction values to 0 for Valid transactions, 1 for Fraud transactions
pred[pred == 1] = 0
pred[pred == -1] = 1

# Saving the prediction to the dataframe df
df['Prediction'] = pred