8000 moved class_prior in NB to __init__ by mrshu · Pull Request #1499 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

moved class_prior in NB to __init__ #1499

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

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions sklearn/naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import numpy as np
from scipy.sparse import issparse
import warnings

from .base import BaseEstimator, ClassifierMixin
from .preprocessing import binarize, LabelBinarizer
Expand Down Expand Up @@ -238,6 +239,12 @@ def fit(self, X, y, sample_weight=None, class_prior=None):
if sample_weight is not None:
Y *= array2d(sample_weight).T

if class_prior is not None:
warnings.warn('class_prior is deprecated in fit function and will '
'be removed in version 0.15. Use it in __init__ instead.')
else:
class_prior = self.class_weight

if class_prior:
if len(class_prior) != n_classes:
raise ValueError("Number of priors must match number of"
Expand Down Expand Up @@ -311,7 +318,7 @@ class MultinomialNB(BaseDiscreteNB):
>>> from sklearn.naive_bayes import MultinomialNB
>>> clf = MultinomialNB()
>>> clf.fit(X, Y)
MultinomialNB(alpha=1.0, fit_prior=True)
MultinomialNB(alpha=1.0, class_weight=None, fit_prior=True)
>>> print(clf.predict(X[2]))
[3]

Expand All @@ -322,9 +329,10 @@ class MultinomialNB(BaseDiscreteNB):
Tackling the poor assumptions of naive Bayes text classifiers, ICML.
"""

def __init__(self, alpha=1.0, fit_prior=True):
def __init__(self, alpha=1.0, fit_prior=True, class_weight=None):
self.alpha = alpha
self.fit_prior = fit_prior
self.class_weight = class_weight

def _count(self, X, Y):
"""Count and smooth feature occurrences."""
Expand Down Expand Up @@ -377,7 +385,7 @@ class BernoulliNB(BaseDiscreteNB):
>>> from sklearn.naive_bayes import BernoulliNB
>>> clf = BernoulliNB()
>>> clf.fit(X, Y)
BernoulliNB(alpha=1.0, binarize=0.0, fit_prior=True)
BernoulliNB(alpha=1.0, binarize=0.0, class_weight=None, fit_prior=True)
>>> print(clf.predict(X[2]))
[3]

Expand All @@ -395,10 +403,11 @@ class BernoulliNB(BaseDiscreteNB):
naive Bayes -- Which naive Bayes? 3rd Conf. on Email and Anti-Spam (CEAS).
"""

def __init__(self, alpha=1.0, binarize=.0, fit_prior=True):
def __init__(self, alpha=1.0, binarize=.0, fit_prior=True, class_weight=None):
self.alpha = alpha
self.binarize = binarize
self.fit_prior = fit_prior
self.class_weight = class_weight

def _count(self, X, Y):
"""Count and smooth feature occurrences."""
Expand Down
0