Update app.py
#2
by
MiaadAlsulami
- opened
app.py
CHANGED
@@ -1,133 +1,79 @@
|
|
|
|
1 |
import pandas as pd
|
2 |
import numpy as np
|
3 |
-
import streamlit as st # Streamlit for UI
|
4 |
import matplotlib.pyplot as plt
|
5 |
import seaborn as sns
|
6 |
-
from sklearn.model_selection import train_test_split
|
7 |
from sklearn.preprocessing import StandardScaler
|
8 |
from sklearn.svm import SVC
|
9 |
-
from sklearn.ensemble import RandomForestClassifier
|
10 |
from sklearn.linear_model import LogisticRegression
|
11 |
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
12 |
|
13 |
# Load Dataset
|
14 |
-
df = pd.read_csv("bank_loan
|
15 |
|
16 |
-
# Preprocessing
|
17 |
-
df['CCAvg'] = df['CCAvg'].astype(
|
18 |
-
|
19 |
-
# Drop Unnecessary Columns
|
20 |
df.drop(['ID', 'ZIP Code'], axis=1, inplace=True)
|
|
|
21 |
|
22 |
-
#
|
23 |
-
df['Income'] = round(df['Income'] / 12, 2)
|
24 |
-
|
25 |
-
# Define X and y
|
26 |
X = df.drop('Personal Loan', axis=1)
|
27 |
y = df['Personal Loan']
|
28 |
|
29 |
-
# Standardize
|
30 |
scaler = StandardScaler()
|
31 |
X_scaled = scaler.fit_transform(X)
|
32 |
|
33 |
-
# Train
|
34 |
X_train, X_temp, y_train, y_temp = train_test_split(X_scaled, y, test_size=0.3, stratify=y, random_state=42)
|
35 |
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)
|
36 |
|
37 |
-
# Train
|
38 |
-
|
39 |
-
|
40 |
-
logistic_regression = LogisticRegression(max_iter=500)
|
41 |
-
|
42 |
-
models = {"SVM (RBF Kernel)": svm_rbf, "SVM (Linear Kernel)": svm_linear, "Logistic Regression": logistic_regression}
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
if acc > best_acc:
|
50 |
-
best_acc = acc
|
51 |
-
best_model = model
|
52 |
|
53 |
# Streamlit UI
|
54 |
-
st.title("
|
55 |
-
st.write("Answer the questions
|
56 |
-
|
57 |
-
|
58 |
-
st.
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
st.
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
if st.button("🔮 Predict Loan Approval"):
|
87 |
-
user_data = np.array([[age, experience, income, family, ccavg, education, mortgage, securities, cd_account, online, credit_card]])
|
88 |
-
user_scaled = scaler.transform(user_data)
|
89 |
-
|
90 |
-
prediction = best_model.predict(user_scaled)[0]
|
91 |
-
probability = best_model.predict_proba(user_scaled)[0][1]
|
92 |
-
|
93 |
-
st.subheader("📊 Prediction Result")
|
94 |
if prediction == 1:
|
95 |
-
st.success(f"
|
96 |
else:
|
97 |
-
st.error(f"
|
98 |
-
|
99 |
-
# Show User Input Data in a Table
|
100 |
-
st.subheader("📌 Your Entered Information")
|
101 |
-
user_df = pd.DataFrame({
|
102 |
-
"Feature": ["Age", "Experience", "Income", "Family", "CCAvg", "Education", "Mortgage",
|
103 |
-
"Securities Account", "CD Account", "Online", "Credit Card"],
|
104 |
-
"Your Answer": [age, experience, income, family, ccavg, education, mortgage, securities, cd_account, online, credit_card]
|
105 |
-
})
|
106 |
-
|
107 |
-
st.dataframe(user_df)
|
108 |
-
|
109 |
-
# Show Summary Visuals
|
110 |
-
st.subheader("📊 Feature Distributions in Dataset")
|
111 |
-
|
112 |
-
fig, ax = plt.subplots(3, 2, figsize=(12, 10))
|
113 |
-
|
114 |
-
sns.histplot(df["Age"], kde=True, bins=20, ax=ax[0, 0])
|
115 |
-
ax[0, 0].set_title("Distribution of Age")
|
116 |
-
|
117 |
-
sns.histplot(df["Experience"], kde=True, bins=20, ax=ax[0, 1])
|
118 |
-
ax[0, 1].set_title("Distribution of Experience")
|
119 |
-
|
120 |
-
sns.histplot(df["Income"], kde=True, bins=20, ax=ax[1, 0])
|
121 |
-
ax[1, 0].set_title("Distribution of Income")
|
122 |
-
|
123 |
-
sns.histplot(df["CCAvg"], kde=True, bins=20, ax=ax[1, 1])
|
124 |
-
ax[1, 1].set_title("Distribution of Credit Card Avg Spending")
|
125 |
-
|
126 |
-
sns.histplot(df["Mortgage"], kde=True, bins=20, ax=ax[2, 0])
|
127 |
-
ax[2, 0].set_title("Distribution of Mortgage")
|
128 |
-
|
129 |
-
sns.histplot(df["Personal Loan"], kde=True, bins=2, ax=ax[2, 1])
|
130 |
-
ax[2, 1].set_title("Loan Approvals (Target Variable)")
|
131 |
-
|
132 |
-
plt.tight_layout()
|
133 |
-
st.pyplot(fig)
|
|
|
1 |
+
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
import seaborn as sns
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
from sklearn.preprocessing import StandardScaler
|
8 |
from sklearn.svm import SVC
|
|
|
9 |
from sklearn.linear_model import LogisticRegression
|
10 |
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
11 |
|
12 |
# Load Dataset
|
13 |
+
df = pd.read_csv("bank_loan.csv")
|
14 |
|
15 |
+
# Data Preprocessing
|
16 |
+
df['CCAvg'] = df['CCAvg'].astype(float)
|
17 |
+
df[df['Experience'] < 0] = df[df['Experience'] < 0].abs()
|
|
|
18 |
df.drop(['ID', 'ZIP Code'], axis=1, inplace=True)
|
19 |
+
df['Income'] = round(df['Income']/12, 2)
|
20 |
|
21 |
+
# Define Features and Target Variable
|
|
|
|
|
|
|
22 |
X = df.drop('Personal Loan', axis=1)
|
23 |
y = df['Personal Loan']
|
24 |
|
25 |
+
# Standardize Features
|
26 |
scaler = StandardScaler()
|
27 |
X_scaled = scaler.fit_transform(X)
|
28 |
|
29 |
+
# Split Data into Train, Validation, and Test Sets
|
30 |
X_train, X_temp, y_train, y_temp = train_test_split(X_scaled, y, test_size=0.3, stratify=y, random_state=42)
|
31 |
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)
|
32 |
|
33 |
+
# Train Model
|
34 |
+
svm_model = SVC(kernel='rbf', C=10, gamma=0.1, probability=True)
|
35 |
+
svm_model.fit(X_train, y_train)
|
|
|
|
|
|
|
36 |
|
37 |
+
def predict_loan_acceptance(sample):
|
38 |
+
sample_scaled = scaler.transform(sample)
|
39 |
+
prediction = svm_model.predict(sample_scaled)[0]
|
40 |
+
probability = svm_model.predict_proba(sample_scaled)[0][1]
|
41 |
+
return prediction, probability
|
|
|
|
|
|
|
42 |
|
43 |
# Streamlit UI
|
44 |
+
st.title("Personal Loan Eligibility Checker")
|
45 |
+
st.write("Answer the following questions to check if you're eligible for a personal loan.")
|
46 |
+
|
47 |
+
age = st.number_input("Enter your age:", min_value=18, max_value=100, step=1)
|
48 |
+
experience = st.number_input("Enter your years of experience:", min_value=0, max_value=80, step=1)
|
49 |
+
income = st.number_input("Enter your monthly income (in thousands):", min_value=0.0, step=0.1)
|
50 |
+
family = st.selectbox("Select number of family members:", [1, 2, 3, 4])
|
51 |
+
ccavg = st.number_input("Enter your average monthly credit card spending:", min_value=0.0, step=0.1)
|
52 |
+
education = st.selectbox("Select your education level:", [1, 2, 3], format_func=lambda x: ["Undergraduate", "Graduate", "Advanced/Professional"][x-1])
|
53 |
+
mortgage = st.number_input("Enter your mortgage amount (Enter 0 if none):", min_value=0.0, step=0.1)
|
54 |
+
securities_account = st.radio("Do you have a securities account?", [0, 1], format_func=lambda x: "Yes" if x else "No")
|
55 |
+
cd_account = st.radio("Do you have a certificate of deposit (CD) account?", [0, 1], format_func=lambda x: "Yes" if x else "No")
|
56 |
+
online = st.radio("Do you use online banking?", [0, 1], format_func=lambda x: "Yes" if x else "No")
|
57 |
+
credit_card = st.radio("Do you have a credit card with the bank?", [0, 1], format_func=lambda x: "Yes" if x else "No")
|
58 |
+
|
59 |
+
if st.button("Check Eligibility"):
|
60 |
+
sample = pd.DataFrame({
|
61 |
+
'Age': [age],
|
62 |
+
'Experience': [experience],
|
63 |
+
'Income': [income],
|
64 |
+
'Family': [family],
|
65 |
+
'CCAvg': [ccavg],
|
66 |
+
'Education': [education],
|
67 |
+
'Mortgage': [mortgage],
|
68 |
+
'Securities Account': [securities_account],
|
69 |
+
'CD Account': [cd_account],
|
70 |
+
'Online': [online],
|
71 |
+
'CreditCard': [credit_card]
|
72 |
+
})
|
73 |
+
prediction, probability = predict_loan_acceptance(sample)
|
74 |
+
|
75 |
+
st.subheader("Prediction Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
if prediction == 1:
|
77 |
+
st.success(f"Loan Accepted ✅\nProbability of Acceptance: {probability:.4f}")
|
78 |
else:
|
79 |
+
st.error(f"Loan Not Accepted ❌\nProbability of Acceptance: {probability:.4f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|