Segmentation AI

AI Customer Segmentation

Yapay zeka ile müşteri segmentasyonu. K-means clustering, RFM, behavioral segmentation. Machine learning ile otomatik gruplandırma ve kişiselleştirme.

🎯Segmentasyon Yöntemleri

AI Customer Segmentation, müşterileri benzer özelliklere göre gruplar. Kişiselleştirilmiş kampanyalar %300'e kadar daha etkilidir.

Demographic

• Yaş, cinsiyet, gelir
• Lokasyon, eğitim
• Meslek, aile durumu
• Basit ama yüzeysel

Behavioral

• Satın alma geçmişi
• Tıklama/görüntüleme
• Engagement (etkileşim)
• En doğru segmentasyon

Psychographic

• İlgi alanları, hobiler
• Değerler, tutumlar
• Yaşam tarzı
• Derinlemesine anlayış

K-Means Clustering

# K-Means ile müşteri segmentasyonu
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Müşteri verileri
df = pd.read_csv('customers.csv')

# Feature engineering
features = pd.DataFrame({
    'total_spend': df['total_spend'],  # Toplam harcama
    'order_count': df['order_count'],  # Sipariş sayısı
    'avg_order_value': df['total_spend'] / df['order_count'],  # Ortalama sepet
    'days_since_last_purchase': df['days_since_last_purchase'],  # Recency
    'product_diversity': df['unique_products_bought'],  # Ürün çeşitliliği
    'discount_usage': df['orders_with_discount'] / df['order_count'],  # İndirim kullanımı
    'return_rate': df['returned_items'] / df['total_items'],  # İade oranı
    'mobile_usage': df['mobile_orders'] / df['order_count']  # Mobil kullanım
})

# Normalize (önemli!)
scaler = StandardScaler()
features_scaled = scaler.fit_transform(features)

# Optimal küme sayısını bul (Elbow Method)
inertias = []
K_range = range(2, 11)

for k in K_range:
    kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
    kmeans.fit(features_scaled)
    inertias.append(kmeans.inertia_)

# Elbow grafiği
plt.plot(K_range, inertias, 'bo-')
plt.xlabel('Number of Clusters (k)')
plt.ylabel('Inertia')
plt.title('Elbow Method')
plt.show()

# Optimal k = 5 (örnek)
k = 5
kmeans = KMeans(n_clusters=k, random_state=42, n_init=10)
df['segment'] = kmeans.fit_predict(features_scaled)

# Segment analizi
segment_summary = df.groupby('segment').agg({
    'total_spend': 'mean',
    'order_count': 'mean',
    'days_since_last_purchase': 'mean',
    'customer_id': 'count'
}).rename(columns={'customer_id': 'customer_count'})

print(segment_summary)
#          total_spend  order_count  days_since_last_purchase  customer_count
# Segment
# 0           125000         8.2                15                 1250
# 1            15000         2.1               120                 3500
# 2            75000         5.5                30                 2200
# 3             3000         1.2               200                 4000
# 4           350000        15.8                 7                  350

# Segment isimlendirme
segment_names = {
    0: 'Loyal High-Value',      # Yüksek harcama, sık alışveriş, son zamanlarda aktif
    1: 'Occasional Shoppers',   # Orta harcama, az alışveriş
    2: 'Regular Customers',     # Düzenli alışveriş, orta harcama
    3: 'Dormant/Lost',          # Uzun süredir alışveriş yok
    4: 'VIP Champions'          # En yüksek harcama, çok sık alışveriş
}

df['segment_name'] = df['segment'].map(segment_names)

# Görselleştirme (2D projection - PCA)
from sklearn.decomposition import PCA

pca = PCA(n_components=2)
features_2d = pca.fit_transform(features_scaled)

plt.figure(figsize=(12, 8))
for segment in range(k):
    mask = df['segment'] == segment
    plt.scatter(
        features_2d[mask, 0],
        features_2d[mask, 1],
        label=segment_names[segment],
        alpha=0.6
    )
plt.xlabel('PCA Component 1')
plt.ylabel('PCA Component 2')
plt.title('Customer Segments (K-Means)')
plt.legend()
plt.show()

DBSCAN (Density-Based Clustering)

# DBSCAN - K-means'ten farkı: outlier detection, küme sayısı otomatik
from sklearn.cluster import DBSCAN
from sklearn.neighbors import NearestNeighbors

# Optimal eps (epsilon) parametresini bul
neighbors = NearestNeighbors(n_neighbors=5)
neighbors.fit(features_scaled)
distances, indices = neighbors.kneighbors(features_scaled)

