Реализация AI-системы реактивации неактивных клиентов
Реактивация "спящих" клиентов дешевле привлечения новых в 5-7 раз. AI-система определяет оптимальный момент реактивации, сегментирует неактивных по причине оттока и формирует персонализированное предложение для каждого сегмента.
Модель сегментации неактивных
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from anthropic import Anthropic
class InactiveCustomerAnalyzer:
def __init__(self, inactivity_threshold_days: int = 90):
self.threshold = inactivity_threshold_days
self.llm = Anthropic()
self.scaler = StandardScaler()
def identify_inactive(self, customers_df: pd.DataFrame,
last_activity_col: str = 'last_purchase_date') -> pd.DataFrame:
"""Определение неактивных клиентов"""
customers_df['days_inactive'] = (
pd.Timestamp.now() -
pd.to_datetime(customers_df[last_activity_col])
).dt.days
inactive = customers_df[
customers_df['days_inactive'] >= self.threshold
].copy()
return inactive
def segment_inactive(self, inactive_df: pd.DataFrame) -> pd.DataFrame:
"""Кластеризация неактивных по паттерну поведения"""
features = pd.DataFrame()
features['days_inactive'] = inactive_df['days_inactive']
features['total_orders'] = inactive_df.get('total_orders', 1)
features['avg_order_value'] = inactive_df.get('avg_order_value', 0)
features['order_frequency'] = inactive_df.get('order_frequency', 0)
features['last_order_value'] = inactive_df.get('last_order_value', 0)
features['support_issues'] = inactive_df.get('support_tickets_total', 0)
X = self.scaler.fit_transform(features.fillna(0))
km = KMeans(n_clusters=5, random_state=42, n_init=10)
inactive_df['segment'] = km.fit_predict(X)
# Описание сегментов
segment_profiles = features.copy()
segment_profiles['segment'] = inactive_df['segment']
segment_stats = segment_profiles.groupby('segment').mean()
# LLM называет каждый сегмент
for seg_id in range(5):
if seg_id not in segment_stats.index:
continue
stats = segment_stats.loc[seg_id].to_dict()
stats_str = ", ".join([f"{k}: {v:.1f}" for k, v in stats.items()])
response = self.llm.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=100,
messages=[{
"role": "user",
"content": f"""Name this inactive customer segment (3-5 words) and suggest reactivation approach.
Stats: {stats_str}
Return: "Segment Name | 1-sentence strategy" """
}]
)
print(f"Segment {seg_id}: {response.content[0].text}")
return inactive_df
class ReactivationCampaign:
"""Кампания реактивации с персонализацией"""
def __init__(self):
self.llm = Anthropic()
self.reactivation_offers = {
0: {'discount': 20, 'message_theme': 'we_miss_you'},
1: {'discount': 15, 'message_theme': 'best_of_what_they_liked'},
2: {'discount': 10, 'free_shipping': True, 'message_theme': 'new_arrivals'},
3: {'special_access': True, 'message_theme': 'exclusive_comeback'},
4: {'survey': True, 'small_incentive': True, 'message_theme': 'help_us_improve'},
}
def create_reactivation_email(self, user: dict, segment: int) -> dict:
"""Персонализированное письмо реактивации"""
offer = self.reactivation_offers.get(segment, {'discount': 10})
days_inactive = user.get('days_inactive', 90)
past_categories = user.get('top_categories', ['products'])
response = self.llm.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=300,
messages=[{
"role": "user",
"content": f"""Write a reactivation email for an inactive customer.
Customer: {user.get('first_name', 'Customer')}
Inactive for: {days_inactive} days
Past purchases: {', '.join(past_categories[:3])}
Offer: {offer}
Requirements:
- Subject line (engaging, personal, 50 chars max)
- Body (150 words max, warm tone, mention specific past interest)
- Clear CTA
Return JSON: {{"subject": "...", "body": "...", "cta": "..."}}"""
}]
)
try:
import json
return json.loads(response.content[0].text)
except Exception:
return {'subject': f"We miss you, {user.get('first_name', '')}!",
'body': response.content[0].text[:400], 'cta': 'Come Back'}
def predict_reactivation_probability(self, user: dict,
offer: dict) -> float:
"""Вероятность реактивации при данном предложении"""
# Упрощённая эвристика (в реальности — обученная модель)
base_prob = 0.05 # Базовая вероятность
# Факторы, повышающие вероятность
if user.get('total_orders', 0) > 5:
base_prob += 0.05 # Лояльный клиент
if user.get('days_inactive', 999) < 180:
base_prob += 0.08 # Недавно ушёл
if offer.get('discount', 0) >= 20:
base_prob += 0.06 # Хорошая скидка
if user.get('email_open_rate', 0) > 0.3:
base_prob += 0.04 # Открывает письма
return min(base_prob, 0.4)
Типичная конверсия реактивационной кампании: 5-15% в зависимости от давности неактивности и качества персонализации. Оптимальные временные окна: 90-180 дней неактивности (лучший response rate), 180-365 дней (нужна более сильная мотивация), > 365 дней (обычно дешевле считать churned. Рекомендуется 1-2 письма с интервалом 7-14 дней, затем прекратить.







