def __init__(self,Gs,ls): self._K = ls.shape[1] self._Gs = Gs self._N = Gs.shape[1] self._ls = ls self._svm = LibSvm('c_svc','rbf',\ gamma=1.0/self._N,C=100,probability=True)
def BuildModel(self, data, labels): # Create and train the classifier. svm = LibSvm(kernel_type=self.kernel, C=self.C, gamma=self.gamma) svm.learn(data, labels) return svm
def bench_mlpy(X, y, T, valid): # # .. MLPy .. # from mlpy import LibSvm start = datetime.now() clf = LibSvm(kernel_type='rbf', C=1., gamma=1. / sigma) clf.learn(X, y.astype(np.float64)) score = np.mean(clf.pred(T) == valid) return score, datetime.now() - start
def __init__(self, config): self.config = config self.commands = {} self.command_names = {} commands_folder = self.config['commands_folder'] for index, command_name in enumerate(os.listdir(commands_folder)): cmd = Command(command_name, index, os.path.join(commands_folder, command_name)) self.commands[command_name] = cmd self.command_names[index] = command_name self.svm = LibSvm(svm_type='c_svc', kernel_type='linear') x, y = [], [] for command in self.commands.itervalues(): for feature in command.objects: x.append(feature) y.append(command.index) self.svm.learn(x, y)
def metric(self): totalTimer = Timer() with totalTimer: model = LibSvm(**self.build_opts) model.learn(self.data_split[0], self.data_split[1]) if len(self.data) >= 2: predictions = model.pred(self.data[1]) metric = {} metric["runtime"] = totalTimer.ElapsedTime() if len(self.data) == 3: confusionMatrix = Metrics.ConfusionMatrix(self.data[2], predictions) metric['ACC'] = Metrics.AverageAccuracy(confusionMatrix) metric['MCC'] = Metrics.MCCMultiClass(confusionMatrix) metric['Precision'] = Metrics.AvgPrecision(confusionMatrix) metric['Recall'] = Metrics.AvgRecall(confusionMatrix) metric['MSE'] = Metrics.SimpleMeanSquaredError( self.data[2], predictions) return metric