# k-distance grafiği
distances = np.sort(distances[:, -1], axis=0)
plt.plot(distances)
plt.ylabel('5-NN Distance')
plt.xlabel('Data Points sorted by distance')
plt.title('k-distance Graph (Elbow: optimal eps)')
plt.show()

# DBSCAN
eps = 0.5  # Elbow noktasından seçilir
min_samples = 10

dbscan = DBSCAN(eps=eps, min_samples=min_samples)
df['dbscan_cluster'] = dbscan.fit_predict(features_scaled)

# -1 = outlier (anomali müşteriler)
outliers = df[df['dbscan_cluster'] == -1]
print(f"\nOutliers: {len(outliers)} customers")
print(outliers[['customer_id', 'total_spend', 'order_count']].head())

# Outlier'lar genellikle:
# - Çok yüksek harcama yapan VIP'ler
# - Veya çok düşük engagement (spam hesaplar)

# Küme sayısı
n_clusters = len(set(dbscan.labels_)) - (1 if -1 in dbscan.labels_ else 0)
print(f"Number of clusters: {n_clusters}")

Hierarchical Clustering

# Hierarchical Clustering + Dendrogram
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.cluster import AgglomerativeClustering

# Linkage matrisi oluştur
linkage_matrix = linkage(features_scaled, method='ward')

# Dendrogram (hiyerarşik ağaç)
plt.figure(figsize=(12, 8))
dendrogram(linkage_matrix, truncate_mode='lastp', p=30)
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('Customer Index (or Cluster Size)')
plt.ylabel('Distance')
plt.show()

# Dendrogram'dan optimal küme sayısını seç (örnek: 6)
n_clusters = 6

hc = AgglomerativeClustering(n_clusters=n_clusters, linkage='ward')
df['hierarchical_cluster'] = hc.fit_predict(features_scaled)

# Avantaj: Alt-segmentler oluşturabilirsin
# Örnek: "High Value" segmenti içinde "Tech Enthusiasts" alt-segmenti

Segment Profiling ve Actionable Insights

# Her segment için detaylı profil ve aksiyon önerileri
def profile_segment(segment_id):
    segment_data = df[df['segment'] == segment_id]

    profile = {
        'size': len(segment_data),
        'avg_total_spend': segment_data['total_spend'].mean(),
        'avg_order_count': segment_data['order_count'].mean(),
        'avg_order_value': (segment_data['total_spend'] / segment_data['order_count']).mean(),
        'avg_recency': segment_data['days_since_last_purchase'].mean(),
        'discount_lovers': (segment_data['discount_usage'] > 0.5).sum() / len(segment_data),
        'mobile_users': (segment_data['mobile_usage'] > 0.7).sum() / len(segment_data),
        'top_categories': segment_data['favorite_category'].value_counts().head(3).to_dict()
    }

    return profile

# Segment profilleri
for segment_id in range(k):
    print(f"\n{'='*60}")
    print(f"SEGMENT {segment_id}: {segment_names[segment_id]}")
    print('='*60)

    profile = profile_segment(segment_id)

    print(f"Size: {profile['size']:,} customers")
    print(f"Avg Total Spend: {profile['avg_total_spend']:,.0f} TL")
    print(f"Avg Order Count: {profile['avg_order_count']:.1f}")
    print(f"Avg Order Value: {profile['avg_order_value']:,.0f} TL")
    print(f"Avg Recency: {profile['avg_recency']:.0f} days")
    print(f"Discount Lovers: {profile['discount_lovers']:.1%}")
    print(f"Mobile Users: {profile['mobile_users']:.1%}")
    print(f"Top Categories: {profile['top_categories']}")

    # Aksiyon önerileri
    recommendations = recommend_actions(segment_id, profile)
    print(f"\nRecommended Actions:")
    for action in recommendations:
        print(f"  - {action}")

