Skip to content

Add class_weight support to Naive Bayes models (GaussianNB, MultinomialNB, BernoulliNB) #32763

Open

Description

@Aaraviitkgp

Describe the workflow you want to enable

Naive Bayes classifiers (GaussianNB, MultinomialNB, BernoulliNB, ComplementNB, CategoricalNB) currently do not support the class_weight parameter, while almost all other scikit-learn classifiers do. This creates API inconsistency and requires users to manually compute and pass sample_weight for imbalanced datasets.

Current Workaround

from sklearn.naive_bayes import GaussianNB
from sklearn.utils.class_weight import compute_sample_weight
sample_weight = compute_sample_weight('balanced', y)
gnb = GaussianNB()
gnb.fit(X, y, sample_weight=sample_weight)

Proposed Enhancement

gnb = GaussianNB(class_weight='balanced')
gnb.fit(X, y)

Describe your proposed solution

Add a class_weight parameter to all Naive Bayes classifiers that internally converts class weights to sample weights before calling the existing fit() logic. This leverages the fact that all NB classifiers already support sample_weight in their fitting methods.

class GaussianNB(_BaseNB):
    _parameter_constraints: dict = {
        "priors": ["array-like", None],
        "var_smoothing": [Interval(Real, 0, None, closed="left")],
        "class_weight": ["dict", "balanced", None],  # NEW
    }

    def __init__(self, *, priors=None, var_smoothing=1e-9, class_weight=None):
        self.priors = priors
        self.var_smoothing = var_smoothing
-->  self.class_weight = class_weight 

Describe alternatives you've considered, if relevant

No response

Additional context

This is a improvement for API consistency. While the workaround exists, having uniform behavior across classifiers reduces confusion and improves discoverability, especially for beginners.

