Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- .gitattributes +2 -0
- AnomlyDetection.ipynb +0 -0
- app.py +79 -0
- credit fraud anomaly detection.pdf +3 -0
- creditcard.csv +3 -0
- model.pkl +3 -0
- requirements.txt +6 -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 |
+
credit[[:space:]]fraud[[:space:]]anomaly[[:space:]]detection.pdf filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
creditcard.csv filter=lfs diff=lfs merge=lfs -text
|
AnomlyDetection.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pickle
|
| 4 |
+
from sklearn.ensemble import IsolationForest # Example model, replace if using a different one
|
| 5 |
+
|
| 6 |
+
st.markdown("<h1 style='text-align: center; font-size: 48px; color: red;'>Credit Card Fraud Anomaly Detection ML App</h1>", unsafe_allow_html=True)
|
| 7 |
+
|
| 8 |
+
# Load the credit card dataset directly
|
| 9 |
+
@st.cache_data
|
| 10 |
+
def load_dataset():
|
| 11 |
+
return pd.read_csv('creditcard.csv') # Path to the creditcard.csv file
|
| 12 |
+
|
| 13 |
+
# Cache function to convert DataFrame to CSV
|
| 14 |
+
@st.cache_data
|
| 15 |
+
def convert_df(df):
|
| 16 |
+
return df.to_csv(index=False).encode("utf-8")
|
| 17 |
+
|
| 18 |
+
# Load pre-trained anomaly detection model
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_model():
|
| 21 |
+
with open("model.pkl", "rb") as f:
|
| 22 |
+
model = pickle.load(f)
|
| 23 |
+
return model
|
| 24 |
+
|
| 25 |
+
# Load the dataset
|
| 26 |
+
df = load_dataset()
|
| 27 |
+
|
| 28 |
+
# Display the dataset preview
|
| 29 |
+
st.write("Dataset Preview:")
|
| 30 |
+
st.dataframe(df.head())
|
| 31 |
+
|
| 32 |
+
# Ensure the dataset has the 'Class' column
|
| 33 |
+
if 'Class' not in df.columns:
|
| 34 |
+
st.error("The dataset must contain a 'Class' column.")
|
| 35 |
+
else:
|
| 36 |
+
# Add functionality for row selection
|
| 37 |
+
st.markdown("### Select a Row for Model Input:")
|
| 38 |
+
|
| 39 |
+
# Option to select a row
|
| 40 |
+
selected_row = st.selectbox("Select a Row", options=range(len(df)), index=0)
|
| 41 |
+
|
| 42 |
+
# Add a button below the row selection for anomaly detection
|
| 43 |
+
detect_button = st.button("Detect Anomaly")
|
| 44 |
+
|
| 45 |
+
# If the "Detect" button is clicked
|
| 46 |
+
if detect_button:
|
| 47 |
+
# Row to use for the model
|
| 48 |
+
row_to_use = df.iloc[selected_row]
|
| 49 |
+
|
| 50 |
+
# Drop 'Class' column (exclude it from features used for prediction)
|
| 51 |
+
row_to_use_for_model = row_to_use.drop('Class', errors='ignore')
|
| 52 |
+
|
| 53 |
+
# Check if the number of features matches the model's expectations
|
| 54 |
+
model = load_model()
|
| 55 |
+
|
| 56 |
+
if len(row_to_use_for_model) != model.n_features_in_:
|
| 57 |
+
st.error(f"The model expects {model.n_features_in_} features, but {len(row_to_use_for_model)} were provided.")
|
| 58 |
+
else:
|
| 59 |
+
# Apply the model for anomaly detection
|
| 60 |
+
prediction = model.predict([row_to_use_for_model])
|
| 61 |
+
|
| 62 |
+
# Display the row and the anomaly detection result
|
| 63 |
+
st.write(f"Row selected for anomaly detection:")
|
| 64 |
+
st.write(row_to_use)
|
| 65 |
+
|
| 66 |
+
# Show the anomaly result for the selected row
|
| 67 |
+
result = "Anomaly" if prediction[0] == -1 else "Not Anomaly"
|
| 68 |
+
st.write(f"Anomaly Detection Result: {result}")
|
| 69 |
+
|
| 70 |
+
# Provide option to download the result
|
| 71 |
+
result_df = row_to_use.to_frame().T # Convert Series to DataFrame
|
| 72 |
+
result_df['Anomaly'] = result
|
| 73 |
+
result_csv = convert_df(result_df)
|
| 74 |
+
st.download_button(
|
| 75 |
+
label="Download Selected Row Result",
|
| 76 |
+
data=result_csv,
|
| 77 |
+
file_name="Selected_Row_Anomaly_Result.csv",
|
| 78 |
+
mime="text/csv",
|
| 79 |
+
)
|
credit fraud anomaly detection.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d9be34201b7e69022198f3bec4556f5c0ebe7b2da004e4c85bcfec1e09edb69d
|
| 3 |
+
size 828929
|
creditcard.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:76274b691b16a6c49d3f159c883398e03ccd6d1ee12d9d8ee38f4b4b98551a89
|
| 3 |
+
size 150828752
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:067eea915e8dab93a19e20104c5baccfc2bfd29a0357f7843bc6f18460f1092a
|
| 3 |
+
size 867587
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
scikit-learn
|
| 3 |
+
numpy
|
| 4 |
+
seaborn
|
| 5 |
+
matplotlib
|
| 6 |
+
streamlit
|