added more utils to retrieve sus data and get true and fake model certainty
Browse files
utils.py
CHANGED
|
@@ -1,4 +1,9 @@
|
|
| 1 |
import streamlit.components.v1 as components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def ChangeButtonColour(widget_label, font_color, background_color='transparent'):
|
| 4 |
htmlstr = f"""
|
|
@@ -76,4 +81,39 @@ feature_texts = {0: "Day out of 30", 1: "Type of transaction: ", 2: "Amount tran
|
|
| 76 |
5: "Initial balance of recipient: ", 6: "New balance of recipient: ", 7: "Sender's balance was exactly credited: ",
|
| 77 |
8: "Receiver's balance was exactly credited: ", 9: "Transaction over 450.000: ", 10: "Frequent receiver of transactions: ", 11: "Receiver is merchant: ", 12: "Sender ID: ", 13: "Receiver ID: "}
|
| 78 |
|
| 79 |
-
example_input = {"instances":[[1,"PAYMENT",9839.64,170136,160296.36,0,0,1,1,0,0,1,84,2424]]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit.components.v1 as components
|
| 2 |
+
from random import randrange, uniform
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
import dill
|
| 6 |
+
from omnixai.data.tabular import Tabular
|
| 7 |
|
| 8 |
def ChangeButtonColour(widget_label, font_color, background_color='transparent'):
|
| 9 |
htmlstr = f"""
|
|
|
|
| 81 |
5: "Initial balance of recipient: ", 6: "New balance of recipient: ", 7: "Sender's balance was exactly credited: ",
|
| 82 |
8: "Receiver's balance was exactly credited: ", 9: "Transaction over 450.000: ", 10: "Frequent receiver of transactions: ", 11: "Receiver is merchant: ", 12: "Sender ID: ", 13: "Receiver ID: "}
|
| 83 |
|
| 84 |
+
example_input = {"instances":[[1,"PAYMENT",9839.64,170136,160296.36,0,0,1,1,0,0,1,84,2424]]}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def get_fake_certainty():
|
| 88 |
+
# Generate a random certainty between 75% and 99%
|
| 89 |
+
fake_certainty = uniform(0.75, 0.99)
|
| 90 |
+
formatted_fake_certainty = "{:.2%}".format(fake_certainty)
|
| 91 |
+
return formatted_fake_certainty
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def get_random_suspicious_transaction():
|
| 95 |
+
data = pd.read_pickle("data/preprocessed_data.pkl")
|
| 96 |
+
suspicious_data=data[data["isFraud"]==1]
|
| 97 |
+
max_n=len(suspicious_data)
|
| 98 |
+
random_nr=randrange(max_n)
|
| 99 |
+
suspicous_transaction = suspicious_data[random_nr-1:random_nr].drop("isFraud", axis=1)
|
| 100 |
+
return suspicous_transaction
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def get_model_certainty(data_point):
|
| 104 |
+
# load the trained model
|
| 105 |
+
model = joblib.load('model/model.joblib')
|
| 106 |
+
# load transformer
|
| 107 |
+
with open("transformer/transformer.dill", "rb") as f:
|
| 108 |
+
transformer = dill.load(f)
|
| 109 |
+
|
| 110 |
+
# create tabular object of datapoint and apply transformation
|
| 111 |
+
sample = Tabular(data_point, categorical_columns=[1])
|
| 112 |
+
transformed = transformer.transform(sample)
|
| 113 |
+
|
| 114 |
+
# get model certainty for sample and transform it to smth intelligble
|
| 115 |
+
probability = model.predict_proba(transformed)
|
| 116 |
+
positive_class_probability = probability[:, 1][0]
|
| 117 |
+
formatted_probability = "{:.5%}".format(positive_class_probability)
|
| 118 |
+
# Print the formatted probability
|
| 119 |
+
return formatted_probability
|