Statistics and Machine Learning Toolbox: Worked examples

Complete step-by-step walkthroughs of the five included machine learning models with domain contexts, input tables, mathematical formulations, and verified results.

← Statistics and Machine Learning Toolbox overview · Getting started · Modeling guide · Methods guide · Reference

Why the workbook includes multiple examples

The Statistics and Machine Learning Toolbox includes five distinct model fixtures. Rather than demonstrating only a single algorithm, these fixtures exercise binary classification, regularized continuous regression, centroid-based unsupervised clustering, decision tree rule partitioning, and spectral dimensionality reduction within the exact same spreadsheet geometry.

The Marimo reactive engine reads the shared worksheet tables, identifies the active dataset via Start!B6, fits the appropriate scikit-learn estimator, runs stratified cross-validation, and publishes normalized results.


Example matrix

Model ID Domain & Problem Task Family Algorithm Primary Metric Top Feature (Rank 1) Diagnostic Notes
customer_churn_logistic Telecom subscriber churn Classification LogisticRegression Accuracy: 1.000000 Feature3 (w=1.135471) 7 checks PASS
housing_regression_ridge Residential property appraisal Regression Ridge \(R^2\): 0.986518 Feature1 (w=60.173745) 7 checks PASS
iris_clustering_kmeans Botanical flower segmentation Clustering KMeans Silhouette: 0.540379 Feature2 (w=0.317303) 7 checks PASS
credit_default_tree Retail lending default underwriting Classification DecisionTreeClassifier Accuracy: 1.000000 Feature4 (w=1.000000) 6 PASS, 1 FAIL (Generalization gap)
feature_pca Correlated signal reduction Decomposition PCA ExpVar: 1.000000 Feature1 (w=0.500750) 6 PASS, 1 FAIL (Condition number \(\kappa\))

1. Customer Churn Prediction (Logistic Regression)

Business context

A subscription telecom provider seeks to identify customers at high risk of canceling their contracts. Identifying churn drivers allows retention teams to proactively offer targeted promotional incentives before cancellation occurs.

Input dataset (Data!A5:G21)

The dataset contains 16 customer accounts:

SampleID Feature1 (Usage Min) Feature2 (Tenure Mo) Feature3 (Complaints) Feature4 (MonthlySpend $) Target (Churn)
1 120.0 24.0 0.0 65.0 0
2 350.0 6.0 2.0 85.0 1
3 210.0 36.0 1.0 55.0 0
4 80.0 4.0 3.0 95.0 1
5 450.0 48.0 0.0 45.0 0
6 190.0 12.0 1.0 75.0 0
7 95.0 3.0 4.0 110.0 1
8 300.0 60.0 0.0 40.0 0
9 140.0 8.0 2.0 90.0 1
10 280.0 18.0 0.0 70.0 0
11 410.0 30.0 0.0 50.0 0
12 115.0 5.0 3.0 105.0 1
13 260.0 40.0 1.0 60.0 0
14 175.0 10.0 2.0 80.0 1
15 320.0 22.0 0.0 65.0 0
16 90.0 2.0 4.0 115.0 1

Mathematical formulation

\[\min_{\mathbf{w}, b} \; \frac{1}{2} \|\mathbf{w}\|_2^2 + C \sum_{i=1}^{16} \mathcal{L}_{\text{BCE}}(y_i, \sigma(\mathbf{w}^T \mathbf{x}_i + b))\]

where \(C = 1.0\), features are standardized (\(z = (x - \mu)/\sigma\)), and optimization uses L-BFGS.

Verified baseline results

  • Primary metric (Accuracy): 1.000000 (100.0%)
  • Secondary metric (F1 score): 1.000000
  • Tertiary metric (ROC-AUC): 1.000000
  • Cross-validation mean (3-fold): 0.944444 (94.4%)
  • Feature importance rankings:
    • Feature3 (Complaints): Weight = 1.135471, Odds Ratio = 3.1127, Rank 1
    • Feature4 (MonthlySpend): Weight = 0.874974, Odds Ratio = 2.3988, Rank 2
    • Feature2 (Tenure): Weight = -0.801402, Odds Ratio = 0.4487, Rank 3
    • Feature1 (Usage): Weight = -0.174595, Odds Ratio = 0.8398, Rank 4
  • Sample predictions:
    • Sample 1: Actual = 0, Predicted = 0, Probability = 0.1176, Split = Train, Correct = YES
    • Sample 2: Actual = 1, Predicted = 1, Probability = 0.9632, Split = Train, Correct = YES
  • Diagnostics: All 7 checks report PASS.

