| import os |
| import joblib |
| import pandas as pd |
| import streamlit as st |
|
|
| st.set_page_config( |
| page_title="Bank Marketing Prediction", |
| page_icon="🏦", |
| layout="centered" |
| ) |
|
|
| BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| MODEL_PATH = os.path.join(BASE_DIR, "bank_model.pkl") |
| COLUMNS_PATH = os.path.join(BASE_DIR, "bank_columns.pkl") |
|
|
|
|
| @st.cache_resource |
| def load_artifacts(): |
| model = joblib.load(MODEL_PATH) |
| columns = joblib.load(COLUMNS_PATH) |
| return model, columns |
|
|
|
|
| model, model_columns = load_artifacts() |
|
|
| st.title("🏦 Bank Marketing Campaign Prediction") |
| st.write("Enter customer information to predict whether the customer will subscribe to the campaign.") |
|
|
| age = st.number_input("Age", min_value=18, max_value=100, value=35) |
| job = st.selectbox("Job", [ |
| "admin.", "blue-collar", "entrepreneur", "housemaid", "management", |
| "retired", "self-employed", "services", "student", "technician", |
| "unemployed", "unknown" |
| ]) |
| marital = st.selectbox("Marital Status", ["divorced", "married", "single"]) |
| education = st.selectbox("Education", ["primary", "secondary", "tertiary", "unknown"]) |
| default = st.selectbox("Default", ["no", "yes"]) |
| balance = st.number_input("Balance", value=1000) |
| housing = st.selectbox("Housing Loan", ["no", "yes"]) |
| loan = st.selectbox("Personal Loan", ["no", "yes"]) |
| contact = st.selectbox("Contact Type", ["cellular", "telephone", "unknown"]) |
| day = st.number_input("Last Contact Day", min_value=1, max_value=31, value=15) |
| month = st.selectbox("Last Contact Month", [ |
| "jan", "feb", "mar", "apr", "may", "jun", |
| "jul", "aug", "sep", "oct", "nov", "dec" |
| ]) |
| duration = st.number_input("Call Duration (seconds)", min_value=0, value=180) |
| campaign = st.number_input("Number of Contacts During Campaign", min_value=1, value=1) |
| pdays = st.number_input("Days Since Last Contact", value=999) |
| previous = st.number_input("Number of Previous Contacts", min_value=0, value=0) |
| poutcome = st.selectbox("Previous Campaign Outcome", ["failure", "other", "success", "unknown"]) |
|
|
| if st.button("Predict"): |
| input_data = pd.DataFrame([{ |
| "age": age, |
| "job": job, |
| "marital": marital, |
| "education": education, |
| "default": default, |
| "balance": balance, |
| "housing": housing, |
| "loan": loan, |
| "contact": contact, |
| "day": day, |
| "month": month, |
| "duration": duration, |
| "campaign": campaign, |
| "pdays": pdays, |
| "previous": previous, |
| "poutcome": poutcome |
| }]) |
|
|
| input_encoded = pd.get_dummies(input_data, drop_first=True) |
| input_encoded = input_encoded.reindex(columns=model_columns, fill_value=0) |
|
|
| prediction = model.predict(input_encoded)[0] |
| prediction_proba = model.predict_proba(input_encoded)[0][1] if hasattr(model, "predict_proba") else None |
|
|
| if prediction == 1: |
| st.success("Prediction: Customer is likely to subscribe.") |
| else: |
| st.error("Prediction: Customer is unlikely to subscribe.") |
|
|
| if prediction_proba is not None: |
| st.info(f"Subscription probability: {prediction_proba:.2%}") |