8000 MRG Check that X is non_zero for MultinomialNB. by amueller · Pull Request #908 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

MRG Check that X is non_zero for MultinomialNB. #908

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load 8000 comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sklearn/naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ def _count(X, Y):
N_c is the count of all features in all samples of class c;
N_c_i is the count of feature i in all samples of class c.
"""
if issparse(X):
if np.any(X.data < 0):
raise ValueError("Input X must be non-negative.")
else:
if np.any(X < 0):
raise ValueError("Input X must be non-negative.")
N_c_i = safe_sparse_dot(Y.T, X)
N_c = np.sum(N_c_i, axis=1)

Expand Down
1 change: 1 addition & 0 deletions sklearn/tests/test_naive_bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def test_mnnb():
for X in [X2, scipy.sparse.csr_matrix(X2)]:
# Check the ability to predict the learning set.
clf = MultinomialNB()
assert_raises(ValueError, clf.fit, -X, y2)
y_pred = clf.fit(X, y2).predict(X)

assert_array_equal(y_pred, y2)
Expand Down
0