def RunNBCShogun(q): totalTimer = Timer() Log.Info("Loading dataset", self.verbose) try: # Load train and test dataset. trainData = np.genfromtxt(self.dataset[0], delimiter=',') testData = np.genfromtxt(self.dataset[1], delimiter=',') # Labels are the last row of the training set. labels = MulticlassLabels(trainData[:, (trainData.shape[1] - 1)]) with totalTimer: # Transform into features. trainFeat = RealFeatures(trainData[:, :-1].T) testFeat = RealFeatures(testData.T) # Create and train the classifier. nbc = GaussianNaiveBayes(trainFeat, labels) nbc.train() # Run Naive Bayes Classifier on the test dataset. nbc.apply(testFeat).get_labels() except Exception as e: q.put(-1) return -1 time = totalTimer.ElapsedTime() q.put(time) return time
def classifier_gnb_modular(fm_train_real=traindat, fm_test_real=testdat, label_train_multiclass=label_traindat): from shogun.Features import RealFeatures, Labels from shogun.Classifier import GaussianNaiveBayes feats_train = RealFeatures(fm_train_real) feats_test = RealFeatures(fm_test_real) labels = Labels(label_train_multiclass) gnb = GaussianNaiveBayes(feats_train, labels) gnb_train = gnb.train() output = gnb.classify(feats_test).get_labels() return gnb, gnb_train, output
import numpy as np import os from shogun.Features import RealFeatures, MulticlassLabels from shogun.Classifier import GaussianNaiveBayes # Load the data. f = open(os.path.dirname(__file__) + '../data/fisheriris.data') data = np.fromfile(f, dtype=np.float64, sep=' ') data = data.reshape(-1, 4) f.close() f = open(os.path.dirname(__file__) + '../data/fisheriris_label.csv') label = np.fromfile(f, dtype=np.float64, sep=' ') f.close() # Naive Bayes classifier. feat = RealFeatures(data.T) test = RealFeatures(data.T) labels = MulticlassLabels(label) nbc = GaussianNaiveBayes(feat, labels) nbc.train() # Predict labels. results = nbc.apply(test).get_labels() print results