Skip to content

MultinomialNB cannot handle single class without fitting prior #17926

@sherbold

Description

@sherbold

Describe the bug

MultinomialNB crashes when predict is called after training with a single class without fitting the prior.

Steps/Code to Reproduce

Example:

import sklearn
from sklearn.naive_bayes import MultinomialNB

sklearn.show_versions()

X = [(0.072777, 0.334995),
     (0.857577, 0.977991),
     (0.310364, 0.230206),
     (0.75821 , 0.600593),
     (0.883202, 0.066408)]

y = [0, 0, 0, 0, 0]

clf = MultinomialNB(fit_prior=False)
clf.fit(X, y)
clf.predict(X)

Expected Results

Either no error is thrown and the data is handled correctly or a ValueError that specifies that either fit_prior must be True or that the data must contain multiple classes.

Actual Results

File "C:\Users\sherbold\PycharmProjects\sklearn-dev\venv\lib\site-packages\sklearn\naive_bayes.py", line 79, in predict
    return self.classes_[np.argmax(jll, axis=1)]
IndexError: index 1 is out of bounds for axis 0 with size 1

Versions

System:
python: 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
executable: C:\Users\sherbold\PycharmProjects\sklearn-dev\venv\Scripts\python.exe
machine: Windows-10-10.0.18362-SP0

Python dependencies:
pip: 19.0.3
setuptools: 40.8.0
sklearn: 0.23.1
numpy: 1.19.0
scipy: 1.5.1
Cython: None
pandas: 1.0.5
matplotlib: None
joblib: 0.16.0
threadpoolctl: 2.1.0

Built with OpenMP: True

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions