Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

add new model named credit card estimator used to detect whether the … #113

Merged
merged 6 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions App.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
st.write(
"- **Stress Level Detector**: Analyze your mental stress levels based on social media interactions."
)
st.write(
"- **Credit Card Fraud Estimator**: Check Whether your transaction is faulty or not."
)
st.write(
"- **Parkinson's Disease Detector**: Assess your risk of Parkinson's Disease with advanced machine learning algorithms."
)
Expand Down
79 changes: 79 additions & 0 deletions form_configs/credit_card_fraud.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"Credit Card Fraud Estimator": {
"Average Amount per Transaction per Day": {
"type": "number",
"min_value": 0,
"max_value": 100000,
"default_value": 100,
"step": 100,
"field_name": "avg_amount_per_day"
},
"Transaction Amount": {
"type": "number",
"min_value": 0,
"max_value": 100000,
"default_value": 3000,
"step": 100,
"field_name": "transaction_amount"
},
"Is Declined": {
"type": "dropdown",
"options": [
"Yes",
"No"
],
"default_value": "No",
"field_name": "Is_declined"
},
"Total Number of Declines per Day": {
"type": "number",
"min_value": 0,
"max_value": 100,
"default_value": 0,
"step": 1,
"field_name": "no_of_declines_per_day"
},
"Is Foreign Transaction": {
"type": "dropdown",
"options": [
"Yes",
"No"
],
"default_value": "No",
"field_name": "Is_Foreign_transaction"
},
"Is High-Risk Country": {
"type": "dropdown",
"options": [
"Yes",
"No"
],
"default_value": "No",
"field_name": "Is_High_Risk_country"
},
"Daily Chargeback Average Amount": {
"type": "number",
"min_value": 0,
"max_value": 10000,
"default_value": 0,
"step": 100,
"field_name": "Daily_chargeback_avg_amt"
},
"6-Month Average Chargeback Amount": {
"type": "number",
"min_value": 0,
"max_value": 10000,
"default_value": 0,
"step": 100,
"field_name": "six_month_avg_chbk_amt"
},
"6-Month Chargeback Frequency": {
"type": "number",
"min_value": 0,
"max_value": 100,
"default_value": 0,
"step": 1,
"field_name": "six_month_chbk_freq"
}
}
}
3,076 changes: 3,076 additions & 0 deletions models/credit_card_fraud/data/creditcardcsvpresent.csv

Large diffs are not rendered by default.

117 changes: 117 additions & 0 deletions models/credit_card_fraud/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# importing libraries
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import warnings
import pickle
import matplotlib.pyplot as plt


# reading dataset
warnings.filterwarnings("ignore")

data = pd.read_csv("/content/creditcardcsvpresent.csv")
df = data.copy(deep=True)

# df.info()

# remove transaction_date all values are null
# and also remove merchant id
df = df.drop(columns=['Merchant_id', 'Transaction date'], axis=1)


# encoding for qualitative variables
code = {
"N": 0,
"Y": 1
}

for obj in df.select_dtypes("object"):
df[obj] = df[obj].map(code)

# Target and Feature Identification
target = "isFradulent"
features = [col for col in df.columns if col != target]

X = df[features] # Create a DataFrame for the features
y = df[target] # Create a Series for the target


# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42)


# Train Random Forest Classifier
rf_model = RandomForestClassifier(n_estimators=100, random_state=42,class_weight='balanced')
rf_model.fit(X_train, y_train)

# Make predictions
y_pred = rf_model.predict(X_test)

# Function to prepare input data into a DataFrame
def prepare_input_data(
avg_amount_per_day,
transaction_amount,
Is_declined,
no_of_declines_per_day,
Is_Foreign_transaction,
Is_High_Risk_country,
Daily_chargeback_avg_amt,
six_month_avg_chbk_amt,
six_month_chbk_freq,
):
# Create a DataFrame with the input data
input_data = {
"Average Amount/transaction/day": [avg_amount_per_day],
"Transaction_amount": [transaction_amount],
"Is declined": [Is_declined],
"Total Number of declines/day": [no_of_declines_per_day],
"isForeignTransaction": [Is_Foreign_transaction],
"isHighRiskCountry": [Is_High_Risk_country],
"Daily_chargeback_avg_amt": [Daily_chargeback_avg_amt],
"6_month_avg_chbk_amt": [six_month_avg_chbk_amt],
"6-month_chbk_freq": [six_month_chbk_freq],
}

return pd.DataFrame(input_data)

