Spaces:
Running
Running
uploading application files
Browse files- .gitattributes +2 -0
- app.py +85 -0
- fraud_data.csv +0 -0
- requirements.txt +5 -0
- venv/Scripts/python.exe +3 -0
- venv/Scripts/streamlit.exe +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
venv/Scripts/python.exe filter=lfs diff=lfs merge=lfs -text
|
37 |
+
venv/Scripts/streamlit.exe filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.ensemble import RandomForestClassifier
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.metrics import accuracy_score, classification_report
|
6 |
+
import streamlit as st
|
7 |
+
import altair as alt
|
8 |
+
|
9 |
+
try:
|
10 |
+
# Load the data
|
11 |
+
df = pd.read_csv("fraud_data.csv")
|
12 |
+
|
13 |
+
# Prepare the data for the model
|
14 |
+
X = df[['TransactionAmount', 'CustomerAge', 'TransactionFrequency']]
|
15 |
+
y = df['IsFraud']
|
16 |
+
|
17 |
+
except FileNotFoundError:
|
18 |
+
st.write("Error: Data file not found.")
|
19 |
+
st.stop()
|
20 |
+
|
21 |
+
except Exception as e:
|
22 |
+
st.write(f"An error occurred: {e}")
|
23 |
+
st.stop()
|
24 |
+
|
25 |
+
# Split the data into training and testing sets
|
26 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
27 |
+
|
28 |
+
# Create and train a Random Forest Classifier model
|
29 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
30 |
+
model.fit(X_train, y_train)
|
31 |
+
|
32 |
+
# Make predictions on the testing set
|
33 |
+
y_pred = model.predict(X_test)
|
34 |
+
|
35 |
+
# Evaluate the model's performance
|
36 |
+
accuracy = accuracy_score(y_test, y_pred)
|
37 |
+
report = classification_report(y_test, y_pred, output_dict=True)
|
38 |
+
|
39 |
+
# Create a Streamlit app
|
40 |
+
st.title("Fraud Detection System")
|
41 |
+
|
42 |
+
# Create tabs
|
43 |
+
tab1, tab2, tab3 = st.tabs(["Data Visualization", "Model Performance", "Fraud Prediction"])
|
44 |
+
|
45 |
+
# Tab 1: Data Visualization
|
46 |
+
with tab1:
|
47 |
+
st.write("### Fraud Data")
|
48 |
+
st.write(df)
|
49 |
+
|
50 |
+
# Scatter plot
|
51 |
+
st.write("### Scatter Plot of Features")
|
52 |
+
for col in ['TransactionAmount', 'CustomerAge', 'TransactionFrequency']:
|
53 |
+
st.write(f"**{col} vs Fraudulent Transactions**")
|
54 |
+
st.altair_chart(
|
55 |
+
alt.Chart(df).mark_circle().encode(
|
56 |
+
x=col,
|
57 |
+
y='IsFraud',
|
58 |
+
tooltip=[col, 'IsFraud']
|
59 |
+
).interactive(),
|
60 |
+
use_container_width=True
|
61 |
+
)
|
62 |
+
|
63 |
+
# Tab 2: Model Performance
|
64 |
+
with tab2:
|
65 |
+
st.write("### Model Performance")
|
66 |
+
st.write(f"Accuracy: {accuracy:.2f}")
|
67 |
+
st.write("Classification Report:")
|
68 |
+
st.json(report)
|
69 |
+
|
70 |
+
# Tab 3: Fraud Prediction
|
71 |
+
with tab3:
|
72 |
+
st.write("### Predict Fraudulent Transactions")
|
73 |
+
amount_input = st.number_input("Transaction Amount", min_value=1.0, value=100.0, step=1.0)
|
74 |
+
age_input = st.number_input("Customer Age", min_value=18, value=30, step=1)
|
75 |
+
frequency_input = st.slider("Transaction Frequency (past month)", min_value=1, max_value=100, value=5, step=1)
|
76 |
+
|
77 |
+
if st.button("Predict"):
|
78 |
+
# Create input array for prediction
|
79 |
+
input_data = [[amount_input, age_input, frequency_input]]
|
80 |
+
|
81 |
+
# Make prediction
|
82 |
+
prediction = model.predict(input_data)[0]
|
83 |
+
result = "Fraudulent" if prediction == 1 else "Legitimate"
|
84 |
+
|
85 |
+
st.write(f"### Prediction: {result}")
|
fraud_data.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas
|
3 |
+
numpy
|
4 |
+
scikit-learn
|
5 |
+
altair
|
venv/Scripts/python.exe
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9b0bffb7a259cd2722df454fdfff41ee13665820cff1f578b1d97d31f9ef93d5
|
3 |
+
size 270616
|
venv/Scripts/streamlit.exe
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ff3d3c12bf95cde3a585c01215ee2134fd2c3075dfd7c58641cb4d4d1aa24461
|
3 |
+
size 108475
|