luisosorio3214 commited on
Commit
0b54fd7
1 Parent(s): c96eb36

added files

Browse files
Files changed (3) hide show
  1. app.py +112 -0
  2. boost.sav +0 -0
  3. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import shap
3
+ import pickle
4
+ import pandas as pd
5
+ import matplotlib.pyplot as plt
6
+ from pathlib import Path
7
+
8
+ # load our models
9
+ boost_path = Path(__file__).parents[0] / "boost.sav"
10
+ boost = pickle.load(open(boost_path,"rb"))
11
+
12
+ # functions
13
+
14
+ # preprocessing data function
15
+ def preprocess(data):
16
+ columns = ['distance_from_home', 'distance_from_last_transaction',
17
+ 'ratio_to_median_purchase_price', 'repeat_retailer', 'used_chip',
18
+ 'used_pin_number', 'online_order']
19
+
20
+ df = pd.DataFrame([data], columns = columns)
21
+
22
+ # convert data type
23
+ df[['repeat_retailer','used_chip','used_pin_number','online_order']] = df[['repeat_retailer','used_chip','used_pin_number','online_order']].astype('int')
24
+
25
+ return df
26
+
27
+
28
+ # Prediction function with probabilities
29
+ def predict(*data):
30
+ df = preprocess(data)
31
+ prob_pred = boost.predict_proba(df)
32
+ return {"Normal": float(prob_pred[0][0]), "Fraud": float(prob_pred[0][1])}
33
+
34
+ # plot function
35
+ def interpret(*data):
36
+ plt.style.use("fivethirtyeight")
37
+
38
+ df = preprocess(data)
39
+
40
+ explainer = shap.TreeExplainer(boost)
41
+ shap_values = explainer.shap_values(df)
42
+ scores_desc = list(zip(shap_values[0], df.columns))
43
+ scores_desc = sorted(scores_desc)
44
+ fig_m = plt.figure(tight_layout=True)
45
+ plt.barh([s[1] for s in scores_desc], [s[0] for s in scores_desc])
46
+ plt.title("Feature Shap Values")
47
+ plt.ylabel("Shap Value")
48
+ plt.xlabel("Feature Importance")
49
+ plt.tight_layout()
50
+
51
+ return fig_m
52
+
53
+
54
+
55
+ with gr.Blocks() as demo:
56
+ gr.HTML("""
57
+ <h1 align="center">Credit Card Fraud Prediction System</h1>
58
+ """)
59
+ with gr.Row():
60
+ with gr.Column():
61
+ repeated_retailer = gr.Radio(["No","Yes"], type = "index", label = "Repeat Retailer", info ="Was the transaction at at a repeated store?")
62
+
63
+ online_order = gr.Radio(["No","Yes"], type = "index", label = "Online Order", info ="Was the transaction an online order?")
64
+
65
+ used_chip = gr.Radio(["No","Yes"], type = "index", label = "Used Chip", info ="did the purchase use the security chip of the card?")
66
+
67
+ used_pin = gr.Radio(["No","Yes"], type = "index", label = "Used Pin Number", info ="Did the transaction use the pin code of the card?")
68
+
69
+ distance_home = gr.Number(value = 25, label = "Distance From Home (miles)", info = "How far was the transaction from the card owner's house? (in Miles)")
70
+
71
+ distance_last = gr.Number(value = 5, label = "Distance From Last Transaction (miles)", info = "How far away was the it from the last transaction that happened? (in Miles)")
72
+
73
+ gr.HTML("""
74
+ <h4 align="center">Ratio Median Purchase Price Equation</h4>
75
+ """)
76
+ ratio_median = gr.Number(value = 1.8, label = "Ratio Median Purchase Price", info = "Divide the purchase price by card owners median purchase price?")
77
+
78
+
79
+ with gr.Column():
80
+ label = gr.Label()
81
+ plot = gr.Plot()
82
+ with gr.Row():
83
+ predict_btn = gr.Button(value="Predict")
84
+ interpret_btn = gr.Button(value="Explain")
85
+ predict_btn.click(
86
+ predict,
87
+ inputs= [
88
+ distance_home,
89
+ distance_last,
90
+ ratio_median,
91
+ repeated_retailer,
92
+ used_chip,
93
+ used_pin,
94
+ online_order
95
+ ],
96
+ outputs=[label],
97
+ )
98
+ interpret_btn.click(
99
+ interpret,
100
+ inputs=[
101
+ distance_home,
102
+ distance_last,
103
+ ratio_median,
104
+ repeated_retailer,
105
+ used_chip,
106
+ used_pin,
107
+ online_order
108
+ ],
109
+ outputs=[plot],
110
+ )
111
+
112
+ demo.launch()
boost.sav ADDED
Binary file (159 kB). View file
 
requirements.txt ADDED
Binary file (3.81 kB). View file