leyuzak/Binary-Classification-with-a-Bank-Dataset
0
1import joblib2import pandas as pd3import streamlit as st4 5# --- sklearn pickle uyumluluğu (sadece bu kalsın) ---6try:7 import sklearn.compose._column_transformer as _ct8 if not hasattr(_ct, "_RemainderColsList"):9 class _RemainderColsList(list):10 pass11 _ct._RemainderColsList = _RemainderColsList12except Exception:13 pass14# ---------------------------------------------------15 16st.set_page_config(17 page_title="Bank Subscription Predictor",18 page_icon="🏦",19 layout="centered"20)21 22@st.cache_resource23def load_model():24 return joblib.load("bank_subscription_model.joblib")25 26model = load_model()27 28st.title("🏦 Bank Subscription Predictor")29st.caption("Predict the probability that a client subscribes to a term deposit (y=1).")30 31with st.expander("ℹ️ What is this?"):32 st.markdown(33 """34This app loads a trained model (`bank_subscription_model.joblib`)35and predicts the probability of `y=1`.36Fill the inputs and click **Predict**.37"""38 )39 40st.subheader("Client Features")41 42col1, col2 = st.columns(2)43 44with col1:45 age = st.number_input("age", min_value=0, max_value=120, value=35, step=1)46 job = st.selectbox(47 "job",48 [49 "admin.", "blue-collar", "entrepreneur", "housemaid", "management",50 "retired", "self-employed", "services", "student", "technician",51 "unemployed", "unknown"52 ],53 index=454 )55 marital = st.selectbox("marital", ["divorced", "married", "single", "unknown"], index=1)56 education = st.selectbox(57 "education",58 [59 "basic.4y", "basic.6y", "basic.9y", "high.school", "illiterate",60 "professional.course", "university.degree", "unknown"61 ],62 index=663 )64 default = st.selectbox("default", ["no", "yes", "unknown"], index=0)65 balance = st.number_input("balance", value=0.0, step=10.0)66 housing = st.selectbox("housing", ["no", "yes", "unknown"], index=1)67 loan = st.selectbox("loan", ["no", "yes", "unknown"], index=0)68 69with col2:70 contact = st.selectbox("contact", ["cellular", "telephone", "unknown"], index=0)71 day = st.number_input("day", min_value=1, max_value=31, value=15, step=1)72 month = st.selectbox(73 "month",74 ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"],75 index=476 )77 duration = st.number_input("duration", min_value=0, value=180, step=10)78 campaign = st.number_input("campaign", min_value=0, value=1, step=1)79 pdays = st.number_input("pdays", value=-1, step=1)80 previous = st.number_input("previous", min_value=0, value=0, step=1)81 poutcome = st.selectbox("poutcome", ["failure", "other", "success", "unknown"], index=3)82 83client_id_str = st.text_input("id (optional)", value="0")84 85try:86 client_id = int(client_id_str)87except Exception:88 client_id = 089 90x = pd.DataFrame([{91 "id": client_id,92 "age": age,93 "job": job,94 "marital": marital,95 "education": education,96 "default": default,97 "balance": float(balance),98 "housing": housing,99 "loan": loan,100 "contact": contact,101 "day": int(day),102 "month": month,103 "duration": int(duration),104 "campaign": int(campaign),105 "pdays": int(pdays),106 "previous": int(previous),107 "poutcome": poutcome108}])109 110st.divider()111 112if st.button("🔮 Predict", type="primary"):113 try:114 if hasattr(model, "predict_proba"):115 proba = float(model.predict_proba(x)[:, 1][0])116 else:117 score = float(model.decision_function(x)[0])118 import math119 proba = 1 / (1 + math.exp(-score))120 121 st.success(f"Predicted probability of y=1: **{proba:.4f}**")122 123 if proba >= 0.7:124 st.write("✅ High likelihood of subscription")125 elif proba >= 0.4:126 st.write("🟡 Medium likelihood of subscription")127 else:128 st.write("🔻 Low likelihood of subscription")129 130 except Exception as e:131 st.error(132 "Prediction failed. Check that the model file matches the expected feature columns."133 )134 st.exception(e)135 