def recommend_actions(segment_id, profile):
    """Segment bazlı kampanya/aksiyon önerileri"""
    actions = []

    # VIP Champions (Segment 4)
    if segment_id == 4:
        actions.append("VIP exclusive products/early access")
        actions.append("Personal shopping assistant")
        actions.append("Birthday gifts, anniversary rewards")
        actions.append("Refer-a-friend program (high incentive)")

    # Loyal High-Value (Segment 0)
    elif segment_id == 0:
        actions.append("Loyalty program tier upgrade")
        actions.append("Cross-sell/upsell recommendations")
        actions.append("Bundle offers")
        actions.append("Subscription model pitch")

    # Dormant/Lost (Segment 3)
    elif segment_id == 3:
        actions.append("Win-back email campaign (steep discounts)")
        actions.append("Survey: Why did you leave?")
        actions.append("Retargeting ads (social media)")
        actions.append("Limited-time offer (urgency)")

    # Occasional Shoppers (Segment 1)
    elif segment_id == 1:
        if profile['discount_lovers'] > 0.6:
            actions.append("Flash sales notifications")
            actions.append("Coupon codes (push them to buy more)")
        else:
            actions.append("Product discovery emails (new arrivals)")
            actions.append("Personalized recommendations")

    # Regular Customers (Segment 2)
    elif segment_id == 2:
        actions.append("Predictive recommendations (next purchase)")
        actions.append("Subscription model (auto-replenishment)")
        actions.append("Loyalty rewards (encourage upgrade to High-Value)")

    return actions

# Output örneği:
# ============================================================
# SEGMENT 4: VIP Champions
# ============================================================
# Size: 350 customers
# Avg Total Spend: 350,000 TL
# Avg Order Count: 15.8
# Avg Order Value: 22,152 TL
# Avg Recency: 7 days
# Discount Lovers: 15.2%
# Mobile Users: 82.5%
# Top Categories: {'Electronics': 120, 'Fashion': 85, 'Home': 65}
#
# Recommended Actions:
#   - VIP exclusive products/early access
#   - Personal shopping assistant
#   - Birthday gifts, anniversary rewards
#   - Refer-a-friend program (high incentive)

Realtime Segmentation API

# Flask API - Yeni müşteri için segment tahmini
from flask import Flask, request, jsonify
import joblib

app = Flask(__name__)

# Pre-trained model ve scaler yükle
kmeans_model = joblib.load('kmeans_model.pkl')
scaler = joblib.load('scaler.pkl')
segment_names = joblib.load('segment_names.pkl')

@app.route('/predict-segment', methods=['POST'])
def predict_segment():
    data = request.json

    # Features
    features = [[
        data['total_spend'],
        data['order_count'],
        data['total_spend'] / data['order_count'],  # AOV
        data['days_since_last_purchase'],
        data['unique_products'],
        data['discount_usage'],
        data['return_rate'],
        data['mobile_usage']
    ]]

    # Normalize
    features_scaled = scaler.transform(features)

    # Tahmin
    segment_id = kmeans_model.predict(features_scaled)[0]
    segment_name = segment_names[segment_id]

    # Segment profile
    profile = profile_segment(segment_id)

    # Kampanya önerileri
    campaigns = recommend_actions(segment_id, profile)

    return jsonify({
        'segment_id': int(segment_id),
        'segment_name': segment_name,
        'recommended_campaigns': campaigns,
        'segment_stats': {
            'avg_spend': profile['avg_total_spend'],
            'avg_orders': profile['avg_order_count']
        }
    })

# Kullanım
# POST /predict-segment
# {
#   "total_spend": 125000,
#   "order_count": 8,
#   "days_since_last_purchase": 15,
#   "unique_products": 25,
#   "discount_usage": 0.3,
#   "return_rate": 0.05,
#   "mobile_usage": 0.75
# }
#
# Response:
# {
#   "segment_id": 0,
#   "segment_name": "Loyal High-Value",
#   "recommended_campaigns": [
#     "Loyalty program tier upgrade",
#     "Cross-sell/upsell recommendations",
#     "Bundle offers"
#   ],
#   "segment_stats": {
#     "avg_spend": 125000,
#     "avg_orders": 8.2
#   }
# }

if __name__ == '__main__':
    app.run(port=5000)

İş Etkileri ve ROI

Segmentation ROI

+300%
Campaign ROI
+52%
Email Open Rate
+38%
Conversion Rate

Örnek: E-Ticaret (100K müşteri)

Önce: Tüm müşterilere aynı email → %2.5 conversion
Segmentasyon sonrası: 5 segment, özel kampanyalar
VIP segment: Özel indirim → %12 conversion (+%380)
Dormant segment: Win-back → %6 conversion (+%140)
Regular segment: Upsell → %8 conversion (+%220)
Ortalama conversion: %7.2 (+%188)
Gelir artışı: %188 × 50M TL ciro = 94M TL ek gelir/yıl

AI Customer Segmentation Çözümünüzü Kuralım

K-means, DBSCAN, hierarchical clustering ile müşterilerinizi segmentleyin. Kişiselleştirilmiş kampanyalarla conversion rate'i 3x artırın.

Demo İste