def get_prediction(
avg_amount_per_day,
transaction_amount,
Is_declined,
no_of_declines_per_day,
Is_Foreign_transaction,
Is_High_Risk_country,
Daily_chargeback_avg_amt,
six_month_avg_chbk_amt,
six_month_chbk_freq,
):
# Prepare the input data
input_df = prepare_input_data(
avg_amount_per_day,
transaction_amount,
Is_declined,
no_of_declines_per_day,
Is_Foreign_transaction,
Is_High_Risk_country,
Daily_chargeback_avg_amt,
six_month_avg_chbk_amt,
six_month_chbk_freq,
)
# Predict using Random Forest
predicted_value = rf_model.predict(input_df)

# Return "Fraud" if fraud (1), else "Not a Fraud"
return "Fraud" if predicted_value[0] == 1 else "Not a Fraud"


# Function to save the model
def save_model():
# Save the Random Forest model
with open("models/credit_card_fraud/transaction_rf_model.pkl", "wb") as file:
pickle.dump(rf_model, file)



# save_model()
73 changes: 73 additions & 0 deletions models/credit_card_fraud/predict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import pandas as pd
from sklearn.preprocessing import StandardScaler
import pickle

def load_model(model_path):
""" Load the trained Random Forest model from the specified path. """
with open(model_path, 'rb') as file:
return pickle.load(file)

def prepare_input_data(
avg_amount_per_day,
transaction_amount,
Is_declined,
no_of_declines_per_day,
Is_Foreign_transaction,
Is_High_Risk_country,
Daily_chargeback_avg_amt,
six_month_avg_chbk_amt,
six_month_chbk_freq,
):
# Create a DataFrame with the input data
input_data = {
"Average Amount/transaction/day": [avg_amount_per_day],
"Transaction_amount": [transaction_amount],
"Is declined": [Is_declined],
"Total Number of declines/day": [no_of_declines_per_day],
"isForeignTransaction": [Is_Foreign_transaction],
"isHighRiskCountry": [Is_High_Risk_country],
"Daily_chargeback_avg_amt": [Daily_chargeback_avg_amt],
"6_month_avg_chbk_amt": [six_month_avg_chbk_amt],
"6-month_chbk_freq": [six_month_chbk_freq],
}

return pd.DataFrame(input_data)

def get_prediction(
avg_amount_per_day,
transaction_amount,
Is_declined,
no_of_declines_per_day,
Is_Foreign_transaction,
Is_High_Risk_country,
Daily_chargeback_avg_amt,
six_month_avg_chbk_amt,
six_month_chbk_freq,
):
# Prepare the input data
input_df = prepare_input_data(
avg_amount_per_day,
transaction_amount,
Is_declined,
no_of_declines_per_day,
Is_Foreign_transaction,
Is_High_Risk_country,
Daily_chargeback_avg_amt,
six_month_avg_chbk_amt,
six_month_chbk_freq,
)

# Convert input data to numeric (if necessary)
input_df = input_df.apply(pd.to_numeric, errors='coerce')

# Fill NaN values (optional: handle as needed)
input_df.fillna(0, inplace=True)

# Load the model
rf_model = load_model("models/credit_card_fraud/transaction_rf_model.pkl")

# Predict using Random Forest
predicted_value = rf_model.predict(input_df)

# Return "Fraud" if fraud (1), else "Not a Fraud"
return "Fraud" if predicted_value[0] == 1 else "Not a Fraud"
Binary file not shown.
4 changes: 4 additions & 0 deletions pages/Credit_Card_Fraud_Estimator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from page_handler import PageHandler

page_handler = PageHandler("pages/pages.json")
page_handler.render_page("Credit Card Fraud Estimator")
22 changes: 22 additions & 0 deletions pages/pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@
}
]
},
"Credit Card Fraud Estimator": {
"title": "Credit Card Fraud Estimator",
"page_title": "Credit Card Fraud Estimator",
"page_icon": "\ud83d\udcb0",
"model_predict_file_path": "models/credit_card_fraud/predict.py",
"model_function": "get_prediction",
"form_config_path": "form_configs/credit_card_fraud.json",
"model_detail_function": "model_details",
"tabs": [
{
"name": "Estimator",
"type": "form",
"form_name": "Credit Card Fraud Estimator"
},
{
"name": "Model Details",
"type": "model_details",
"problem_statement": "The model predicts whether a credit card transaction is fraudulent based on input features such as transaction amount, card type, and customer profile.",
"description": "This model uses a Random Forest Classifier to classify credit card transactions as fraudulent or non-fraudulent. It learns from historical transaction data with both normal and fraudulent transactions to make accurate predictions."
}
]
},
"Loan Eligibility Estimator": {
"title": "Loan Eligibility Estimator",
"page_title": "Loan Eligibility Estimator",
Expand Down