Kwasiasomani commited on
Commit
b180a08
·
verified ·
1 Parent(s): e9f4346

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -45
app.py CHANGED
@@ -1,32 +1,18 @@
1
- import streamlit as st
2
- import pandas as pd
3
- import numpy as np
4
- import pickle
5
- from sklearn.preprocessing import StandardScaler
6
- from sklearn.ensemble import RandomForestClassifier
7
- import matplotlib.pyplot as plt
8
- from PIL import Image
9
-
10
- # Load pre-trained model and scaler
11
- with open('standard_scaler.pkl', 'rb') as scaler_file:
12
- scaler = pickle.load(scaler_file)
13
-
14
- with open('random_forest_model.pkl', 'rb') as model_file:
15
- model = pickle.load(model_file)
16
-
17
  def predict_fraud(user_input):
18
  user_input_amount = user_input['Amount']
19
- user_input_features = user_input.drop(columns=['Amount'])
 
20
 
21
- # Scale the amount column
22
  user_input_amount_scaled = scaler.transform(np.array(user_input_amount).reshape(-1, 1))
 
23
 
24
  # Reshape user_input_features if necessary
25
  if len(user_input_features.shape) == 1:
26
  user_input_features = user_input_features.values.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
 
31
  # Ensure user_input_scaled is a 2D array
32
  if len(user_input_scaled.shape) == 1:
@@ -37,27 +23,6 @@ def predict_fraud(user_input):
37
  probability = model.predict_proba(user_input_scaled)[0][1]
38
  return prediction, probability
39
 
40
- # Function to generate charts
41
- def generate_charts(prediction, probability, amount):
42
- fig, axes = plt.subplots(1, 2, figsize=(12, 6))
43
-
44
- # Pie chart for prediction distribution
45
- prediction_labels = ['Non-Fraudulent', 'Fraudulent']
46
- prediction_values = [1 - prediction, prediction]
47
- axes[0].pie(prediction_values, labels=prediction_labels, autopct='%1.1f%%', startangle=90, colors=['skyblue', 'lightcoral'])
48
- axes[0].set_title('Prediction Distribution')
49
-
50
- # Bar chart for probability distribution
51
- axes[1].bar(['Probability'], [probability], color='lightgreen' if prediction == 0 else 'lightcoral')
52
- axes[1].set_title('Probability of Fraud')
53
-
54
- # Display amount
55
- st.write(f"Transaction Amount: ${amount}")
56
-
57
- # Display charts
58
- st.pyplot(fig)
59
-
60
-
61
  # Streamlit app
62
  def main():
63
  # Set page layout
@@ -77,15 +42,20 @@ def main():
77
  max_amount = st.sidebar.number_input('Maximum Amount', value=10000.0, step=0.01)
78
  amount = st.sidebar.number_input('Amount', min_value=min_amount, max_value=max_amount, value=(min_amount + max_amount) / 2, step=0.01)
79
 
 
 
 
 
80
  # Descriptions and explanations
81
  st.markdown("# Fraud Detection Prediction App")
82
  st.markdown("This app predicts the likelihood of a transaction being fraudulent based on input features.")
83
- st.markdown("The input features include V1 to V28, representing anonymized numerical features, and the transaction amount.")
84
 
85
  # Make prediction on button click
86
  if st.sidebar.button('Predict'):
87
  user_input_df = pd.DataFrame([user_input]) # Convert list of dictionaries to DataFrame
88
  user_input_df['Amount'] = amount
 
89
  prediction, probability = predict_fraud(user_input_df)
90
 
91
  # Display prediction result
@@ -95,6 +65,3 @@ def main():
95
 
96
  # Generate, display, and save charts
97
  generate_charts(prediction, probability, amount)
98
-
99
- if __name__ == "__main__":
100
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def predict_fraud(user_input):
2
  user_input_amount = user_input['Amount']
3
+ user_input_time = user_input['Time']
4
+ user_input_features = user_input.drop(columns=['Amount', 'Time'])
5
 
6
+ # Scale the amount and time columns
7
  user_input_amount_scaled = scaler.transform(np.array(user_input_amount).reshape(-1, 1))
8
+ user_input_time_scaled = scaler.transform(np.array(user_input_time).reshape(-1, 1))
9
 
10
  # Reshape user_input_features if necessary
11
  if len(user_input_features.shape) == 1:
12
  user_input_features = user_input_features.values.reshape(1, -1)
13
 
14
+ # Combine scaled amount and time with other features
15
+ user_input_scaled = np.concatenate((user_input_features, user_input_amount_scaled, user_input_time_scaled), axis=1)
16
 
17
  # Ensure user_input_scaled is a 2D array
18
  if len(user_input_scaled.shape) == 1:
 
23
  probability = model.predict_proba(user_input_scaled)[0][1]
24
  return prediction, probability
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Streamlit app
27
  def main():
28
  # Set page layout
 
42
  max_amount = st.sidebar.number_input('Maximum Amount', value=10000.0, step=0.01)
43
  amount = st.sidebar.number_input('Amount', min_value=min_amount, max_value=max_amount, value=(min_amount + max_amount) / 2, step=0.01)
44
 
45
+ min_time = st.sidebar.number_input('Minimum Time', value=0.0, step=1)
46
+ max_time = st.sidebar.number_input('Maximum Time', value=172800.0, step=1)
47
+ time = st.sidebar.number_input('Time', min_value=min_time, max_value=max_time, value=(min_time + max_time) / 2, step=1)
48
+
49
  # Descriptions and explanations
50
  st.markdown("# Fraud Detection Prediction App")
51
  st.markdown("This app predicts the likelihood of a transaction being fraudulent based on input features.")
52
+ st.markdown("The input features include V1 to V28, representing anonymized numerical features, and the transaction amount and time.")
53
 
54
  # Make prediction on button click
55
  if st.sidebar.button('Predict'):
56
  user_input_df = pd.DataFrame([user_input]) # Convert list of dictionaries to DataFrame
57
  user_input_df['Amount'] = amount
58
+ user_input_df['Time'] = time
59
  prediction, probability = predict_fraud(user_input_df)
60
 
61
  # Display prediction result
 
65
 
66
  # Generate, display, and save charts
67
  generate_charts(prediction, probability, amount)