Activity

  1. glevv commented on Nov 23, 2025

    @glevv
    Contributor

    I think you can get the same result by changing priors parameter, because it is by definition prior class weight

  2. Aaraviitkgp commented on Nov 23, 2025

    @Aaraviitkgp
    Author

    @glevv

    You're right that priors and class_weight have overlapping functionality for reweighting classes. However, there are important differences:

    User Intent: priors represents domain knowledge about true class distribution, while class_weight represents a training procedure choice for handling imbalance. Conceptually distinct.

    API Consistency: As I said in the feature request all other sklearn classifiers use class_weight except naive bayes models which can be confusing.

    Automatic Balancing: class_weight='balanced' automatically computes appropriate weights, while manually setting priors requires the user to calculate the right values.

    Interaction with sample_weight: class_weight naturally combines with sample_weight (multiplicative), giving users flexible control.

    Parameter Estimation: While subtle, class_weight affects likelihood parameter estimation (weighted means/variances), not just the prior

    That said, you're correct that for many practical cases, carefully chosen priors can achieve similar decision boundaries.But it can be extremely tedious for a beginner to do so.

  3. glevv commented on Nov 23, 2025

    @glevv
    Contributor

    AI-generated comments are in clear violation of the Code of Conduct of scikit-learn.

  4. Aaraviitkgp commented on Nov 23, 2025

    @Aaraviitkgp
    Author

    @glevv I am not a native english speaker, I used AI to explain my point, I am sorry about that if it is violating any code of conduct of scikit-learn repo. I am a active contributor in tensorflow repo and just wanted to contribute to scikit learn. Once again I am really sorry if any code of conduct was violated.

  5. StefanieSenger commented on Nov 23, 2025

    @StefanieSenger
    Member

    Closing as AI spam.

  6. added
    spam
    on Nov 23, 2025
  7. StefanieSenger commented on Nov 23, 2025

    @StefanieSenger
    Member

    Change "close as completed" to "close as not planned" for the stats.

  8. adrinjalali commented on Nov 24, 2025

    @adrinjalali
    Member

    We've re-evaluated this with @StefanieSenger , and concluded this as not a "spam".

    We're somewhat flooded with AI junk these days, and that's why sometimes posts get flagged as false positive.

    Your issue is valid, and might be easy to implement (it's not always easy).

    We can't promise a high priority review if you open a PR, but having the PR would help assessing the complexity of the implementation. So your contribution here is very much welcome @Aaraviitkgp

  9. removed
    spam
    Needs Triage
    on Nov 24, 2025
  10. StefanieSenger commented on Nov 24, 2025

    @StefanieSenger
    Member

    I'm very sorry @Aaraviitkgp, I had wrongly assumed your initial issue was purely AI generated (since we had had similar "add class_weight" issues in the past). Your contribution is very welcome here.

  11. DhmhtrhsPakakis commented on Nov 24, 2025

    @DhmhtrhsPakakis

    Hello.I am new at open-source projects and i would like to contribute to this issue.

  12. evantakahashi commented on Nov 24, 2025

    @evantakahashi

    I am also new to open source projects and would also like to contribute to this issue. Maybe we could work on this together @DhmhtrhsPakakis. I have started to work on this already, and we could communicate possibly?

  13. DhmhtrhsPakakis commented on Nov 24, 2025

    @DhmhtrhsPakakis

    @evantakahashi the Naive Bayes classifiers (GaussianNB, MultinomialNB, BernoulliNB, ComplementNB, CategoricalNB) are 5 , maybe i could add the class_weight to 2 or 3 of them and you to the others. In Addition i have found what needs to be changed and i have left the implementation, so if you need help at the idea i could help you.

  14. evantakahashi commented on Nov 24, 2025

    @evantakahashi

    @DhmhtrhsPakakis Actually I think that @Aaraviitkgp has submitted a pr already

  15. Aaraviitkgp commented on Nov 24, 2025

    @Aaraviitkgp
    Author

    @DhmhtrhsPakakis @evantakahashi I have already made a pr on this issue, you can look into other issues.

  16. lesteve commented on Nov 25, 2025

    @lesteve
    Member

    @Aaraviitkgp can you explain the motivations behind the issue?

    • do you have a real application use case behind this or is it more of a API consistency thing that you noticed?
    • out of curiosity, how did you bump into this limitation?
    • is it more than your main motivation is to contribute to scikit-learn? If that's the case, I would suggest you have a look at our contributing doc and in particular the section Issues for new contributors.

    As far as I am concerned I would guess that Naive Bayes models are not used that much in practice, happy to hear opinions from other maintainers about this!

    To set expectations, if Naive Bayes models are not used that much, it's unlikely to be at the top of a maintainer's priority list. This means it's unlikely that a PR addressing this issue will get reviewed, and even less merged.

  17. Aaraviitkgp commented on Nov 25, 2025

    @Aaraviitkgp
    Author

    @lesteve It was more of an api consistency thing I noticed when I started using scikit, it was confusing as all other classification algorithms have it.

  18. Aaraviitkgp commented on Nov 25, 2025

    @Aaraviitkgp
    Author

    @lesteve Well I am a student so I was experimenting on different models for spam detection project, thats when I stumbled on this inconsistency, as for any practical usecase I personally dont think there would be any major usecase.

  19. lesteve commented on Nov 25, 2025

    @lesteve
    Member

    OK so trying to set expectation here, I would wait for a maintainer to explicitly say something like "I think this is important and I am willing to review your PR to address this issue".

    As I said personally, I think this doesn't feel like something that it important enough but other maintainers may have different opinions.

    You are more than welcome to keep working on #32783 as some kind of training to be more familiar with the contributing workflow, but be aware that your PR may never get a review.

  20. dev121-python commented on Jan 24, 2026

    @dev121-python

    Hi, thanks for opening this issue - the API inconsistency makes sense.

    I looked into naive_bayes.py, specifically BaseNB.fit, and it seems all NB classifiers already support sample_weight adding a class_weight parameter and internally converting it to sample_weight (using compute_sample_weight) at the start of fit would align well with existing sklearn patterns used in other classifiers

    If this approach sounds reasonable I’d be happy to work on a PR and add the corresponding tests.

  21. Dhruvi-kanabar30 commented on Sep 23, 2026

    @Dhruvi-kanabar30

    I would like to work on this issue please assign it to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions