← Back to all products
$39
Customer Segmentation Toolkit
RFM analysis, customer lifetime value calculation, churn prediction models, and personalized campaign targeting scripts.
PythonYAMLMarkdownJSON
Product Preview
Try the interactive reader and demo tools below, or get the full product with all content unlocked.
Interactive Reader (Free Preview) ⚙ Try Demo Tools Download Free SampleFile Structure 28 files
customer-segmentation-toolkit/
├── LICENSE
├── README.md
├── configs/
│ └── segmentation_config.yaml
├── data/
│ ├── sample_customers.csv
│ └── sample_transactions.csv
├── free-sample.zip
├── guide/
│ ├── 01-overview.md
│ ├── 02-rfm-analysis-for-customer-segmentation.md
│ └── 03-customer-lifetime-value-and-churn-predic.md
├── guides/
│ └── segmentation_guide.md
├── index.html
├── interactive.html
├── src/
│ ├── __init__.py
│ ├── __pycache__/
│ │ ├── __init__.cpython-312.pyc
│ │ ├── campaign_targeting.cpython-312.pyc
│ │ ├── churn_predictor.cpython-312.pyc
│ │ ├── clv_calculator.cpython-312.pyc
│ │ ├── rfm_engine.cpython-312.pyc
│ │ └── segment_definitions.cpython-312.pyc
│ ├── campaign_targeting.py
│ ├── churn_predictor.py
│ ├── clv_calculator.py
│ ├── rfm_engine.py
│ └── segment_definitions.py
└── tests/
├── __pycache__/
│ └── test_segmentation.cpython-312-pytest-9.0.2.pyc
└── test_segmentation.py
Documentation Preview README excerpt
Customer Segmentation Toolkit
A complete Python pipeline for customer segmentation: RFM scoring, lifetime value estimation, churn prediction, segment classification, and campaign targeting — all from raw transaction data.
What You Get
- RFM Scoring Engine — Recency-Frequency-Monetary analysis with quintile scoring and named segment mapping (Champions, At Risk, Dormant, etc.)
- CLV Calculator — Historical and predictive customer lifetime value with NPV discounting and tier assignment
- Churn Prediction Model — Feature engineering pipeline + logistic regression / random forest classifier (scikit-learn, guarded). Falls back to an effective rule-based scorer when sklearn is not available
- Segment Definitions — 8 unified segments combining RFM, CLV, and churn data with recommended marketing actions for each
- Campaign Targeting — Export audience lists for email platforms (Mailchimp/Klaviyo-ready CSV) and ad platforms (hashed emails for lookalike audiences), plus auto-generated campaign briefs
- Sample Data — Customer and transaction CSVs ready to test the full pipeline
Project Structure
customer-segmentation-toolkit/
├── README.md
├── LICENSE
├── .gitignore
├── src/
│ ├── __init__.py
│ ├── rfm_engine.py # RFM scoring & segmentation
│ ├── clv_calculator.py # Customer lifetime value
│ ├── churn_predictor.py # Churn prediction (ML + rules)
│ ├── segment_definitions.py # Unified segment classification
│ └── campaign_targeting.py # Audience export & campaign briefs
├── data/
│ ├── sample_customers.csv # 30 customer profiles
│ └── sample_transactions.csv # 50 transaction records
├── tests/
│ └── test_segmentation.py # Comprehensive test suite
├── configs/
│ └── segmentation_config.yaml # Configuration reference
└── guides/
└── segmentation_guide.md # Detailed usage guide
Quick Start
# Run RFM analysis on sample data
python -m src.rfm_engine data/sample_transactions.csv
# Compute customer lifetime value
python -m src.clv_calculator data/sample_transactions.csv
# Run churn prediction
python -m src.churn_predictor data/sample_transactions.csv
# See segment definitions
python -m src.segment_definitions
# Run all tests
python -m pytest tests/ -v
Usage Example
*... continues with setup instructions, usage examples, and more.*
Code Sample .py preview
src/campaign_targeting.py
"""
Campaign Targeting & Export
============================
Takes segment assignments and produces audience lists ready for email
platforms (Mailchimp, Klaviyo, etc.) and ad platforms (Meta, Google Ads).
Generates:
- CSV audience exports with customer attributes and segment tags
- Platform-specific formatted files
- Campaign brief templates with recommended messaging per segment
- Suppression lists (e.g. exclude churned customers from acquisition ads)
Usage::
targeter = CampaignTargeter(customer_segments)
targeter.export_email_list("output/email_audience.csv", segment="At Risk")
targeter.export_ad_audience("output/meta_lookalike.csv", segment="VIP")
brief = targeter.campaign_brief("At Risk")
"""
from __future__ import annotations
import csv
import json
import logging
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Data structures
# ---------------------------------------------------------------------------
@dataclass
class CustomerProfile:
"""Customer record enriched with segmentation data."""
customer_id: str
email: str
segment: str
clv_tier: str
churn_risk: str
rfm_segment: str
total_spend: float = 0.0
total_orders: int = 0
last_purchase_date: str = ""
# ... 272 more lines ...