BlogData Analytics SeriesChapter 18
SERIES · CHAPTER 18Advanced

Predictive Analytics & Machine Learning Basics for Data Analysts

Supervised vs unsupervised learning, decision trees, logistic regression, customer segmentation with K-means, model evaluation metrics — all from a data analyst's perspective, with Indian business use cases (churn prediction, loan default, customer segments) and Python sklearn code.

DATA ANALYTICS SERIES:← Ch 17: A/B TestingCh 18: Predictive Analytics ←Ch 19: Advanced SQL →

The ML Landscape for Data Analysts

SUPERVISED LEARNING
Classification
Predict a category. Will this customer churn? Is this transaction fraudulent? What tier of credit risk is this applicant?
ALGORITHMS:
Logistic Regression, Decision Trees, Random Forest
INDIAN EXAMPLES:
Churn prediction, loan default, lead scoring
SUPERVISED LEARNING
Regression
Predict a number. What will next month's revenue be? What will this customer's LTV be? What price maximises demand?
ALGORITHMS:
Linear Regression, Ridge, Random Forest Regressor
INDIAN EXAMPLES:
Revenue forecasting, LTV prediction, pricing
UNSUPERVISED LEARNING
Clustering
Discover natural groups. Which customer segments exist in my data? What product affinities cluster together?
ALGORITHMS:
K-Means, DBSCAN, Hierarchical Clustering
INDIAN EXAMPLES:
Customer segmentation, product grouping
UNSUPERVISED LEARNING
Dimensionality Reduction
Reduce many features to fewer while preserving information. Useful for visualisation and as a preprocessing step.
ALGORITHMS:
PCA (Principal Component Analysis)
INDIAN EXAMPLES:
Visualising high-dimensional survey data

Classification — Churn Prediction (Decision Tree + Logistic Regression)

Python · Customer Churn Prediction — Indian Telecom Dataset
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.metrics import (classification_report, confusion_matrix,
                              roc_auc_score, roc_curve)
import matplotlib.pyplot as plt

df = pd.read_csv('india_telecom_customers.csv')

# ── Feature Engineering ────────────────────────────────────
df['months_since_last_recharge'] = (
    pd.Timestamp('today') - pd.to_datetime(df['last_recharge_date'])
).dt.days / 30

df['avg_monthly_spend'] = df['total_spend_6m'] / 6

features = [
    'months_since_last_recharge',
    'avg_monthly_spend',
    'total_calls_6m',
    'data_gb_6m',
    'complaints_count',
    'plan_type_enc',
    'city_tier',
]

# Encode categorical
le = LabelEncoder()
df['plan_type_enc'] = le.fit_transform(df['plan_type'])

X = df[features].fillna(0)
y = df['churned']   # 1 = churned, 0 = retained

# Class imbalance check
print(f"Churn rate: {y.mean():.1%}  ({y.sum():,} churned / {len(y):,} total)")

# ── Train / Test Split ────────────────────────────────────
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# ── Model 1: Decision Tree (interpretable) ────────────────
dt = DecisionTreeClassifier(max_depth=4, min_samples_leaf=50, random_state=42)
dt.fit(X_train, y_train)

print("\n=== Decision Tree Rules ===")
print(export_text(dt, feature_names=features, max_depth=3))

# Feature importance
importances = pd.Series(dt.feature_importances_, index=features).sort_values(ascending=False)
print("\nFeature Importance:")
for feat, imp in importances.items():
    bar = '█' * int(imp * 30)
    print(f"  {feat:35s}: {imp:.3f}  {bar}")

# ── Model 2: Logistic Regression ──────────────────────────
scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)
X_test_s  = scaler.transform(X_test)

lr = LogisticRegression(C=1.0, random_state=42, max_iter=1000)
lr.fit(X_train_s, y_train)

# ── Evaluation ───────────────────────────────────────────
for name, model, X_ev in [('Decision Tree', dt, X_test),
                            ('Logistic Reg',  lr, X_test_s)]:
    y_pred = model.predict(X_ev)
    y_prob = model.predict_proba(X_ev)[:, 1]
    auc    = roc_auc_score(y_test, y_prob)

    print(f"\n=== {name} ===")
    print(f"ROC-AUC: {auc:.3f}")
    print(classification_report(y_test, y_pred,
                                  target_names=['Retained','Churned']))
    cm = confusion_matrix(y_test, y_pred)
    tn, fp, fn, tp = cm.ravel()
    print(f"Confusion Matrix: TN={tn}  FP={fp}  FN={fn}  TP={tp}")
    print(f"Precision: {tp/(tp+fp):.1%}  Recall: {tp/(tp+fn):.1%}")

