def test_poissonnb_prior(): """Test whether class priors are properly set. """ Xp = X + np.array([2, 2]) clf = PoissonNB().fit(Xp, y) assert_array_almost_equal(np.array([3, 3]) / 6.0, clf.class_prior_, 8) clf.fit(X2, y2) assert_array_almost_equal(clf.class_prior_.sum(), 1) # Verify that np.log(clf.predict_proba(X)) gives the same results as # clf.predict_log_proba(X) y_pred_proba = clf.predict_proba(X2) y_pred_log_proba = clf.predict_log_proba(X2) assert_array_almost_equal(np.log(y_pred_proba), y_pred_log_proba, 8)
def test_check_accuracy_on_digits(): # Non regression test to make sure that any further refactoring / optim # of the NB models do not harm the performance on a slightly non-linearly # separable dataset digits = load_digits() X, y = digits.data, digits.target binary_3v8 = np.logical_or(digits.target == 3, digits.target == 8) X_3v8, y_3v8 = X[binary_3v8], y[binary_3v8] # Multinomial NB scores = cross_val_score(MultinomialNB(alpha=10), X, y, cv=10) assert_greater(scores.mean(), 0.86) scores = cross_val_score(MultinomialNB(alpha=10), X_3v8, y_3v8, cv=10) assert_greater(scores.mean(), 0.94) # Bernoulli NB scores = cross_val_score(BernoulliNB(alpha=10), X > 4, y, cv=10) assert_greater(scores.mean(), 0.83) scores = cross_val_score(BernoulliNB(alpha=10), X_3v8 > 4, y_3v8, cv=10) assert_greater(scores.mean(), 0.92) # Gaussian NB scores = cross_val_score(GaussianNB(), X, y, cv=10) assert_greater(scores.mean(), 0.77) scores = cross_val_score(GaussianNB(), X_3v8, y_3v8, cv=10) assert_greater(scores.mean(), 0.86) # Poisson NB scores = cross_val_score(PoissonNB(), X, y, cv=10) assert_greater(scores.mean(), 0.85) scores = cross_val_score(PoissonNB(), X_3v8, y_3v8, cv=10) assert_greater(scores.mean(), 0.95)
def test_poissonnb(): clf = PoissonNB() assert_raises(ValueError, clf.fit, -X2, y2) y_pred = clf.fit(X2, y2).predict(X2) assert_array_equal(y_pred, y2)