File size: 4,295 Bytes
60ab12d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6249c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import streamlit as st
import numpy as np
import joblib

# MODEL YÜKLE / LOAD MODEL
# Dosya adlarının klasöründekilerle aynı olduğundan emin ol!
try:
    model = joblib.load("kmeans.pkl")
    scaler = joblib.load("scaler.pkl")
except:
    st.error("Model dosyaları bulunamadı! / Model files not found!")

st.set_page_config(page_title="Customer Segmentation", page_icon="💎")
st.title("Customer Personality Segmentation / Müşteri Segmentasyonu")
st.write("---")

# 📊 SIDEBAR (Kriterler / Criteria)
st.sidebar.header("📊 Segment Criteria / Segment Kriterleri")

st.sidebar.subheader("💎 Premium")
st.sidebar.write("Income / Gelir > 100,000")
st.sidebar.write("Spending / Harcama > 2,000")
st.sidebar.write("Children / Çocuk = 0")

st.sidebar.write("---")

st.sidebar.subheader("🛒 Budget / Bütçeli")
st.sidebar.write("Income / Gelir > 40,000")
st.sidebar.write("Spending / Harcama > 500")

st.sidebar.write("---")

st.sidebar.subheader("📉 Occasional / Seyrek")
st.sidebar.write("Income / Gelir < 20,000")
st.sidebar.write("Spending / Harcama < 100")

st.sidebar.write("---")

st.sidebar.subheader("⚠️ Alternative / Alternatif")
st.sidebar.write("Others / Diğerleri")

# 🎯 INPUTLAR / INPUTS
col1, col2 = st.columns(2)

with col1:
    # İstediğin 110.000 ve 3.000 değerlerini varsayılan (default) yaptım:
    income = st.number_input("Income / Gelir ($)", value=110000)
    spending = st.number_input("Spending / Harcama ($)", value=3000)

with col2:
    kidhome = st.number_input("Kids / Çocuklar", value=0)
    teenhome = st.number_input("Teens / Gençler", value=0)
    recency = st.slider("Recency / Son Alışveriş (Gün)", 0, 100, 15)

# FEATURE ENGINEERING
children = kidhome + teenhome

st.write("---")

# 🔥 SEGMENT FONKSİYONU / SEGMENT FUNCTION
def segment_belirle(income, spending, children):
    # Premium Kuralı
    if income > 100000 and spending > 2000 and children == 0:
        return "💎 Premium"
    # Budget Kuralı
    elif income > 40000 and spending > 500:
        return "🛒 Budget"
    # Occasional Kuralı
    elif income < 20000 and spending < 100:
        return "📉 Occasional"
    # Diğerleri
    else:
        return "⚠️ Alternative"

# 🚀 BUTON / PREDICT
if st.button("Predict Segment / Segmenti Tahmin Et", use_container_width=True):

    try:
        # MODEL GİRDİSİ (4 Özellik beklediğini varsayıyoruz)
        # Sıralama: [Income, Spending, Children, Recency]
        data = np.array([[income, spending, children, recency]])
        data_scaled = scaler.transform(data)
        cluster = model.predict(data_scaled)[0]

        # 🔥 ASIL SONUÇ (KURAL)
        segment = segment_belirle(income, spending, children)

        # Kullanıcıya bilgi ver / Info Box
        st.info(f"""
        📌 **Entered Values / Girilen Değerler:**
        - Income / Gelir: **${income:,}**
        - Spending / Harcama: **${spending:,}**
        - Total Children / Toplam Çocuk: **{children}**
        - Recency / Güncellik: **{recency} days**
        """)

        st.write(f"🔍 **AI Cluster / Model Kümesi:** {cluster}")

        # 🎯 SONUÇ GÖSTERİMİ / RESULT DISPLAY
        if segment == "💎 Premium":
            st.balloons() # 🎈 Premium tebriği!
            st.success("### Result: 💎 Premium Customer / Değerli Müşteri")
            st.write("This customer is high-value and loyal. / Bu müşteri yüksek değerli ve sadıktır.")

        elif segment == "🛒 Budget":
            st.info("### Result: 🛒 Budget Customer / Bütçeli Müşteri")
            st.write("Standard customer with stable spending. / Dengeli harcaması olan standart müşteri.")

        elif segment == "📉 Occasional":
            st.warning("### Result: 📉 Occasional Customer / Seyrek Müşteri")
            st.write("Low frequency, potential for campaigns. / Düşük frekanslı, kampanya hedefi olabilir.")

        else:
            st.error("### Result: ⚠️ Alternative Customer / Alternatif Müşteri")
            st.write("Uncategorized or unique behavior. / Kategorize edilmemiş veya özel davranışlı.")

    except Exception as e:
        st.error(f"Error / Hata oluştu: {e}")

st.write("---")
st.caption("Marketing Campaign Analysis 2026 | Streamlit Multi-Language App")