Skip to content

Multinomial Bayes issue #5814

Closed
Closed

Description

@mladenk42

Given the digits dataset (available in sklearn.datasets), we split it into train and test set.
We fit a MultinomialNB classifier on the train set, and generate predictions on that same train
set. When this is done without smoothing, the classification performance is rather low.
This is contrary to expectations, as the classifier has already seen all possible feature values.
So not including smoothing shouldn't really make such a big difference.

It seems the BernoulliNB class from sklearn also has this problem.

My inefficient but straightforward implementation performs expectedly well on the training set.
Code to reproduce the issue with some more tests is available at http://pastebin.com/2hsrA8xL
I hope the issue is not caused by some trivial implementation detail I've overlooked.

Activity

  1. agramfort commented on Nov 14, 2015

    @agramfort
    Member
  2. mladenk42 commented on Nov 14, 2015

    @mladenk42
    Author

    Might be a silly question but where do i send it :)? The scikit-learn-issues mailing list?
    Thanks.

  3. agramfort commented on Nov 14, 2015

    @agramfort
    Member
  4. mladenk42 commented on Nov 15, 2015

    @mladenk42
    Author

    Since it's my first time submitting an error report I'm completely lost :(

    It says here that PR-s are used to tell others about changes I pushed to a repository. I didn't push anything O_o, nor do I want to. The demo script I submitted (via pastebin link) doesn't fix the issue, or change anything in sklearn code. It's just there to reproduce the issue, so someone who knows the sklearn code can debug it more easily. Perhaps I'm misunderstanding something.

  5. absolutelyNoWarranty commented on Nov 19, 2015

    @absolutelyNoWarranty
    Contributor

    It seems when alpha=0.0 and the data has features which never change, feature_log_prob_ has -inf which causes the calculations down the line to become all nan.

    Given
    X = np.array([[1, 0],[1, 1]])
    y = np.array([0, 1])

    Compare

    nb = BernoulliNB(alpha=0.)
    nb.fit(X, y)
    
    nb.predict_proba(X)
    Out[135]:
    array([[ nan,  nan],
           [ nan,  nan]])
    
    nb.feature_log_prob_
    Out[139]:
    array([[  0., -inf],
           [  0.,   0.]])
    

    and

    nb = BernoulliNB(alpha=1e-15)
    nb.fit(X, y)
    nb.predict_proba(X)
    
    Out[136]:
    array([[  1.0000e+00,   8.8818e-16],
           [  1.0000e-15,   1.0000e+00]])
    
  6. amueller commented on Sep 14, 2016

    @amueller
    Member

    We should probably throw an error when that happens if that creates issues.

  7. added this to the 0.19 milestone on Sep 14, 2016
  8. added a commit that references this issue on Sep 23, 2016
    4ce3e8f
  9. yl565 commented on Sep 24, 2016

    @yl565
    Contributor

    @amueller Please see my PR to this issue.

  10. added a commit that references this issue on Oct 17, 2016
    d3bb0ec
  11. added a commit that references this issue on Jun 15, 2017
    ddb5383
  12. jmschrei commented on Jun 19, 2017

    @jmschrei
    Member

    Fixed via #9131

  13. added a commit that references this issue on Jun 19, 2017
    b4b5de8
  14. added 2 commits that reference this issue on Aug 7, 2017
    8f4dd76
    4c89d6a
  15. added a commit that references this issue on Aug 11, 2017
    34163ad
  16. added a commit that references this issue on Aug 19, 2017
    b47d6c8
  17. added a commit that references this issue on Aug 29, 2017
    9a4dba1
  18. added a commit that references this issue on Nov 15, 2017
    4fb8495
  19. added a commit that references this issue on Dec 18, 2017
    1da68c1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions