Download src/streamlit_app.py from MSK34/Credit-Card-Customer-Segmentation: direct link, hf CLI and curl.
- Browser
- Download file 2.83 kB
-
https://huggingface.co/spaces/MSK34/Credit-Card-Customer-Segmentation/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/MSK34/Credit-Card-Customer-Segmentation/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/MSK34/Credit-Card-Customer-Segmentation/resolve/main/src/streamlit_app.py
2.83 kB
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import pickle | |
| st.title("Credit Card Customer Segmentation") | |
| st.write( | |
| "Bu uygulama, KMeans kümeleme algoritmasını kullanarak müşteri segmentlerini tahmin eder." | |
| ) | |
| # Model ve scaler yükleme | |
| model = pickle.load(open("src/kmeans_model.pkl", "rb")) | |
| scaler = pickle.load(open("src/scaler.pkl", "rb")) | |
| # Kullanıcı girişleri | |
| balance = st.number_input("Balance", value=1000.0) | |
| balance_frequency = st.slider( | |
| "Balance Frequency", | |
| 0.0, | |
| 1.0, | |
| 0.9 | |
| ) | |
| purchases = st.number_input("Purchases", value=500.0) | |
| oneoff_purchases = st.number_input("Oneoff Purchases", value=200.0) | |
| installments_purchases = st.number_input( | |
| "Installments Purchases", | |
| value=300.0 | |
| ) | |
| cash_advance = st.number_input("Cash Advance", value=0.0) | |
| purchases_frequency = st.slider( | |
| "Purchases Frequency", | |
| 0.0, | |
| 1.0, | |
| 0.5 | |
| ) | |
| oneoff_frequency = st.slider( | |
| "Oneoff Purchases Frequency", | |
| 0.0, | |
| 1.0, | |
| 0.3 | |
| ) | |
| installments_frequency = st.slider( | |
| "Installments Purchases Frequency", | |
| 0.0, | |
| 1.0, | |
| 0.4 | |
| ) | |
| credit_limit = st.number_input("Credit Limit", value=5000.0) | |
| payments = st.number_input("Payments", value=1000.0) | |
| minimum_payments = st.number_input( | |
| "Minimum Payments", | |
| value=300.0 | |
| ) | |
| full_payment = st.slider( | |
| "Full Payment Ratio", | |
| 0.0, | |
| 1.0, | |
| 0.3 | |
| ) | |
| tenure = st.slider( | |
| "Tenure", | |
| 1, | |
| 12, | |
| 12 | |
| ) | |
| # DataFrame oluşturma | |
| input_data = pd.DataFrame({ | |
| "BALANCE":[balance], | |
| "BALANCE_FREQUENCY":[balance_frequency], | |
| "PURCHASES":[purchases], | |
| "ONEOFF_PURCHASES":[oneoff_purchases], | |
| "INSTALLMENTS_PURCHASES":[installments_purchases], | |
| "CASH_ADVANCE":[cash_advance], | |
| "PURCHASES_FREQUENCY":[purchases_frequency], | |
| "ONEOFF_PURCHASES_FREQUENCY":[oneoff_frequency], | |
| "PURCHASES_INSTALLMENTS_FREQUENCY":[installments_frequency], | |
| "CASH_ADVANCE_FREQUENCY":[0], | |
| "CASH_ADVANCE_TRX":[0], | |
| "PURCHASES_TRX":[10], | |
| "CREDIT_LIMIT":[credit_limit], | |
| "PAYMENTS":[payments], | |
| "MINIMUM_PAYMENTS":[minimum_payments], | |
| "PRC_FULL_PAYMENT":[full_payment], | |
| "TENURE":[tenure] | |
| }) | |
| # Log dönüşümü | |
| input_data = np.log1p(input_data) | |
| # Scaling | |
| input_scaled = scaler.transform(input_data) | |
| # Tahmin | |
| if st.button("Predict Cluster"): | |
| cluster = model.predict(input_scaled) | |
| st.subheader(f"Predicted Cluster: {cluster[0]}") | |
| if cluster[0] == 0: | |
| st.write("Risky cash advance users") | |
| elif cluster[0] == 1: | |
| st.write("Premium high spending customers") | |
| elif cluster[0] == 2: | |
| st.write("Balanced active customers") | |
| elif cluster[0] == 3: | |
| st.write("Low spending passive customers") | |
| else: | |
| st.write("Low balance customers") |