# geminusf/ml_final_project architecture

> Architecture diagram of the GitHub repository geminusf/ml_final_project, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/geminusf/ml_final_project
- Repository: https://github.com/geminusf/ml_final_project

## Overview

#### Purpose and execution

This is a Python 3.12 research repository implementing ensemble-learning and unsupervised algorithms primarily with NumPy, then evaluating them through deterministic experiment suites. The main operational entry point is `src/experiments/run_all.py`; it runs full or quick profiles, writes evidence to `results/`, and produces plots in `figures/`. Data acquisition/verification begins at `download_data.sh`.

#### Core supervised learners

The custom weighted CART tree is the shared weak/base learner: `src/trees/decision_tree.py`. AdaBoost implements multiclass SAMME, staged predictions, early stopping, and SAMME.R support in `src/boosting/adaboost.py`, with a compatibility/export module at `src/trees/adaboost.py`. Random Forest owns bootstrap sampling, feature subsampling, OOB accounting, aligned probabilities, and deterministic parallel/sequential execution: `src/bagging/random_forest.py`, `src/trees/random_forest.py`.

Gradient Boosting is a separate bonus binary-classification implementation in `src/boosting/gradient_boosting.py`.

#### Data and leakage boundary

Dataset registration, source loading, deterministic Covertype subset selection, and provenance checks are centralized in `src/experiments/datasets.py`; the verified snapshot is `data/dataset_audit.json`. Training/test and fold boundaries are enforced before transforms. Leakage-safe numeric/categorical preprocessing, training-only fitting of encoders/scalers, and fold-local oversampling live in `src/utils/preprocessing.py`. Raw downloaded data is intentionally external/Git-ignored.

#### Experiment orchestration

`src/experiments/config.py` defines experiment profiles and parameters; `src/experiments/runner.py` executes the seven experiment families and bonus studies. `src/experiments/run_all.py` coordinates datasets, profiles, artifact generation, and final manifests. The runner drives baseline comparisons, boosting/RF scaling, cross-validated head-to-head tests, label-noise replicates, bootstrap bias-variance analysis, and unsupervised evaluation.

Progress and runtime state are separated from numerical computation in `src/experiments/progress.py`, preserving deterministic seeds and artifacts.

#### Metrics and artifact contract

Classification and bias-variance evaluation are centralized in `src/metrics/evaluation.py`. Result artifacts are stable CSV, JSON, and NPZ files grouped by experiment under `results/`; aggregate reproducibility/runtime metadata is written to `results/manifest.json` and `results/runtime.json`. These files are the boundary between compute stages and report/figure consumers.

#### Unsupervised pipeline

From-scratch centered PCA, K-Means, and DBSCAN reside in `src/unsupervised/pca.py`, `src/unsupervised/kmeans.py`, and `src/unsupervised/dbscan.py`. Experiment execution produces PCA variance/scree outputs, K-Means restart/elbow outputs, DBSCAN k-distance and clustering metrics, and visualization inputs under `results/07_unsupervised/`.

#### Runtime and external dependencies

The material runtime is local Python/NumPy with pinned dependencies in `requirements.txt`. scikit-learn is limited to permitted dataset loaders/reference estimators and utilities such as ARI/t-SNE. Adult data may come from UCI/OpenML and Covertype from UCI/scikit-learn cache; `download_data.sh` supports local fallbacks. Random Forest may use spawned multiprocessing workers, with deterministic sequential fallback when process pipes are unavailable.