# ── Business Impact: Churn Intervention ROI ───────────────
avg_monthly_revenue_per_customer = 450   # ₹
intervention_cost_per_customer   = 80    # ₹ (discount voucher / retention call)
retention_success_rate           = 0.35  # 35% of contacted customers retained

monthly_churners_caught = tp   # true positives per test month
monthly_revenue_saved   = (monthly_churners_caught * retention_success_rate
                            * avg_monthly_revenue_per_customer * 12)
intervention_cost_total = (tp + fp) * intervention_cost_per_customer

print(f"\n=== Churn Model Business Case ===")
print(f"Churners caught per month:  {monthly_churners_caught:,}")
print(f"Annual revenue saved:        ₹{monthly_revenue_saved/1e5:.1f} lakh")
print(f"Monthly intervention cost:  ₹{intervention_cost_total/1e3:.0f}K")
print(f"Net annual benefit:          ₹{(monthly_revenue_saved - intervention_cost_total*12)/1e5:.1f} lakh")

Clustering — Customer Segmentation with K-Means

K-means groups customers into clusters based on behavioural similarity — without any pre-defined labels. This drives personalised marketing, different product offerings per segment, and targeted retention strategies.

Python · RFM Segmentation — Indian E-Commerce
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

df = pd.read_csv('india_orders.csv', parse_dates=['order_date'])
snapshot_date = pd.Timestamp('2026-09-01')

# ── Build RFM Table ───────────────────────────────────────
# Recency   = days since last order (lower = better)
# Frequency = total number of orders (higher = better)
# Monetary  = total spend in ₹ (higher = better)

rfm = (df.groupby('customer_id')
         .agg(
           recency   = ('order_date', lambda x: (snapshot_date - x.max()).days),
           frequency = ('order_id',   'count'),
           monetary  = ('amount_inr', 'sum'),
         )
         .reset_index())

print(rfm.describe().round(0))

# ── Scale features ───────────────────────────────────────
scaler   = StandardScaler()
rfm_scaled = scaler.fit_transform(rfm[['recency', 'frequency', 'monetary']])

# ── Choose K with Elbow Method ────────────────────────────
inertias = []
k_range  = range(2, 9)
for k in k_range:
    km = KMeans(n_clusters=k, random_state=42, n_init=10)
    km.fit(rfm_scaled)
    inertias.append(km.inertia_)

plt.figure(figsize=(7, 4))
plt.plot(k_range, inertias, 'o-', color='#16a34a', linewidth=2)
plt.xlabel('Number of Clusters (K)'); plt.ylabel('Inertia')
plt.title('Elbow Method — Choose K', fontweight='bold')
plt.xticks(k_range); plt.grid(alpha=0.3); plt.tight_layout(); plt.show()
# Typically K=4 shows clear elbow for RFM data

# ── Fit Final Model ───────────────────────────────────────
K = 4
km_final = KMeans(n_clusters=K, random_state=42, n_init=10)
rfm['segment'] = km_final.fit_predict(rfm_scaled)

# ── Interpret Segments ────────────────────────────────────
seg_profile = rfm.groupby('segment').agg(
    n_customers = ('customer_id', 'count'),
    avg_recency = ('recency',    'mean'),
    avg_orders  = ('frequency',  'mean'),
    avg_spend   = ('monetary',   'mean'),
    total_spend = ('monetary',   'sum'),
).round(0).sort_values('avg_spend', ascending=False)

print("\nSegment Profiles:")
print(seg_profile)

# Label segments based on profile (business interpretation)
# Typical results for Indian e-commerce:
# Seg 0 — High spend, recent, frequent  → "Champions" (VIP)
# Seg 1 — Moderate spend, moderate freq → "Loyal Regulars"
# Seg 2 — Low recency (haven't ordered lately) → "At-Risk"
# Seg 3 — Low freq, low spend, old orders → "Dormant"

segment_labels = {0: 'Champions', 1: 'Loyal Regulars', 2: 'At-Risk', 3: 'Dormant'}
rfm['segment_name'] = rfm['segment'].map(segment_labels)

print("\nMarketing Action by Segment:")
actions = {
    'Champions':      'VIP perks, early access to Diwali sale, referral programme',
    'Loyal Regulars': 'Cross-sell premium category, loyalty tier upgrade offer',
    'At-Risk':        'Win-back email/WhatsApp with 15% coupon, personalised reco',
    'Dormant':        'Low-cost reactivation campaign; sunset if 12m+ no activity',
}
for seg, action in actions.items():
    n = (rfm['segment_name'] == seg).sum()
    print(f"  {seg:18s} ({n:,} customers): {action}")

Model Evaluation — Which Metric to Use

