Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add class_weight parameter to Naive Bayes classifiers - #32769

Closed
U-C4N wants to merge 1 commit into
scikit-learn:mainfrom
U-C4N:main
Closed

Add class_weight parameter to Naive Bayes classifiers#32769
U-C4N wants to merge 1 commit into
scikit-learn:mainfrom
U-C4N:main

Conversation

@U-C4N

@U-C4N U-C4N commented Nov 23, 2025

Copy link
Copy Markdown

Changes

  • Added class_weight parameter accepting None, 'balanced', or a dict
  • Converts class weights to sample weights internally using compute_sample_weight()
  • Multiplies with user-provided sample_weight if both are specified
  • Raises informative error when using class_weight='balanced' with partial_fit
  • Updated docstrings for all 5 classifiers
  • Added comprehensive tests

Reference Issues/PRs

What does this implement/fix? Explain your changes.

Any other comments?

 ## Changes

  - Added `class_weight` parameter accepting `None`, `'balanced'`, or a dict
  - Converts class weights to sample weights internally using `compute_sample_weight()`
  - Multiplies with user-provided `sample_weight` if both are specified
  - Raises informative error when using `class_weight='balanced'` with `partial_fit`
  - Updated docstrings for all 5 classifiers
  - Added comprehensive tests
@github-actions

Copy link
Copy Markdown

❌ Linting issues

This PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling pre-commit hooks. Instructions to enable them can be found here.

You can see the details of the linting issues under the lint job here


ruff check

ruff detected issues. Please run ruff check --fix --output-format=full locally, fix the remaining issues, and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.11.7.

Details

sklearn/naive_bayes.py:468:89: E501 Line too long (95 > 88)
    |
466 |                     "For 'balanced' weights, use compute_class_weight('balanced', "
467 |                     "classes=np.unique(y), y=y). For repeated calls to partial_fit, "
468 |                     "y should be the complete target vector to compute accurate class weights."
    |                                                                                         ^^^^^^^ E501
469 |                 )
470 |             # Convert to numpy for compute_sample_weight
    |

sklearn/naive_bayes.py:608:89: E501 Line too long (105 > 88)
    |
606 |     }
607 |
608 |     def __init__(self, alpha=1.0, fit_prior=True, class_prior=None, force_alpha=True, class_weight=None):
    |                                                                                         ^^^^^^^^^^^^^^^^^ E501
609 |         self.alpha = alpha
610 |         self.fit_prior = fit_prior
    |

sklearn/naive_bayes.py:771:89: E501 Line too long (95 > 88)
    |
769 |                     "For 'balanced' weights, use compute_class_weight('balanced', "
770 |                     "classes=np.unique(y), y=y). For repeated calls to partial_fit, "
771 |                     "y should be the complete target vector to compute accurate class weights."
    |                                                                                         ^^^^^^^ E501
772 |                 )
773 |             class_sample_weight = compute_sample_weight(self.class_weight, y)
    |

sklearn/naive_bayes.py:978:89: E501 Line too long (97 > 88)
    |
977 |     def __init__(
978 |         self, *, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, class_weight=None
    |                                                                                         ^^^^^^^^^ E501
979 |     ):
980 |         super().__init__(
    |

Found 4 errors.

ruff format

ruff detected issues. Please run ruff format locally and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.11.7.

Details

--- sklearn/naive_bayes.py
+++ sklearn/naive_bayes.py
@@ -471,7 +471,7 @@
             y_np = _convert_to_numpy(y, xp=xp_y)
             class_sample_weight = compute_sample_weight(self.class_weight, y_np)
             # Convert back if needed
-            if xp.__name__ != 'numpy':
+            if xp.__name__ != "numpy":
                 class_sample_weight = xp.asarray(class_sample_weight, device=device_)
             if sample_weight is not None:
                 sample_weight = sample_weight * class_sample_weight
@@ -605,7 +605,14 @@
         "class_weight": [None, dict, StrOptions({"balanced"})],
     }
 
-    def __init__(self, alpha=1.0, fit_prior=True, class_prior=None, force_alpha=True, class_weight=None):
+    def __init__(
+        self,
+        alpha=1.0,
+        fit_prior=True,
+        class_prior=None,
+        force_alpha=True,
+        class_weight=None,
+    ):
         self.alpha = alpha
         self.fit_prior = fit_prior
         self.class_prior = class_prior
@@ -975,7 +982,13 @@
     """
 
     def __init__(
-        self, *, alpha=1.0, force_alpha=True, fit_prior=True, class_prior=None, class_weight=None
+        self,
+        *,
+        alpha=1.0,
+        force_alpha=True,
+        fit_prior=True,
+        class_prior=None,
+        class_weight=None,
     ):
         super().__init__(
             alpha=alpha,

1 file would be reformatted, 924 files already formatted

Generated for commit: 5b67e46. Link to the linter CI: here

@StefanieSenger

StefanieSenger commented Nov 23, 2025

Copy link
Copy Markdown
Member

Closing as AI spam.

See author's interaction on pandas (pandas-dev/pandas#62818)

@StefanieSenger StefanieSenger added the spam spam PR and issues label Nov 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:naive_bayes spam spam PR and issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants