Clustermap

E-commerce User Segments

Customer behavior patterns clustered by shopping metrics

Output
E-commerce User Segments
Python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(654)

metrics = ['Avg Order', 'Frequency', 'Recency', 'Cart Size', 'Browse Time',
           'Return Rate', 'Review Score', 'Loyalty Pts', 'Discount Use', 'Mobile %']
segments = ['VIP', 'Regular', 'Occasional', 'New', 'At-Risk', 'Dormant', 'Bargain', 'Premium']

data = np.random.rand(10, 8) * 4
data[:4, 0] += 3
data[8, 6] += 4
data[5, 7] = 0.5

df = pd.DataFrame(data, index=metrics, columns=segments)

seg_value = ['High']*2 + ['Medium']*3 + ['Low']*3
value_palette = {'High': '#6CF527', 'Medium': '#F5B027', 'Low': '#F5276C'}
col_colors = pd.Series(seg_value, index=segments).map(value_palette)

neon_cmap = LinearSegmentedColormap.from_list('ecom', ['#0a0a0f', '#4927F5', '#27D3F5', '#F5B027'])

g = sns.clustermap(df, cmap=neon_cmap, col_colors=col_colors,
                   method='average', metric='euclidean', standard_scale=0,
                   linewidths=0.3, linecolor='#1a1a2e',
                   figsize=(9, 8), dendrogram_ratio=(0.12, 0.12),
                   cbar_pos=(0.01, 0.08, 0.008, 0.12),
                   tree_kws={'linewidths': 1.5, 'colors': '#27D3F5'})

g.fig.patch.set_facecolor('#0a0a0f')
g.ax_heatmap.set_facecolor('#0a0a0f')
g.ax_heatmap.tick_params(colors='white', labelsize=8)
g.ax_row_dendrogram.set_facecolor('#0a0a0f')
g.ax_col_dendrogram.set_facecolor('#0a0a0f')
g.cax.tick_params(colors='white', labelsize=7)

g.fig.suptitle('E-commerce Customer Segments', color='white', fontsize=13, fontweight='bold', y=1.02)
plt.show()
Library

Matplotlib

Category

Heatmaps & Density

Did this help you?

Support PyLucid to keep it free & growing

Support