import streamlit as st import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, classification_report, confusion_matrix # Load Dataset df = pd.read_csv("bank_loan.csv") # Data Preprocessing df['CCAvg'] = df['CCAvg'].astype(float) df[df['Experience'] < 0] = df[df['Experience'] < 0].abs() df.drop(['ID', 'ZIP Code'], axis=1, inplace=True) df['Income'] = round(df['Income']/12, 2) # Define Features and Target Variable X = df.drop('Personal Loan', axis=1) y = df['Personal Loan'] # Standardize Features scaler = StandardScaler() X_scaled = scaler.fit_transform(X) # Split Data into Train, Validation, and Test Sets X_train, X_temp, y_train, y_temp = train_test_split(X_scaled, y, test_size=0.3, stratify=y, random_state=42) X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, stratify=y_temp, random_state=42) # Train Model svm_model = SVC(kernel='rbf', C=10, gamma=0.1, probability=True) svm_model.fit(X_train, y_train) def predict_loan_acceptance(sample): sample_scaled = scaler.transform(sample) prediction = svm_model.predict(sample_scaled)[0] probability = svm_model.predict_proba(sample_scaled)[0][1] return prediction, probability # Streamlit UI st.title("Personal Loan Eligibility Checker") st.write("Answer the following questions to check if you're eligible for a personal loan.") age = st.number_input("Enter your age:", min_value=18, max_value=100, step=1) experience = st.number_input("Enter your years of experience:", min_value=0, max_value=80, step=1) income = st.number_input("Enter your monthly income (in thousands):", min_value=0.0, step=0.1) family = st.selectbox("Select number of family members:", [1, 2, 3, 4]) ccavg = st.number_input("Enter your average monthly credit card spending:", min_value=0.0, step=0.1) education = st.selectbox("Select your education level:", [1, 2, 3], format_func=lambda x: ["Undergraduate", "Graduate", "Advanced/Professional"][x-1]) mortgage = st.number_input("Enter your mortgage amount (Enter 0 if none):", min_value=0.0, step=0.1) securities_account = st.radio("Do you have a securities account?", [0, 1], format_func=lambda x: "Yes" if x else "No") cd_account = st.radio("Do you have a certificate of deposit (CD) account?", [0, 1], format_func=lambda x: "Yes" if x else "No") online = st.radio("Do you use online banking?", [0, 1], format_func=lambda x: "Yes" if x else "No") credit_card = st.radio("Do you have a credit card with the bank?", [0, 1], format_func=lambda x: "Yes" if x else "No") if st.button("Check Eligibility"): sample = pd.DataFrame({ 'Age': [age], 'Experience': [experience], 'Income': [income], 'Family': [family], 'CCAvg': [ccavg], 'Education': [education], 'Mortgage': [mortgage], 'Securities Account': [securities_account], 'CD Account': [cd_account], 'Online': [online], 'CreditCard': [credit_card] }) prediction, probability = predict_loan_acceptance(sample) st.subheader("Prediction Result") if prediction == 1: st.success(f"Loan Accepted ✅\nProbability of Acceptance: {probability:.4f}") else: st.error(f"Loan Not Accepted ❌\nProbability of Acceptance: {probability:.4f}")