diff --git a/sklearn/naive_bayes.py b/sklearn/naive_bayes.py index 276ba53d91200..534acc420af66 100644 --- a/sklearn/naive_bayes.py +++ b/sklearn/naive_bayes.py @@ -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) diff --git a/sklearn/tests/test_naive_bayes.py b/sklearn/tests/test_naive_bayes.py index b90e77a1e3d59..4028595c46049 100644 --- a/sklearn/tests/test_naive_bayes.py +++ b/sklearn/tests/test_naive_bayes.py @@ -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)