def qda(train_features, train_labels, test_featues, test_labels): from modshogun import QDA, MulticlassAccuracy qda = QDA(train_features, train_labels) qda.train() train_output = qda.apply() test_output = qda.apply(test_features) evaluator = MulticlassAccuracy() print 'QDA training error is %.4f' % ((1-evaluator.evaluate(train_output, train_labels))*100) print 'QDA test error is %.4f' % ((1-evaluator.evaluate(test_output, test_labels))*100)
def classifier_qda_modular (train_fname=traindat, test_fname=testdat, label_fname=label_traindat, tolerance=1e-4, store_covs=False): try: from modshogun import RealFeatures, MulticlassLabels, QDA, CSVFile except ImportError: return feats_train=RealFeatures(CSVFile(train_fname)) feats_test=RealFeatures(CSVFile(test_fname)) labels=MulticlassLabels(CSVFile(label_fname)) qda = QDA(feats_train, labels, tolerance, store_covs) qda.train() qda.apply(feats_test).get_labels() qda.set_features(feats_test) return qda, qda.apply().get_labels()
def classifier_qda_modular(train_fname=traindat, test_fname=testdat, label_fname=label_traindat, tolerance=1e-4, store_covs=False): try: from modshogun import RealFeatures, MulticlassLabels, QDA, CSVFile except ImportError: return feats_train = RealFeatures(CSVFile(train_fname)) feats_test = RealFeatures(CSVFile(test_fname)) labels = MulticlassLabels(CSVFile(label_fname)) qda = QDA(feats_train, labels, tolerance, store_covs) qda.train() qda.apply(feats_test).get_labels() qda.set_features(feats_test) return qda, qda.apply().get_labels()
def qda(train_features, train_labels, test_featues, test_labels): from modshogun import QDA, MulticlassAccuracy qda = QDA(train_features, train_labels) qda.train() train_output = qda.apply() test_output = qda.apply(test_features) evaluator = MulticlassAccuracy() print 'QDA training error is %.4f' % ( (1 - evaluator.evaluate(train_output, train_labels)) * 100) print 'QDA test error is %.4f' % ( (1 - evaluator.evaluate(test_output, test_labels)) * 100)
pylab.contour(xx, yy, Z, linewidths = 3, colors = 'k') # Number of classes M = 3 # Number of samples of each class N = 300 # Dimension of the data dim = 2 cols = ['blue', 'green', 'red'] fig = pylab.figure() ax = fig.add_subplot(111) pylab.title('Quadratic Discrimant Analysis') X, y = gen_data() labels = MulticlassLabels(y) features = RealFeatures(X.T) qda = QDA(features, labels, 1e-4, True) qda.train() ypred = qda.apply().get_labels() plot_data(qda, X, y, ypred, ax) for i in range(M): plot_cov(ax, qda.get_mean(i), qda.get_cov(i), cols[i]) plot_regions(qda) pylab.connect('key_press_event', util.quit) pylab.show()
plot(x_pos, y_pos, 'bo'); # negative examples mean_neg = [0, -3] cov_neg = [[100,50], [20, 3]] x_neg, y_neg = np.random.multivariate_normal(mean_neg, cov_neg, 500).T plot(x_neg, y_neg, 'ro'); # train qda labels = MulticlassLabels( np.concatenate([np.zeros(N), np.ones(N)]) ) pos = np.array([x_pos, y_pos]) neg = np.array([x_neg, y_neg]) features = RealFeatures( np.array(np.concatenate([pos, neg], 1)) ) qda = QDA() qda.set_labels(labels) qda.train(features) # compute output plot iso-lines xs = np.array(np.concatenate([x_pos, x_neg])) ys = np.array(np.concatenate([y_pos, y_neg])) x1_max = max(1.2*xs) x1_min = min(1.2*xs) x2_max = max(1.2*ys) x2_min = min(1.2*ys) x1 = np.linspace(x1_min, x1_max, size) x2 = np.linspace(x2_min, x2_max, size)
# Number of classes M = 3 # Number of samples of each class N = 300 # Dimension of the data dim = 2 cols = ['blue', 'green', 'red'] fig = pylab.figure() ax = fig.add_subplot(111) pylab.title('Quadratic Discrimant Analysis') X, y = gen_data() labels = MulticlassLabels(y) features = RealFeatures(X.T) qda = QDA(features, labels, 1e-4, True) qda.train() ypred = qda.apply().get_labels() plot_data(qda, X, y, ypred, ax) for i in range(M): plot_cov(ax, qda.get_mean(i), qda.get_cov(i), cols[i]) plot_regions(qda) pylab.connect('key_press_event', util.quit) pylab.show()