-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
188 lines (164 loc) · 7.54 KB
/
script.py
File metadata and controls
188 lines (164 loc) · 7.54 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# =============================================
# ONLINE RETAIL - COMPLETE CUSTOMER ANALYTICS SUITE
# Windows-safe version (no Unicode errors)
# =============================================
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
import warnings
warnings.filterwarnings('ignore')
# Create charts folder
Path("charts").mkdir(exist_ok=True)
# -------------------------------
# 1. LOAD & CLEAN DATA
# -------------------------------
print("Loading data...")
df = pd.read_csv("data.csv", encoding='latin1', low_memory=False)
print(f"Original shape: {df.shape}")
df['InvoiceDate'] = pd.to_datetime(df['InvoiceDate'], errors='coerce')
# Clean
df = df[~df['InvoiceNo'].astype(str).str.startswith('C')]
df = df[df['Quantity'] > 0]
df = df[df['UnitPrice'] > 0]
df = df.dropna(subset=['CustomerID'])
df['CustomerID'] = df['CustomerID'].astype(int)
df['TotalAmount'] = df['Quantity'] * df['UnitPrice']
print(f"Cleaned shape: {df.shape}")
df.to_csv("clean_data.csv", index=False)
# -------------------------------
# 2. RFM + SEGMENT + CLV + CHURN
# -------------------------------
print("\nBUILDING RFM...")
snapshot_date = df['InvoiceDate'].max() + pd.Timedelta(days=1)
rfm = df.groupby('CustomerID').agg({
'InvoiceDate': lambda x: (snapshot_date - x.max()).days,
'InvoiceNo': 'nunique',
'TotalAmount': 'sum'
}).rename(columns={
'InvoiceDate': 'Recency',
'InvoiceNo': 'Frequency',
'TotalAmount': 'Monetary'
}).reset_index()
rfm['R_score'] = pd.qcut(rfm['Recency'], 5, labels=[5,4,3,2,1], duplicates='drop').astype(int)
rfm['F_score'] = pd.qcut(rfm['Frequency'].rank(method='first'), 5, labels=[1,2,3,4,5], duplicates='drop').astype(int)
rfm['M_score'] = pd.qcut(rfm['Monetary'], 5, labels=[1,2,3,4,5], duplicates='drop').astype(int)
rfm['RFM_Score'] = rfm['R_score'].astype(str) + rfm['F_score'].astype(str) + rfm['M_score'].astype(str)
def assign_segment(row):
r, f, m = row['R_score'], row['F_score'], row['M_score']
if r >= 4 and f >= 4: return "Champions"
if r >= 3 and f >= 3: return "Loyal Customers"
if r >= 4 and f >= 2: return "Potential Loyalists"
if r >= 4 and f >= 1: return "New Customers"
if r >= 2 and f >= 3: return "At Risk"
if r <= 2 and f >= 4: return "Can't Lose Them"
if r <= 2 and f >= 2: return "Hibernating"
if r <= 2 and f <= 2: return "Lost"
if m >= 4: return "Big Spenders"
return "Low Value"
rfm['Segment'] = rfm.apply(assign_segment, axis=1)
# CLV
avg_order_value = df.groupby('CustomerID')['TotalAmount'].mean()
purchase_freq = df.groupby('CustomerID')['InvoiceNo'].nunique()
estimated_lifespan = 365 / (rfm.set_index('CustomerID')['Recency'].replace(0, 1))
rfm['CLV_Estimate'] = rfm['CustomerID'].map(avg_order_value * purchase_freq * estimated_lifespan).round(2)
# Churn
last_purchase = df.groupby('CustomerID')['InvoiceDate'].max()
days_since = (pd.Timestamp('today') - last_purchase).dt.days
avg_gap = df.groupby('CustomerID')['InvoiceDate'].apply(lambda x: x.sort_values().diff().dt.days.mean())
rfm['Churned'] = rfm['CustomerID'].map(days_since > avg_gap * 4).astype(int)
# Summary
summary = rfm.groupby('Segment').agg({
'CustomerID': 'count',
'Monetary': 'sum',
'Recency': 'mean',
'Frequency': 'mean',
'CLV_Estimate': 'sum'
}).round(0)
summary.columns = ['Customers', 'TotalRevenue', 'AvgRecency', 'AvgFrequency', 'TotalCLV']
summary['% Customers'] = (summary['Customers'] / summary['Customers'].sum() * 100).round(1)
summary['% Revenue'] = (summary['TotalRevenue'] / summary['TotalRevenue'].sum() * 100).round(1)
summary = summary.sort_values('TotalRevenue', ascending=False)
# -------------------------------
# 3. SAVE ALL CHARTS
# -------------------------------
plt.style.use('seaborn-v0_8-darkgrid')
# Monthly Revenue Trend
monthly_rev = df.groupby(df['InvoiceDate'].dt.to_period('M'))['TotalAmount'].sum()
plt.figure(figsize=(14,7))
monthly_rev.plot(kind='line', marker='o', linewidth=3, color='#2ecc71')
plt.title('Monthly Revenue Trend', fontsize=18, fontweight='bold')
plt.ylabel('Revenue (GBP)')
plt.xlabel('Month')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("charts/01_Monthly_Revenue_Trend.png", dpi=300, bbox_inches='tight')
plt.close()
# Revenue by Segment
plt.figure(figsize=(12,7))
sns.barplot(data=summary.reset_index(), x='Segment', y='TotalRevenue', palette='viridis')
plt.title('Revenue by Customer Segment', fontsize=18, fontweight='bold')
plt.ylabel('Revenue (GBP)')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig("charts/02_Revenue_by_Segment.png", dpi=300, bbox_inches='tight')
plt.close()
# Customers by Segment
plt.figure(figsize=(12,7))
sns.barplot(data=summary.reset_index(), x='Segment', y='Customers', palette='magma')
plt.title('Customer Count by Segment', fontsize=18, fontweight='bold')
plt.ylabel('Number of Customers')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig("charts/03_Customers_by_Segment.png", dpi=300, bbox_inches='tight')
plt.close()
# Cohort Heatmap
df_cohort = df[['CustomerID', 'InvoiceDate']].copy()
df_cohort['OrderMonth'] = df_cohort['InvoiceDate'].dt.to_period('M')
df_cohort['CohortMonth'] = df_cohort.groupby('CustomerID')['InvoiceDate'].transform('min').dt.to_period('M')
cohort_data = df_cohort.groupby(['CohortMonth', 'OrderMonth']).agg(n_customers=('CustomerID', 'nunique')).reset_index()
cohort_data['CohortPeriod'] = (cohort_data['OrderMonth'] - cohort_data['CohortMonth']).apply(lambda x: x.n)
cohort_pivot = cohort_data.pivot(index='CohortMonth', columns='CohortPeriod', values='n_customers')
cohort_sizes = cohort_pivot.iloc[:, 0]
retention = cohort_pivot.divide(cohort_sizes, axis=0)
plt.figure(figsize=(14,10))
sns.heatmap(retention, annot=True, fmt='.0%', cmap='Blues', linewidths=.5)
plt.title('Customer Retention by Cohort', fontsize=18, fontweight='bold')
plt.ylabel('Cohort Month')
plt.xlabel('Months Since First Purchase')
plt.tight_layout()
plt.savefig("charts/04_Cohort_Retention_Heatmap.png", dpi=300, bbox_inches='tight')
plt.close()
# RFM Heatmap
heatmap_data = rfm.pivot_table(values='Monetary', index='R_score', columns='F_score', aggfunc='mean').sort_index(ascending=False)
plt.figure(figsize=(10,7))
sns.heatmap(heatmap_data, annot=True, fmt=".0f", cmap="YlGnBu")
plt.title('Average Spend by RF Score', fontsize=18, fontweight='bold')
plt.xlabel('Frequency Score')
plt.ylabel('Recency Score (5 = most recent)')
plt.tight_layout()
plt.savefig("charts/05_RFM_Heatmap.png", dpi=300, bbox_inches='tight')
plt.close()
# -------------------------------
# 4. EXPORT
# -------------------------------
summary = summary.reset_index()
rfm.to_csv("FINAL_RFM_CUSTOMERS.csv", index=False)
summary.to_csv("MARKETING_DASHBOARD_SUMMARY.csv", index=False)
# Windows-safe success message
print("\n" + "="*70)
print("SUCCESS! YOUR FULL E-COMMERCE ANALYTICS PACK IS READY!")
print("="*70)
print("Files created:")
print(" - clean_data.csv -> 397,884 cleaned transactions")
print(" - FINAL_RFM_CUSTOMERS.csv -> 4,338 customers + RFM + CLV + Churn")
print(" - MARKETING_DASHBOARD_SUMMARY.csv-> Executive summary")
print(" - charts/ folder -> 5 high-quality charts:")
print(" 01_Monthly_Revenue_Trend.png")
print(" 02_Revenue_by_Segment.png")
print(" 03_Customers_by_Segment.png")
print(" 04_Cohort_Retention_Heatmap.png")
print(" 05_RFM_Heatmap.png")
print("\nOpen the 'charts' folder and drop the images into PowerPoint.")
print("You're done. Go be a legend.")