MetricFormulaUse WhenIndian Business Context
AccuracyCorrect / TotalBalanced classes (roughly equal 0s and 1s)Marketing email open prediction when open rate ≈ 50%
PrecisionTP / (TP + FP)False alarms are costly — each intervention costs moneyFraud detection: wrongly blocking a transaction annoys customers
RecallTP / (TP + FN)Missing true positives is costly — missed churn = lost revenueChurn prediction: missing a churner costs 12 months of LTV
F1 Score2 × P×R / (P+R)Imbalanced classes; need to balance precision and recallLead scoring: 5% of leads convert; accuracy is useless
ROC-AUCArea under ROC curveComparing models or selecting probability thresholdComparing logistic regression vs decision tree for loan default
MAEMean |actual − predicted|Regression; want error in original unitsRevenue forecast: MAE of ₹2L means predictions off by ₹2L on average
RMSE√Mean (actual − predicted)²Regression; want to penalise large errors moreDemand forecasting: large stock-out errors are worse than small ones
R-squaredVar explained / Total varRegression; proportion of variance the model explainsAd spend → revenue model: R² = 0.71 explains 71% of revenue variation
Continue the Series
← Ch 17: A/B TestingCh 19: Advanced SQL →

Frequently Asked Questions

What is the difference between a data analyst and a data scientist in India?

In the Indian job market, the boundary is increasingly blurred but a useful working distinction is this: a data analyst answers "what happened and why" using SQL, Excel, Python (pandas), and dashboards. They work with structured business data, focus on descriptive and diagnostic analytics, and communicate findings to business stakeholders. A data scientist answers "what will happen and what should we do" by building predictive and prescriptive models — machine learning pipelines, feature engineering, model training, and deployment. They typically have stronger maths backgrounds (linear algebra, probability, optimisation) and work closer to engineering teams. In practice, many Indian companies expect their "data analysts" to handle basic ML tasks (churn prediction, customer segmentation), which is why understanding predictive analytics concepts is increasingly valuable for analyst roles — especially at e-commerce, fintech, and SaaS companies.

When should a data analyst use machine learning vs simpler methods?

Use simpler methods (regression, SQL, descriptive stats) when: the relationship is approximately linear, you need explainability (you must explain to a non-technical stakeholder why a prediction was made), the dataset is small (ML overfits with limited data), or you just need to understand patterns rather than build a live prediction system. Use machine learning when: the relationship between inputs and output is complex and non-linear (customer churn has many interacting factors), you have large amounts of data, you need to automate predictions at scale (score every customer daily), or the business question is inherently a prediction or classification task. The analyst's practical test: if a decision tree with 5 rules explains 80% of the variance and a random forest adds 2% more at the cost of a black box, choose the decision tree. In Indian business contexts, simple interpretable models often win over complex models because they can be explained to leadership and are easier to maintain.

What is overfitting and how do you prevent it?

Overfitting occurs when a model learns the training data so precisely that it captures noise rather than the underlying pattern — it memorises rather than generalises. An overfit model has high accuracy on training data but performs poorly on new, unseen data. Signs: training accuracy is much higher than validation accuracy; the model has very complex rules that do not make business sense. Prevention: (1) Train/test split — keep 20-30% of data separate and never use it during training; (2) Cross-validation — evaluate on multiple held-out folds; (3) Regularisation — penalise model complexity (Ridge/Lasso for regression, max_depth for trees); (4) Use more training data if available; (5) Simpler models — start with logistic regression or a shallow decision tree before trying complex ensembles. In Indian business analytics, overfitting is common when analysts build models on a few hundred rows or when they tune the model on the test set. Always evaluate on data the model has never seen.

What evaluation metrics should a data analyst know for classification models?

For classification (predicting a category like churned/not churned): Accuracy: correct predictions / total — misleading for imbalanced data (if 95% of customers do not churn, a model that always predicts "no churn" gets 95% accuracy but is useless). Precision: of all customers predicted to churn, what fraction actually churned? High precision = fewer false alarms. Recall (Sensitivity): of all customers who actually churned, what fraction did the model catch? High recall = fewer missed churns. F1 Score: harmonic mean of precision and recall — balances both. ROC-AUC: measures how well the model separates classes across all probability thresholds. Values above 0.7 are generally useful; above 0.8 is good. In Indian business contexts: for churn prediction, recall matters more (catching churners is more valuable than avoiding false alarms). For fraud detection, precision matters more (flagging too many legitimate transactions annoys customers). Always report the confusion matrix alongside these metrics.

EVIKA ACADEMY · NOIDA SECTOR 51

Learn Predictive Analytics on Real Business Data

Our curriculum covers churn prediction, customer segmentation, and ML model evaluation applied to real Indian fintech and e-commerce datasets — with hands-on Python sklearn labs.

Book Free Demo Class →
🎓 Free Demo Class — Online & Offline · Noida Sector 51