2. Property Valuation (Ridge Regression)

Business context

A real estate appraisal desk needs a reliable, regularized model to estimate residential market prices from physical and location attributes, avoiding the erratic coefficients often produced by ordinary least squares under correlated housing features.

Input dataset (Data!A22:G33)

The dataset contains 12 residential properties:

SampleID Feature1 (Sqft) Feature2 (Bedrooms) Feature3 (Age Yrs) Feature4 (Dist CBD Mi) Target (Price $k)
1 1400.0 3.0 15.0 5.0 320.0
2 1800.0 3.0 10.0 3.5 395.0
3 2200.0 4.0 5.0 2.0 490.0
4 1200.0 2.0 25.0 8.0 260.0
5 2600.0 4.0 2.0 1.5 580.0
6 1600.0 3.0 18.0 4.0 350.0
7 1950.0 3.0 8.0 3.0 425.0
8 2400.0 4.0 4.0 2.5 520.0
9 1100.0 2.0 30.0 9.0 240.0
10 1700.0 3.0 12.0 4.5 375.0
11 2800.0 5.0 1.0 1.0 640.0
12 1500.0 3.0 20.0 6.0 330.0

Mathematical formulation

\[\min_{\mathbf{w}} \; \frac{1}{24} \|\mathbf{y} - \mathbf{X}\mathbf{w}\|_2^2 + \frac{\alpha}{2} \|\mathbf{w}\|_2^2\]

with \(\alpha = 1.0\) configured on Model Config!C7.

Verified baseline results

  • Primary metric (\(R^2\)): 0.986518 (98.65% variance explained)
  • Secondary metric (RMSE): 14.398038 ($14,398 prediction error)
  • Tertiary metric (MAE): 10.988185 ($10,988 mean absolute error)
  • Cross-validation mean (3-fold): 0.953845
  • Feature coefficients:
    • Feature1 (Sqft): Weight = 60.173745, AbsImportance = 60.173745, Rank 1
    • Feature2 (Bedrooms): Weight = 31.544822, AbsImportance = 31.544822, Rank 2
    • Feature3 (Age): Weight = -17.500994, AbsImportance = 17.500994, Rank 3
    • Feature4 (Distance): Weight = -8.434350, AbsImportance = 8.434350, Rank 4
  • Sample predictions (Holdout Sample 1):
    • Actual Price: $320.0k
    • Predicted Price: $340.92k
    • Residual (Predictions!Probability): -$20.92k
    • Split: Test
    • Correct tag: Residual
  • Diagnostics: All 7 checks report PASS. Residual normality via Shapiro-Wilk yields \(p = 0.5843 \ge 0.05\).

3. Botanical Morphological Clustering (Iris K-Means)

Business context

A botanical institute wishes to segment unlabeled flower specimens into natural morphological taxonomic groups based solely on petal and sepal dimensions, evaluating cluster coherence without relying on species tags.

Input dataset (Data!A34:G45)

The dataset contains 12 flower observations across 4 physical measurements:

SampleID Feature1 (SepalLen) Feature2 (SepalWid) Feature3 (PetalLen) Feature4 (PetalWid) Target (ClusterLabel)
1 5.1 3.5 1.4 0.2 0
2 4.9 3.0 1.4 0.2 0
3 4.7 3.2 1.3 0.2 0
4 4.6 3.1 1.5 0.2 0
5 7.0 3.2 4.7 1.4 1
6 6.4 3.2 4.5 1.5 1
7 6.9 3.1 4.9 1.5 1
8 5.5 2.3 4.0 1.3 1
9 6.3 3.3 6.0 2.5 2
10 5.8 2.7 5.1 1.9 2
11 7.1 3.0 5.9 2.1 2
12 6.3 2.9 5.6 1.8 2

Mathematical formulation

\[\min_{\mathcal{S}, \{\boldsymbol{\mu}_k\}_{k=1}^3} \; \sum_{k=1}^3 \sum_{\mathbf{x}_i \in S_k} \|\mathbf{x}_i - \boldsymbol{\mu}_k\|_2^2\]

with \(k = 3\) centroids initialized via k-means++.

Verified baseline results

  • Primary metric (Silhouette Score): 0.540379 (strong cluster separation)
  • Secondary metric (Inertia): 11.238210
  • Tertiary metric (Calinski-Harabasz): 14.868735
  • CV score mean: 0.540379
  • Feature variance shares:
    • Feature2 (SepalWid): Weight = 0.317303, Rank 1
    • Feature3 (PetalLen): Weight = 0.242379, Rank 2
    • Feature4 (PetalWid): Weight = 0.228701, Rank 3
    • Feature1 (SepalLen): Weight = 0.211617, Rank 4
  • Sample predictions:
    • Sample 1: Actual = 0, Predicted Cluster = 1 (or assigned cluster index), Centroid Distance = 1.0623, Split = Test, Correct = ClusterAssigned
  • Diagnostics: All 7 checks report PASS. Minimum cluster size is 4 samples (\(\ge 2\)).

