Skip to content

Optimize GaussianNB predict and predict_proba hot loops - #317

Merged
godofecht merged 2 commits into
mainfrom
fix/212-220-gaussian-nb-inference
Aug 17, 2026
Merged

godofecht merged 2 commits into
mainfrom
fix/212-220-gaussian-nb-inference

Conversation

@godofecht

@godofecht godofecht commented Aug 17, 2026 •

Copy link
Copy Markdown
Owner

Progresses #212 and #220.

The inference path previously recomputed log(prior), the Gaussian normalization constant, and log(variance) inside every sample × class × feature iteration. predict_proba also allocated a fresh class-score buffer for every sample.

This change:

  • precomputes class log-priors, per-feature log-normalization terms and inverse twice-variance terms once per prediction call;
  • uses direct row-major Matrix.data access in the Gaussian hot loop;
  • reuses one probability scratch buffer across all rows;
  • writes probability results directly to the row-major output matrix;
  • leaves fitted model state and GaussianNB.fit latency unchanged.

A dedicated regression test verifies class decisions, posterior normalization, the symmetric 0.5/0.5 midpoint posterior, and probability ordering.

Measured scaled evidence for GaussianNB.predict versus the exact pre-inference base shows 1.74×–4.22× speedups across 100/1k/10k × 8/32-feature cases, with the self-baseline gate passing. This evidence satisfies #212. The existing scaled benchmark does not time predict_proba, so #220 remains open until dedicated probability timing is added/recorded.

The branch is now rebased directly onto the merged #210 main commit; fresh checks are running.

@godofecht
godofecht force-pushed the fix/212-220-gaussian-nb-inference branch from e47b836 to b10cf22 Compare August 17, 2026 10:06
@godofecht
godofecht changed the base branch from fix/210-gaussian-nb-fit to main August 17, 2026 10:06
@godofecht
godofecht marked this pull request as ready for review August 17, 2026 10:24
@godofecht
godofecht merged commit f09a54e into main Aug 17, 2026
@godofecht
godofecht deleted the fix/212-220-gaussian-nb-inference branch August 19, 2026 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant