Spaces:
Sleeping
Sleeping
Kwasiasomani
commited on
Commit
•
2dd86d2
1
Parent(s):
f58d895
Upload 4 files
Browse files- app.py +82 -0
- random_search_model.pkl +3 -0
- requirements.txt +6 -0
- standard_scaler.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import pickle
|
6 |
+
from sklearn.preprocessing import StandardScaler
|
7 |
+
from sklearn.ensemble import RandomForestClassifier
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
# Load pre-trained model and scaler
|
13 |
+
with open('standard_scaler.pkl', 'rb') as scaler_file:
|
14 |
+
scaler = pickle.load(scaler_file)
|
15 |
+
|
16 |
+
with open('random_search_model.pkl', 'rb') as model_file:
|
17 |
+
model = pickle.load(model_file)
|
18 |
+
|
19 |
+
# Function to preprocess user input and make predictions
|
20 |
+
def predict_fraud(user_input):
|
21 |
+
# Separate the amount column
|
22 |
+
user_input_amount = user_input[-1]
|
23 |
+
user_input_features = user_input[:-1]
|
24 |
+
|
25 |
+
# Scale the amount column
|
26 |
+
user_input_amount_scaled = scaler.transform(np.array(user_input_amount).reshape(1, -1))
|
27 |
+
|
28 |
+
# Combine scaled amount with other features
|
29 |
+
user_input_scaled = np.concatenate((user_input_features, user_input_amount_scaled), axis=1)
|
30 |
+
prediction = model.predict(user_input_scaled)[0]
|
31 |
+
probability = model.predict_proba(user_input_scaled)[0][1]
|
32 |
+
return prediction, probability
|
33 |
+
|
34 |
+
# Function to generate charts
|
35 |
+
def generate_charts(prediction, probability, amount):
|
36 |
+
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
|
37 |
+
|
38 |
+
# Pie chart for prediction distribution
|
39 |
+
prediction_labels = ['Non-Fraudulent', 'Fraudulent']
|
40 |
+
prediction_values = [1 - prediction, prediction]
|
41 |
+
axes[0].pie(prediction_values, labels=prediction_labels, autopct='%1.1f%%', startangle=90, colors=['skyblue', 'lightcoral'])
|
42 |
+
axes[0].set_title('Prediction Distribution')
|
43 |
+
|
44 |
+
# Bar chart for probability distribution
|
45 |
+
axes[1].bar(['Probability'], [probability], color='lightgreen' if prediction == 0 else 'lightcoral')
|
46 |
+
axes[1].set_title('Probability of Fraud')
|
47 |
+
|
48 |
+
# Display amount
|
49 |
+
st.write(f"Transaction Amount: ${amount}")
|
50 |
+
|
51 |
+
# Display charts
|
52 |
+
st.pyplot(fig)
|
53 |
+
|
54 |
+
# Streamlit app
|
55 |
+
def main():
|
56 |
+
st.title("Fraud Detection Prediction App")
|
57 |
+
|
58 |
+
# User input fields
|
59 |
+
user_input = []
|
60 |
+
for i in range(1, 29):
|
61 |
+
user_input.append(st.number_input(f'V{i}', value=0.0))
|
62 |
+
|
63 |
+
min_amount = st.number_input('Minimum Amount', value=0.0)
|
64 |
+
max_amount = st.number_input('Maximum Amount', value=10000.0)
|
65 |
+
|
66 |
+
amount = st.number_input('Amount', min_value=min_amount, max_value=max_amount, value=(min_amount + max_amount) / 2)
|
67 |
+
|
68 |
+
# Make prediction on button click
|
69 |
+
if st.button('Predict'):
|
70 |
+
user_input_array = np.array(user_input)
|
71 |
+
user_input_array = np.append(user_input_array, amount)
|
72 |
+
prediction, probability = predict_fraud(user_input_array)
|
73 |
+
|
74 |
+
# Display prediction result
|
75 |
+
st.write(f"Prediction: {'Fraudulent Transaction' if prediction == 1 else 'Non-Fraudulent Transaction'}")
|
76 |
+
st.write(f"Probability: {probability:.2%}")
|
77 |
+
|
78 |
+
# Generate and display charts
|
79 |
+
generate_charts(prediction, probability, amount)
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
main()
|
random_search_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:84a5ee1ab2f4b04a768d3bac50d605fe1160149c91036c6704f6025344f00929
|
3 |
+
size 3025399
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
scikit-learn
|
2 |
+
streamlit
|
3 |
+
pandas
|
4 |
+
numpy
|
5 |
+
matplotlib
|
6 |
+
plotly
|
standard_scaler.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e54d791dea453fe62fdf8d07dc528e7b44fd30fbc370a581b7e9c2620fc568ac
|
3 |
+
size 129
|