4. Credit Risk Classification (Decision Tree)

Business context

A credit committee requires an interpretable rule-based underwriting model to classify consumer loan default risk. Decision trees offer clear boolean split criteria that can be directly audited by compliance regulators.

Input dataset (Data!A46:G53)

The dataset contains 8 applicant loan profiles:

SampleID Feature1 (Income $k) Feature2 (DebtRatio) Feature3 (LatePayments) Feature4 (CreditScore) Target (Default)
1 45.0 0.35 0.0 720.0 0
2 85.0 0.22 0.0 780.0 0
3 28.0 0.55 2.0 610.0 1
4 35.0 0.42 1.0 660.0 0
5 18.0 0.70 3.0 580.0 1
6 95.0 0.18 0.0 810.0 0
7 52.0 0.48 1.0 640.0 1
8 65.0 0.30 0.0 750.0 0

Mathematical formulation

Recursive binary partitioning minimizing Gini impurity:

\[I_G(t) = 1 - \sum_{c \in \{0, 1\}} p(c \mid t)^2\]

with max_depth = 3.

Verified baseline results

  • Primary metric (Accuracy): 1.000000 (100.0%)
  • Secondary metric (F1 score): 1.000000
  • Tertiary metric (ROC-AUC): 1.000000
  • Cross-validation mean: 0.722222 (72.2%)
  • Feature Gini importances:
    • Feature4 (CreditScore): Importance = 1.000000, Rank 1
    • Feature1, Feature2, Feature3: Importance = 0.000000, Rank 2–4
  • Diagnostics:
    • 6 checks report PASS.
    • Check 4 (Generalization & stability): Reports FAIL.
      • Diagnostic Evidence: Generalization gap \(|1.0000 - 0.7222| = 0.2778 > 0.25\).
      • Interpretation: With only 8 samples, a tree of depth 3 isolates individual records, creating a substantial gap between training fit and out-of-fold generalization.
      • Remediation: Setting max_depth = 1 on Model Config!C9 restricts the tree to a single decision stump on CreditScore, preserving high predictive power while stabilizing the generalization gap.

5. Multi-Feature Dimensionality Reduction (PCA)

Business context

An industrial engineering group collects multichannel sensor readings (Dim1 through Dim4) from manufacturing telemetry. The signals exhibit severe collinearity. The team uses PCA to extract orthogonal principal components that summarize process variance without redundancy.

Input dataset (Data!A54:G59)

The dataset contains 6 sensor measurements:

SampleID Feature1 (Dim1) Feature2 (Dim2) Feature3 (Dim3) Feature4 (Dim4) Target
1 10.0 20.0 30.0 40.0 0
2 12.0 22.0 28.0 38.0 0
3 9.0 19.0 31.0 42.0 0
4 15.0 25.0 25.0 35.0 0
5 8.0 18.0 32.0 44.0 0
6 14.0 24.0 26.0 36.0 0

Mathematical formulation

\[\mathbf{X}_c = \mathbf{U} \mathbf{S} \mathbf{V}^T, \quad \mathbf{Z} = \mathbf{X}_c \mathbf{V}_2\]

with n_components = 2 on Model Config!C10.

Verified baseline results

  • Primary metric (Total Explained Variance): 1.000000 (100.0% of all sample variance captured)
  • Secondary metric (PC1 Explained Variance Ratio): 0.999607 (99.96%)
  • Tertiary metric (PC2 Explained Variance Ratio): 0.000393 (0.04%)
  • Cross-validation mean: 1.000000
  • Feature loadings (PC1):
    • Feature1: Loading = 0.500750, Rank 1
    • Feature2: Loading = 0.500750, Rank 2
    • Feature3: Loading = -0.500750, Rank 3
    • Feature4: Loading = -0.497745, Rank 4
  • Diagnostics:
    • 6 checks report PASS.
    • Check 5 (Multicollinearity & conditioning): Reports FAIL.
      • Diagnostic Evidence: Condition number \(\kappa = 431.13 \ge 100\).
      • Interpretation: The sensor features are almost perfectly collinear. This diagnostic failure directly verifies the necessity of applying PCA to compress the collinear inputs into orthogonal latent factors.