Files changed (1) hide show
  1. app.py +52 -106
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, GridSearchCV
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(1).csv")
15
 
16
- # Preprocessing
17
- df['CCAvg'] = df['CCAvg'].astype('float64')
18
-
19
- # Drop Unnecessary Columns
20
  df.drop(['ID', 'ZIP Code'], axis=1, inplace=True)
 
21
 
22
- # Convert annual income to monthly
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 the features
30
  scaler = StandardScaler()
31
  X_scaled = scaler.fit_transform(X)
32
 
33
- # Train-Validation-Test Split
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 Models
38
- svm_rbf = SVC(kernel='rbf', C=10, gamma=0.1, probability=True)
39
- svm_linear = SVC(kernel='linear', C=1, probability=True)
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
- # Train and Select the Best Model
45
- best_model, best_acc = None, 0
46
- for name, model in models.items():
47
- model.fit(X_train, y_train)
48
- acc = accuracy_score(y_val, model.predict(X_val))
49
- if acc > best_acc:
50
- best_acc = acc
51
- best_model = model
52
 
53
  # Streamlit UI
54
- st.title("🏦 Personal Loan Prediction System")
55
- st.write("Answer the questions below to check your **loan eligibility.**")
56
-
57
- # Collect User Inputs
58
- st.subheader("📋 Personal & Financial Information")
59
-
60
- age = st.slider("📌 Age", min_value=18, max_value=100, value=30)
61
- experience = st.slider("📌 Years of Experience", min_value=0, max_value=50, value=5)
62
- income = st.slider("📌 Monthly Income (in thousands)", min_value=0, max_value=50, value=5)
63
- family = st.selectbox("📌 Number of Family Members", [1, 2, 3, 4])
64
-
65
- st.subheader("💳 Credit & Banking Behavior")
66
-
67
- ccavg = st.slider("📌 Avg Monthly Credit Card Spending", min_value=0, max_value=10, value=1)
68
- education = st.radio("📌 Education Level", ["Undergraduate (1)", "Graduate (2)", "Advanced/Professional (3)"])
69
- mortgage = st.slider("📌 Mortgage Amount (in thousands)", min_value=0, max_value=500, value=0)
70
-
71
- st.subheader("📡 Account Information")
72
-
73
- securities = st.radio("📌 Do you have a Securities Account?", ["No", "Yes"])
74
- cd_account = st.radio("📌 Do you have a Certificate of Deposit (CD) Account?", ["No", "Yes"])
75
- online = st.radio("📌 Do you use Online Banking?", ["No", "Yes"])
76
- credit_card = st.radio("📌 Do you have a Credit Card with the bank?", ["No", "Yes"])
77
-
78
- # Convert Inputs for Model
79
- education = 1 if "Undergraduate" in education else (2 if "Graduate" in education else 3)
80
- securities = 1 if securities == "Yes" else 0
81
- cd_account = 1 if cd_account == "Yes" else 0
82
- online = 1 if online == "Yes" else 0
83
- credit_card = 1 if credit_card == "Yes" else 0
84
-
85
- # Submit Button
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"Loan Approved! (Probability: {probability:.2f})")
96
  else:
97
- st.error(f"Loan Not Approved. (Probability: {probability:.2f})")
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}")