Skip to content

naive_bayes/base.py (_many) methods to narwhals#1931

Merged
MaxHalford merged 20 commits into
online-ml:mainfrom
DagaBhai:nbbase.py
Jun 30, 2026
Merged

naive_bayes/base.py (_many) methods to narwhals#1931
MaxHalford merged 20 commits into
online-ml:mainfrom
DagaBhai:nbbase.py

Conversation

@DagaBhai

@DagaBhai DagaBhai commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

contributing to #1919

as the approach mentioned
steps 1,2,3,4 are done for the methods in the BaseNB (joint_log_likelihood_many, predict_proba_many, pandas .subtract)

AI writing disclosure

We welcome the use of AI tools to help with contributions. For transparency and to help us improve our review process, please indicate the level of AI involvement in this PR.

  • No AI usage: the PR was written entirely by a human.
  • AI-assisted: some parts were suggested or improved by AI, but the PR was written and reviewed by a human.
  • AI-generated: the PR was mostly or fully generated by an AI tool.

@DagaBhai

Copy link
Copy Markdown
Contributor Author

if there are any changes i got it wrong do lemme know and for the testing i have test for the predict_proba_many should i push that too??

@MaxHalford

Copy link
Copy Markdown
Member

Ideally I'd like there to be tests to compare against sklearn. Also, you need to support Narwhals in the learn_many method.

Honestly this is the kind of migration task that a coding agent excels at, I wouldn't shy away from using one!

@DagaBhai

Copy link
Copy Markdown
Contributor Author

Ideally I'd like there to be tests to compare against sklearn. Also, you need to support Narwhals in the learn_many method.

on it

Honestly this is the kind of migration task that a coding agent excels at, I wouldn't shy away from using one!

oh ok got it that would speed things up thank you

@DagaBhai

Copy link
Copy Markdown
Contributor Author

changes made
joint_log_likelihood_many and learn_many in gaussian.py
testing - test_predict_proba_many and test_predict_proba_many_matches_sklearn

DagaBhai added 9 commits June 26, 2026 22:10
Refactor return value to use original input X instead of x_nw.
Remove redundant return statement in Gaussian class.
Add missing newline at end of file.
Removed example usage and unnecessary comments from the GaussianNB class.
Removed print statements for column names in test.
Added examples for using the GaussianNB model with both single and mini-batch learning.
@@ -0,0 +1,70 @@
from __future__ import annotations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be called test_gaussian.py, I would say

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread river/naive_bayes/test_gaussian.py Outdated
assert np.allclose(
river_probs.to_numpy(),
skl_probs.to_numpy(),
atol=1e-2,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a high tolerance. Can you check why the tolerance can't be lower?

@DagaBhai DagaBhai Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.max(np.abs(river_probs.to_numpy() - skl_probs.to_numpy())) using this i got 0.005864465204894698 , 1e-2 is the lowest

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but can you check if this is an algorithmic issue? This can be indicative that the implementation is not exactly correct

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sklearn's GaussianNB uses population variance (ddof=0) because Gaussian Naive Bayes is based on maximum-likelihood estimation (MLE), where population variance is the correct estimator. Our implementation was using the library's default (ddof=1), which is intended for general statistical estimation. Instead of changing the library-wide default, I fixed it only in GaussianNB so it matches the algorithm while leaving the default behavior unchanged.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thank you, that's much better :)

@MaxHalford MaxHalford merged commit 4def49e into online-ml:main Jun 30, 2026
3 checks passed
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.

2 participants