def test_naive_bayes_sklearn(): from EvoDAG.naive_bayes import NaiveBayes as NB from EvoDAG.node import NaiveBayes try: from sklearn.naive_bayes import GaussianNB except ImportError: return class Var(object): def __init__(self, a): self.hy = a self.hy_test = None m = GaussianNB().fit(X, cl) hy = m._joint_log_likelihood(X) # hy = m.predict_proba(X) * 2 - 1 vars = [Var(SparseArray.fromlist(x)) for x in X.T] nb = NB(mask=SparseArray.fromlist([1 for _ in X[:, 0]]), klass=SparseArray.fromlist(cl), nclass=3) naive_bayes = NaiveBayes([x for x in range(4)], naive_bayes=nb) naive_bayes.eval(vars) for a, b in zip(naive_bayes.hy, hy.T): for a1, b1 in zip(a.full_array(), b): assert_almost_equals(a1, b1, 3)
testRows = tmp print 'test length = ', len(testRows) classes = [ 'Nominated Best Picture', 'Won Best Picture', ] clf=GaussianNB() clf.fit(features[trainRows, :])[:, favoriteCols], labels[trainRows, 0], sample_weight=None) clf._update_mean_variance(n_past, mu, var, X, sample_weight=None) clf.partial_fit(features[trainRows, :])[:, favoriteCols], labels[trainRows, 0], classes=classes, sample_weight=None) clf._partial_fit(features[trainRows, :])[:, favoriteCols], labels[trainRows, 0], classes=classes, _refit=False,sample_weight=None) clf._joint_log_likelihood(features[trainRows, :])[:, favoriteCols]) print 'accuracy = %f' %(np.mean((y_test-y_pred)==0))