def compute_SVM_results(i_train, i_test, n_components=5): classifiers = [] predictions = [] Xtests = [] ytests = [] Xtrains = [] ytrains = [] for i in range(len(attributes)): Xtrain = X[i][i_train] Xtest = X[i][i_test] ytrain = y[i][i_train] ytest = y[i][i_test] clf = GMMBayes(n_components, min_covar=1E-5, covariance_type='full', random_state=0) clf.fit(Xtrain, ytrain) y_pred = clf.predict(Xtest) classifiers.append(clf) predictions.append(y_pred) return classifiers, predictions
def gmm_bayes_analysis(X_train, X_test, y_train, y_test): clf = GMMBayes() t1 = time.time() clf.fit(X_train, y_train) t2 = time.time() t_train = t2 - t1 t1 = time.time() score = clf.score(X_test, y_test) t2 = time.time() t_test = t2 - t1 # Generate graphs/data for analysis tpr, fpr, roc_auc = roc_calc(GMMBayes(), X_train, X_test, y_train, y_test) return tpr, fpr, roc_auc, t_train, t_test, score
def test_too_many_components_warning(): X = np.random.normal(0, 1, size=(3, 2)) y = np.zeros(3) ncm = 5 clf = GMMBayes(ncm) with pytest.warns(UserWarning, match="Expected n_samples >= " "n_components but got "): clf.fit(X, y)
def test_incompatible_shapes_exception(): X = np.random.normal(0, 1, size=(100, 2)) y = np.zeros(99) ncm = 1 clf = GMMBayes(ncm) with pytest.raises(Exception) as e: assert clf.fit(X, y) assert str(e.value) == "X and y have incompatible shapes"
def test_incompatible_number_of_components_exception(): X = np.random.normal(0, 1, size=(100, 2)) y = np.zeros(100) ncm = [1, 2, 3] clf = GMMBayes(ncm) with pytest.raises(Exception) as e: assert clf.fit(X, y) assert str(e.value) == ("n_components must be compatible with " "the number of classes")
def test_gmm1d(): x1 = np.random.normal(0, 1, size=100) x2 = np.random.normal(10, 1, size=100) X = np.concatenate((x1, x2)).reshape((200, 1)) y = np.zeros(200) y[100:] = 1 ncm = 1 clf = GMMBayes(ncm) clf.fit(X, y) predicted = clf.predict(X) assert_allclose(y, predicted)
def test_gmm2d(): x1 = np.random.normal(0, 1, size=(100, 2)) x2 = np.random.normal(10, 1, size=(100, 2)) X = np.vstack((x1, x2)) y = np.zeros(200) y[100:] = 1 for ncm in (1, 2, 3): clf = GMMBayes(ncm) clf.fit(X, y) predicted = clf.predict(X) assert_allclose(y, predicted)
def compute_GMMbayes(Ncolors, Ncomp): classifiers = [] predictions = [] for ncm in Ncomp: classifiers.append([]) predictions.append([]) for nc in Ncolors: clf = GMMBayes(ncm, min_covar=1E-5, covariance_type='full') clf.fit(X_train[:, :nc], y_train) y_pred = clf.predict(X_test[:, :nc]) classifiers[-1].append(clf) predictions[-1].append(y_pred) return classifiers, predictions
#posterior[m] = knc.predict_proba(X_test) print "Error-Correcting Output Code: ", np.mean( accuracy) / 0.72, np.std(accuracy) / 0.72 print k for i in range(0, 6): for j in range(0, 6): print '{:5.2f} '.format(box[i, j] / 100.0), print #end GNB box = np.zeros([6, 6]) accuracy = np.zeros(100) for m in range(0, 100): gmm = GMMBayes(n_components=3) y_pred = gmm.fit(X_train, y_train).predict(X_test) for i in range(0, len(y_pred)): if y_pred[i] == y_test[i]: n = n + 1 accuracy[m] = accuracy[m] + 1 box[y_test[i] - 1, y_pred[i] - 1] = box[y_test[i] - 1, y_pred[i] - 1] + 1 print "Gaussian Mixture Models, n_components=3: ", np.mean( accuracy) / 0.72, np.std(accuracy) / 0.72 for i in range(0, 6): for j in range(0, 6): print '{:5.2f} '.format(box[i, j] / 100.0), print box = np.zeros([6, 6])
X_test = sample2 y_test = labels[272:, i] else: X_train = training y_train = labels[:172, i] X_test = sampletest y_test = labels[172:, i] if i >= 2: j = 3 else: j = 6 accuracy = np.zeros(72) posterior = np.empty([10000, 72, 6]) box = np.zeros([6, 6]) for m in range(0, 100): gmm = GMMBayes(n_components=6) gmm.fit(X_train, y_train) y_pred = gmm.predict(X_test) n = 0 for i in range(0, len(y_pred)): if y_pred[i] == y_test[i]: #print i, y_pred[i], y_test[i] n = n + 1 accuracy[i] = accuracy[i] + 1 box[y_test[i] - 1, y_pred[i] - 1] = box[y_test[i] - 1, y_pred[i] - 1] + 1 posterior[m] = gmm.predict_proba(X_test) print 30, 20, sum(accuracy[0:8]) / 8.0, sum(accuracy[8:18]) / 10.0, sum( accuracy[18:30]) / 12.0, sum(accuracy[30:43]) / 13.0, sum( accuracy[43:56]) / 13.0, sum(