Skip to content

MAINT validate parameter in GaussianNB #23583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 28, 2022
14 changes: 11 additions & 3 deletions sklearn/naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import warnings

from abc import ABCMeta, abstractmethod

from numbers import Real

import numpy as np
from scipy.special import logsumexp
Expand All @@ -30,7 +30,7 @@
from .utils.multiclass import _check_partial_fit_first_call
from .utils.validation import check_is_fitted, check_non_negative
from .utils.validation import _check_sample_weight

from .utils._param_validation import Interval

__all__ = [
"BernoulliNB",
Expand Down Expand Up @@ -137,7 +137,7 @@ class GaussianNB(_BaseNB):

Parameters
----------
priors : array-like of shape (n_classes,)
priors : array-like of shape (n_classes,), default=None
Prior probabilities of the classes. If specified, the priors are not
adjusted according to the data.

Expand Down Expand Up @@ -212,6 +212,11 @@ class labels known to the classifier.
[1]
"""

_parameter_constraints = {
"priors": ["array-like", None],
"var_smoothing": [Interval(Real, 0, None, closed="left")],
}

def __init__(self, *, priors=None, var_smoothing=1e-9):
self.priors = priors
self.var_smoothing = var_smoothing
Expand Down Expand Up @@ -239,6 +244,7 @@ def fit(self, X, y, sample_weight=None):
self : object
Returns the instance itself.
"""
self._validate_params()
y = self._validate_data(y=y)
return self._partial_fit(
X, y, np.unique(y), _refit=True, sample_weight=sample_weight
Expand Down Expand Up @@ -360,6 +366,8 @@ def partial_fit(self, X, y, classes=None, sample_weight=None):
self : object
Returns the instance itself.
"""
self._validate_params()

return self._partial_fit(
X, y, classes, _refit=False, sample_weight=sample_weight
)
Expand Down
1 change: 0 additions & 1 deletion sklearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,6 @@ def test_estimators_do_not_raise_errors_in_init_or_set_params(Estimator):
"FunctionTransformer",
"GammaRegressor",
"GaussianMixture",
"GaussianNB",
"GaussianProcessClassifier",
"GaussianProcessRegressor",
"GaussianRandomProjection",
Expand Down