You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add class_weight support to Naive Bayes models (GaussianNB, MultinomialNB, BernoulliNB) #32763
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
fromsklearn.naive_bayesimportGaussianNBfromsklearn.utils.class_weightimportcompute_sample_weightsample_weight=compute_sample_weight('balanced', y)
gnb=GaussianNB()
gnb.fit(X, y, sample_weight=sample_weight)
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.
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.
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.
@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.
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
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.
Reacted by Aarav Maheshwari and sarthakpurii-apnamart
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?
@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.
@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.
@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.
@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.
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.
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.
Activity
glevv commentedon Nov 23, 2025
I think you can get the same result by changing
priorsparameter, because it is by definition prior class weightAaraviitkgp commentedon Nov 23, 2025
@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.
glevv commentedon Nov 23, 2025
AI-generated comments are in clear violation of the Code of Conduct of scikit-learn.
Aaraviitkgp commentedon Nov 23, 2025
@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.
StefanieSenger commentedon Nov 23, 2025
Closing as AI spam.
StefanieSenger commentedon Nov 23, 2025
Change "close as completed" to "close as not planned" for the stats.
adrinjalali commentedon Nov 24, 2025
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
StefanieSenger commentedon Nov 24, 2025
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.
DhmhtrhsPakakis commentedon Nov 24, 2025
Hello.I am new at open-source projects and i would like to contribute to this issue.
evantakahashi commentedon Nov 24, 2025
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?
DhmhtrhsPakakis commentedon Nov 24, 2025
@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.
evantakahashi commentedon Nov 24, 2025
@DhmhtrhsPakakis Actually I think that @Aaraviitkgp has submitted a pr already
Aaraviitkgp commentedon Nov 24, 2025
@DhmhtrhsPakakis @evantakahashi I have already made a pr on this issue, you can look into other issues.
lesteve commentedon Nov 25, 2025
@Aaraviitkgp can you explain the motivations behind the issue?
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.
Aaraviitkgp commentedon Nov 25, 2025
@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.
Aaraviitkgp commentedon Nov 25, 2025
@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.
lesteve commentedon Nov 25, 2025
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.
dev121-python commentedon Jan 24, 2026
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.
Dhruvi-kanabar30 commentedon Sep 23, 2026
I would like to work on this issue please assign it to me