Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import pickle | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.ensemble import RandomForestClassifier | |
import matplotlib.pyplot as plt | |
# Load pre-trained model and scaler | |
with open('scalers.pkl', 'rb') as scaler_file: | |
scaler = pickle.load(scaler_file) | |
with open('rf_random_model.pkl', 'rb') as model_file: | |
model = pickle.load(model_file) | |
def predict_fraud(user_input): | |
user_input_amount = user_input['Amount'] | |
user_input_time = user_input['Time'] | |
user_input_features = user_input.drop(columns=['Amount', 'Time']) | |
# Scale the amount and time columns | |
user_input_amount_scaled = scaler.transform(np.array(user_input_amount).reshape(-1, 1)) | |
user_input_time_scaled = scaler.transform(np.array(user_input_time).reshape(-1, 1)) | |
# Reshape user_input_features if necessary | |
if len(user_input_features.shape) == 1: | |
user_input_features = user_input_features.values.reshape(1, -1) | |
# Combine scaled amount and time with other features | |
user_input_scaled = np.concatenate((user_input_features, user_input_amount_scaled, user_input_time_scaled), axis=1) | |
# Ensure user_input_scaled is a 2D array | |
if len(user_input_scaled.shape) == 1: | |
user_input_scaled = user_input_scaled.reshape(1, -1) | |
# Make predictions | |
prediction = model.predict(user_input_scaled)[0] | |
probability = model.predict_proba(user_input_scaled)[0][1] | |
return prediction, probability | |
def generate_charts(prediction, probability, amount): | |
fig, axes = plt.subplots(1, 2, figsize=(12, 6)) | |
# Pie chart for prediction distribution | |
prediction_labels = ['Non-Fraudulent', 'Fraudulent'] | |
prediction_values = [1 - prediction, prediction] | |
axes[0].pie(prediction_values, labels=prediction_labels, autopct='%1.1f%%', startangle=90, colors=['skyblue', 'lightcoral']) | |
axes[0].set_title('Prediction Distribution') | |
# Bar chart for probability distribution | |
axes[1].bar(['Probability'], [probability], color='lightgreen' if prediction == 0 else 'lightcoral') | |
axes[1].set_title('Probability of Fraud') | |
# Display amount | |
st.write(f"Transaction Amount: ${amount}") | |
# Display charts | |
st.pyplot(fig) | |
def main(): | |
# Set page layout | |
st.set_page_config( | |
page_title="Fraud Detection Prediction App", | |
layout="wide", | |
initial_sidebar_state="collapsed" | |
) | |
# Sidebar - User input fields | |
st.sidebar.title("Input Parameters") | |
user_input = {} | |
for i in range(1, 29): | |
user_input[f'V{i}'] = st.sidebar.number_input(f'V{i}', value=0.0) | |
min_amount = st.sidebar.number_input('Minimum Amount', value=0.0, step=0.01) | |
max_amount = st.sidebar.number_input('Maximum Amount', value=10000.0, step=0.01) | |
amount = st.sidebar.number_input('Amount', min_value=min_amount, max_value=max_amount, value=(min_amount + max_amount) / 2, step=0.01) | |
min_time = st.sidebar.number_input('Minimum Time', value=0.0, step=1) | |
max_time = st.sidebar.number_input('Maximum Time', value=172800.0, step=1) | |
time = st.sidebar.number_input('Time', min_value=min_time, max_value=max_time, value=(min_time + max_time) / 2, step=1) | |
# Descriptions and explanations | |
st.markdown("# Fraud Detection Prediction App") | |
st.markdown("This app predicts the likelihood of a transaction being fraudulent based on input features.") | |
st.markdown("The input features include V1 to V28, representing anonymized numerical features, and the transaction amount and time.") | |
# Make prediction on button click | |
if st.sidebar.button('Predict'): | |
user_input_df = pd.DataFrame([user_input]) # Convert list of dictionaries to DataFrame | |
user_input_df['Amount'] = amount | |
user_input_df['Time'] = time | |
prediction, probability = predict_fraud(user_input_df) | |
# Display prediction result | |
st.markdown("# Prediction Result") | |
st.write(f"Prediction: {'Fraudulent Transaction' if prediction == 1 else 'Non-Fraudulent Transaction'}") | |
st.write(f"Probability: {probability:.2%}") | |
# Generate, display, and save charts | |
generate_charts(prediction, probability, amount) | |
if __name__ == "__main__": | |
main() | |