Main entry points: [`src/experiments/runner.py`](https://github.com/geminusf/ml_final_project/blob/main/src/experiments/runner.py) (Experiment runner), [`results/manifest.json`](https://github.com/geminusf/ml_final_project/blob/main/results/manifest.json) (Results artifacts), [`src/trees/decision_tree.py`](https://github.com/geminusf/ml_final_project/blob/main/src/trees/decision_tree.py) (Weighted CART), [`src/experiments/datasets.py`](https://github.com/geminusf/ml_final_project/blob/main/src/experiments/datasets.py) (Dataset registry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data boundary"]
  node_download["Data acquisition<br/>shell script<br/>[download_data.sh]"]
  node_audit["Dataset audit<br/>verified snapshot<br/>[dataset_audit.json]"]
  node_datasets["Dataset registry<br/>data loading<br/>[datasets.py]"]
  node_preprocessing["Leakage-safe preprocessing<br/>transform pipeline<br/>[preprocessing.py]"]
end

subgraph group_learners["Supervised learners"]
  node_tree["Weighted CART<br/>base learner<br/>[decision_tree.py]"]
  node_adaboost["AdaBoost SAMME<br/>boosting learner<br/>[adaboost.py]"]
  node_adaboost_compat["AdaBoost compatibility export<br/>compatibility module<br/>[adaboost.py]"]
  node_random_forest["Random Forest<br/>bagging learner<br/>[random_forest.py]"]
  node_rf_compat["RF compatibility export<br/>compatibility module<br/>[random_forest.py]"]
  node_gradient_boosting["Gradient Boosting<br/>bonus learner"]
end

subgraph group_experiments["Experiment runtime"]
  node_config["Experiment profiles<br/>configuration code<br/>[config.py]"]
  node_run_all["Run all<br/>entry point<br/>[run_all.py]"]
  node_runner["Experiment runner<br/>orchestrator<br/>[runner.py]"]
  node_progress["Progress state<br/>runtime state<br/>[progress.py]"]
  node_metrics["Evaluation metrics<br/>[evaluation.py]"]
  node_results[("Results artifacts<br/>artifact store<br/>[manifest.json]")]
  node_manifest["Run manifests<br/>reproducibility metadata<br/>[runtime.json]"]
  node_figures["Figures<br/>plot output"]
end

subgraph group_unsupervised["Unsupervised pipeline"]
  node_pca["Centered PCA<br/>unsupervised algorithm<br/>[pca.py]"]
  node_kmeans["K-Means<br/>unsupervised algorithm<br/>[kmeans.py]"]
  node_dbscan["DBSCAN<br/>unsupervised algorithm<br/>[dbscan.py]"]
end

node_download -->|"acquires sources"| node_datasets
node_audit -->|"verifies provenance"| node_datasets
node_datasets -->|"loads registered data"| node_run_all
node_config -->|"selects profile"| node_run_all
node_run_all -->|"launches suites"| node_runner
node_runner -->|"creates train/fold transforms"| node_preprocessing
node_preprocessing -->|"prepared training data"| node_tree
node_tree -->|"weak learner"| node_adaboost
node_tree -->|"tree estimator"| node_random_forest
node_adaboost_compat -.->|"exports"| node_adaboost
node_rf_compat -.->|"exports"| node_random_forest
node_runner -->|"runs bonus study"| node_gradient_boosting
node_runner -->|"runs scaling and comparisons"| node_adaboost
node_runner -->|"runs scaling and comparisons"| node_random_forest
node_runner -->|"runs unsupervised evaluation"| node_pca
node_runner -->|"runs restarts and elbow"| node_kmeans
node_runner -->|"runs k-distance and clustering"| node_dbscan
node_runner -->|"evaluates predictions"| node_metrics
node_runner -.->|"reports runtime state"| node_progress
node_metrics -->|"writes experiment evidence"| node_results
node_runner -->|"writes outputs"| node_results
node_results -->|"aggregates metadata"| node_manifest
node_results -->|"supplies plot inputs"| node_figures

click node_download "https://github.com/geminusf/ml_final_project/blob/main/download_data.sh"
click node_audit "https://github.com/geminusf/ml_final_project/blob/main/data/dataset_audit.json"
click node_datasets "https://github.com/geminusf/ml_final_project/blob/main/src/experiments/datasets.py"
click node_preprocessing "https://github.com/geminusf/ml_final_project/blob/main/src/utils/preprocessing.py"
click node_tree "https://github.com/geminusf/ml_final_project/blob/main/src/trees/decision_tree.py"
click node_adaboost "https://github.com/geminusf/ml_final_project/blob/main/src/boosting/adaboost.py"
click node_adaboost_compat "https://github.com/geminusf/ml_final_project/blob/main/src/trees/adaboost.py"
click node_random_forest "https://github.com/geminusf/ml_final_project/blob/main/src/bagging/random_forest.py"
click node_rf_compat "https://github.com/geminusf/ml_final_project/blob/main/src/trees/random_forest.py"
click node_gradient_boosting "https://github.com/geminusf/ml_final_project/blob/main/src/boosting/gradient_boosting.py"
click node_config "https://github.com/geminusf/ml_final_project/blob/main/src/experiments/config.py"
click node_run_all "https://github.com/geminusf/ml_final_project/blob/main/src/experiments/run_all.py"
click node_runner "https://github.com/geminusf/ml_final_project/blob/main/src/experiments/runner.py"
click node_progress "https://github.com/geminusf/ml_final_project/blob/main/src/experiments/progress.py"
click node_metrics "https://github.com/geminusf/ml_final_project/blob/main/src/metrics/evaluation.py"
click node_results "https://github.com/geminusf/ml_final_project/blob/main/results/manifest.json"
click node_manifest "https://github.com/geminusf/ml_final_project/blob/main/results/runtime.json"
click node_figures "https://github.com/geminusf/ml_final_project/tree/main/figures/07_unsupervised"
click node_pca "https://github.com/geminusf/ml_final_project/blob/main/src/unsupervised/pca.py"
click node_kmeans "https://github.com/geminusf/ml_final_project/blob/main/src/unsupervised/kmeans.py"
click node_dbscan "https://github.com/geminusf/ml_final_project/blob/main/src/unsupervised/dbscan.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_download,node_audit,node_datasets,node_preprocessing toneBlue
class node_tree,node_adaboost,node_adaboost_compat,node_random_forest,node_rf_compat,node_gradient_boosting toneAmber
class node_config,node_run_all,node_runner,node_progress,node_metrics,node_results,node_manifest,node_figures toneMint
class node_pca,node_kmeans,node_dbscan toneRose
```

## Components

### Data boundary

Verified data and leakage-safe preparation.

- [Data acquisition](https://github.com/geminusf/ml_final_project/blob/main/download_data.sh): shell script · Downloads data or uses local fallbacks. · `download_data.sh`
- [Dataset audit](https://github.com/geminusf/ml_final_project/blob/main/data/dataset_audit.json): verified snapshot · Stores dataset provenance verification. · `data/dataset_audit.json`
- [Dataset registry](https://github.com/geminusf/ml_final_project/blob/main/src/experiments/datasets.py): data loading · Registers sources and selects deterministic subsets. · `src/experiments/datasets.py`
- [Leakage-safe preprocessing](https://github.com/geminusf/ml_final_project/blob/main/src/utils/preprocessing.py): transform pipeline · Fits transforms and oversampling only on training folds. · `src/utils/preprocessing.py`

### Supervised learners

Custom NumPy classification implementations.

- [Weighted CART](https://github.com/geminusf/ml_final_project/blob/main/src/trees/decision_tree.py): base learner · Shared weighted decision-tree weak learner. · `src/trees/decision_tree.py`
- [AdaBoost SAMME](https://github.com/geminusf/ml_final_project/blob/main/src/boosting/adaboost.py): boosting learner · Supports multiclass staging, early stopping, and SAMME.R. · `src/boosting/adaboost.py`
- [AdaBoost compatibility export](https://github.com/geminusf/ml_final_project/blob/main/src/trees/adaboost.py): compatibility module · `src/trees/adaboost.py`
- [Random Forest](https://github.com/geminusf/ml_final_project/blob/main/src/bagging/random_forest.py): bagging learner · Bootstraps, subsamples features, tracks OOB, and supports deterministic parallelism. · `src/bagging/random_forest.py`
- [RF compatibility export](https://github.com/geminusf/ml_final_project/blob/main/src/trees/random_forest.py): compatibility module · `src/trees/random_forest.py`
- [Gradient Boosting](https://github.com/geminusf/ml_final_project/blob/main/src/boosting/gradient_boosting.py): bonus learner · Separate binary-classification implementation. · `src/boosting/gradient_boosting.py`

### Experiment runtime

Deterministic experiment execution and artifacts.

- [Experiment profiles](https://github.com/geminusf/ml_final_project/blob/main/src/experiments/config.py): configuration code · Defines full and quick profiles and parameters. · `src/experiments/config.py`
- [Run all](https://github.com/geminusf/ml_final_project/blob/main/src/experiments/run_all.py): entry point · Coordinates datasets, profiles, artifacts, and manifests. · `src/experiments/run_all.py`
- [Experiment runner](https://github.com/geminusf/ml_final_project/blob/main/src/experiments/runner.py): orchestrator · Executes seven experiment families and bonus studies. · `src/experiments/runner.py`
- [Progress state](https://github.com/geminusf/ml_final_project/blob/main/src/experiments/progress.py): runtime state · Separates progress reporting from deterministic numerical work. · `src/experiments/progress.py`
- [Evaluation metrics](https://github.com/geminusf/ml_final_project/blob/main/src/metrics/evaluation.py): evaluation · Centralizes classification and bias-variance evaluation. · `src/metrics/evaluation.py`
- [Results artifacts](https://github.com/geminusf/ml_final_project/blob/main/results/manifest.json): artifact store · Stable CSV, JSON, and NPZ outputs by experiment. · `results/manifest.json`
- [Run manifests](https://github.com/geminusf/ml_final_project/blob/main/results/runtime.json): reproducibility metadata · Aggregate manifest and runtime metadata. · `results/runtime.json`
- [Figures](https://github.com/geminusf/ml_final_project/tree/main/figures/07_unsupervised): plot output · `figures/07_unsupervised`

### Unsupervised pipeline

From-scratch clustering and dimensionality reduction.

- [Centered PCA](https://github.com/geminusf/ml_final_project/blob/main/src/unsupervised/pca.py): unsupervised algorithm · `src/unsupervised/pca.py`
- [K-Means](https://github.com/geminusf/ml_final_project/blob/main/src/unsupervised/kmeans.py): unsupervised algorithm · `src/unsupervised/kmeans.py`
- [DBSCAN](https://github.com/geminusf/ml_final_project/blob/main/src/unsupervised/dbscan.py): unsupervised algorithm · `src/unsupervised/dbscan.py`

## Connections

- Data acquisition → Dataset registry: acquires sources
- Dataset audit → Dataset registry: verifies provenance
- Dataset registry → Run all: loads registered data
- Experiment profiles → Run all: selects profile
- Run all → Experiment runner: launches suites
- Experiment runner → Leakage-safe preprocessing: creates train/fold transforms
- Leakage-safe preprocessing → Weighted CART: prepared training data
- Weighted CART → AdaBoost SAMME: weak learner
- Weighted CART → Random Forest: tree estimator
- AdaBoost compatibility export → AdaBoost SAMME: exports
- RF compatibility export → Random Forest: exports
- Experiment runner → Gradient Boosting: runs bonus study
- Experiment runner → AdaBoost SAMME: runs scaling and comparisons
- Experiment runner → Random Forest: runs scaling and comparisons
- Experiment runner → Centered PCA: runs unsupervised evaluation
- Experiment runner → K-Means: runs restarts and elbow
- Experiment runner → DBSCAN: runs k-distance and clustering
- Experiment runner → Evaluation metrics: evaluates predictions
- Experiment runner → Progress state: reports runtime state
- Evaluation metrics → Results artifacts: writes experiment evidence
- Experiment runner → Results artifacts: writes outputs
- Results artifacts → Run manifests: aggregates metadata
- Results artifacts → Figures: supplies plot inputs
