-
-
Notifications
You must be signed in to change notification settings - Fork 26k
[MRG] Adding variable force_alpha to classes in naive_bayes.py #18805
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
Changes from all commits
282a7dd
d78e17b
3b79637
464dc37
a4429bf
cf35eb1
15a658f
ec786b3
4606d85
f0debb6
dcce4a8
81d5f32
43dfda5
be0ebfe
2968400
7e1b649
5782e6f
a7337d2
2d16091
728b842
a8209e0
44c50fd
ded1f6e
2a2d8f3
d8f784e
a3897f7
23c68dd
b1151d7
1d01c6c
aa1d8de
203af9e
cc4fda7
91127bc
c4d0736
8964a16
e7a5f37
98c0c12
2d9ab41
16af708
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -462,6 +462,11 @@ Changelog | |||||
|
||||||
:mod:`sklearn.naive_bayes` | ||||||
.......................... | ||||||
- |Fix| A new parameter `force_alpha` was added to :class:`BernoulliNB` and | ||||||
class:`MultinomialNB`, allowing user to set parameter alpha to a very | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also need to mention |
||||||
small number, greater or equal 0, which was earlier automatically changed | ||||||
to `_ALPHA_MIN` instead. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change |
||||||
:pr:`16747`, :pr:`18805` by :user:`arka204` and :user:`hongshaoyang`. | ||||||
|
||||||
- |Fix| The `fit` and `partial_fit` methods of the discrete naive Bayes | ||||||
classifiers (:class:`naive_bayes.BernoulliNB`, | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -537,11 +537,18 @@ def _check_alpha(self): | |||||
"with shape [n_features]" | ||||||
) | ||||||
if np.min(self.alpha) < _ALPHA_MIN: | ||||||
warnings.warn( | ||||||
"alpha too small will result in numeric errors, " | ||||||
"setting alpha = %.1e" % _ALPHA_MIN | ||||||
) | ||||||
return np.maximum(self.alpha, _ALPHA_MIN) | ||||||
if self.force_alpha: | ||||||
warnings.warn( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really useful to warn when the user specifically set the parameter |
||||||
"alpha too small will result in numeric errors. " | ||||||
"Proceeding with alpha = %.1e, as " | ||||||
"force_alpha was set to True." % self.alpha | ||||||
) | ||||||
else: | ||||||
warnings.warn( | ||||||
"alpha too small will result in numeric errors, " | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add something like "use force_alpha=True to keep alpha unchanged.". |
||||||
"setting alpha = %.1e" % _ALPHA_MIN | ||||||
) | ||||||
return np.maximum(self.alpha, _ALPHA_MIN) | ||||||
return self.alpha | ||||||
|
||||||
def partial_fit(self, X, y, classes=None, sample_weight=None): | ||||||
|
@@ -735,7 +742,14 @@ class MultinomialNB(_BaseDiscreteNB): | |||||
---------- | ||||||
alpha : float, default=1.0 | ||||||
Additive (Laplace/Lidstone) smoothing parameter | ||||||
(0 for no smoothing). | ||||||
(set alpha=0 and force_alpha=True, for no smoothing). | ||||||
|
||||||
force_alpha : bool, default=False | ||||||
If False and alpha is too close to 0, it will set alpha to | ||||||
`_ALPHA_MIN`. If True, warn user about potential numeric errors | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change |
||||||
and proceed with alpha unchanged. | ||||||
|
||||||
.. versionadded:: 1.0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
fit_prior : bool, default=True | ||||||
Whether to learn class prior probabilities or not. | ||||||
|
@@ -820,8 +834,11 @@ class MultinomialNB(_BaseDiscreteNB): | |||||
https://nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html | ||||||
""" | ||||||
|
||||||
def __init__(self, *, alpha=1.0, fit_prior=True, class_prior=None): | ||||||
def __init__( | ||||||
self, *, alpha=1.0, force_alpha=False, fit_prior=True, class_prior=None | ||||||
): | ||||||
self.alpha = alpha | ||||||
self.force_alpha = force_alpha | ||||||
self.fit_prior = fit_prior | ||||||
self.class_prior = class_prior | ||||||
|
||||||
|
@@ -862,7 +879,15 @@ class ComplementNB(_BaseDiscreteNB): | |||||
Parameters | ||||||
---------- | ||||||
alpha : float, default=1.0 | ||||||
Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing). | ||||||
Additive (Laplace/Lidstone) smoothing parameter | ||||||
(set alpha=0 and force_alpha=True, for no smoothing). | ||||||
|
||||||
force_alpha : bool, default=False | ||||||
If False and alpha is too close to 0, it will set alpha to | ||||||
`_ALPHA_MIN`. If True, warn user about potential numeric errors | ||||||
and proceed with alpha unchanged. | ||||||
|
||||||
.. versionadded:: 1.0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
fit_prior : bool, default=True | ||||||
Only used in edge case with a single class in the training set. | ||||||
|
@@ -949,8 +974,17 @@ class ComplementNB(_BaseDiscreteNB): | |||||
https://people.csail.mit.edu/jrennie/papers/icml03-nb.pdf | ||||||
""" | ||||||
|
||||||
def __init__(self, *, alpha=1.0, fit_prior=True, class_prior=None, norm=False): | ||||||
def __init__( | ||||||
self, | ||||||
*, | ||||||
alpha=1.0, | ||||||
force_alpha=False, | ||||||
fit_prior=True, | ||||||
class_prior=None, | ||||||
norm=False, | ||||||
): | ||||||
self.alpha = alpha | ||||||
self.force_alpha = force_alpha | ||||||
self.fit_prior = fit_prior | ||||||
self.class_prior = class_prior | ||||||
self.norm = norm | ||||||
|
@@ -998,7 +1032,14 @@ class BernoulliNB(_BaseDiscreteNB): | |||||
---------- | ||||||
alpha : float, default=1.0 | ||||||
Additive (Laplace/Lidstone) smoothing parameter | ||||||
(0 for no smoothing). | ||||||
(set alpha=0 and force_alpha=True, for no smoothing). | ||||||
|
||||||
force_alpha : bool, default=False | ||||||
If False and alpha is too close to 0, it will set alpha to | ||||||
`_ALPHA_MIN`. If True, warn user about potential numeric errors | ||||||
and proceed with alpha unchanged. | ||||||
|
||||||
.. versionadded:: 1.0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
binarize : float or None, default=0.0 | ||||||
Threshold for binarizing (mapping to booleans) of sample features. | ||||||
|
@@ -1079,8 +1120,17 @@ class BernoulliNB(_BaseDiscreteNB): | |||||
naive Bayes -- Which naive Bayes? 3rd Conf. on Email and Anti-Spam (CEAS). | ||||||
""" | ||||||
|
||||||
def __init__(self, *, alpha=1.0, binarize=0.0, fit_prior=True, class_prior=None): | ||||||
def __init__( | ||||||
self, | ||||||
*, | ||||||
alpha=1.0, | ||||||
force_alpha=False, | ||||||
binarize=0.0, | ||||||
fit_prior=True, | ||||||
class_prior=None, | ||||||
): | ||||||
self.alpha = alpha | ||||||
self.force_alpha = force_alpha | ||||||
self.binarize = binarize | ||||||
self.fit_prior = fit_prior | ||||||
self.class_prior = class_prior | ||||||
|
@@ -1144,7 +1194,14 @@ class CategoricalNB(_BaseDiscreteNB): | |||||
---------- | ||||||
alpha : float, default=1.0 | ||||||
Additive (Laplace/Lidstone) smoothing parameter | ||||||
(0 for no smoothing). | ||||||
(set alpha=0 and force_alpha=True, for no smoothing). | ||||||
|
||||||
force_alpha : bool, default=False | ||||||
If False and alpha is too close to 0, it will set alpha to | ||||||
`_ALPHA_MIN`. If True, warn user about potential numeric errors | ||||||
and proceed with alpha unchanged. | ||||||
|
||||||
.. versionadded:: 1.0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
fit_prior : bool, default=True | ||||||
Whether to learn class prior probabilities or not. | ||||||
|
@@ -1221,9 +1278,16 @@ class CategoricalNB(_BaseDiscreteNB): | |||||
""" | ||||||
|
||||||
def __init__( | ||||||
self, *, alpha=1.0, fit_prior=True, class_prior=None, min_categories=None | ||||||
self, | ||||||
*, | ||||||
alpha=1.0, | ||||||
force_alpha=False, | ||||||
fit_prior=True, | ||||||
class_prior=None, | ||||||
min_categories=None, | ||||||
): | ||||||
self.alpha = alpha | ||||||
self.force_alpha = force_alpha | ||||||
self.fit_prior = fit_prior | ||||||
self.class_prior = class_prior | ||||||
self.min_categories = min_categories | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because v1.0 is already released, this entry should now be moved to
v1.1.rst
.