def __init__(self, model, dataset, kernel): self.kernel = kernel modelc = model.contents self.svm_type = modelc.param.svm_type if is_classification_problem(self.svm_type) \ and modelc.nSV[0] == 0: raise ValueError, 'model contains no support vectors' if is_classification_problem(self.svm_type): self.nr_class = modelc.nr_class self.labels = N.array(modelc.labels[:self.nr_class]) nrho = self.nr_class * (self.nr_class - 1) / 2 self.rho = modelc.rho[:nrho] self.sv_coef = [modelc.sv_coef[i][:modelc.l] for i in range(self.nr_class - 1)] self.nSV = [modelc.nSV[i] for i in range(self.nr_class)] start = N.zeros((self.nr_class,), N.intc) for i in range(1, self.nr_class): start[i] = start[i - 1] + modelc.nSV[i - 1] self.start = start else: self.rho = modelc.rho[0] self.sv_coef = modelc.sv_coef[0][:modelc.l] if modelc.param.kernel_type != libsvm.PRECOMPUTED: svptrs = [modelc.SV[i] for i in range(modelc.l)] support_vectors = [dataset.iddatamap[addressof(svptr[0])] for svptr in svptrs] else: ids = [int(modelc.SV[i][0].value) for i in range(modelc.l)] support_vectors = [dataset[id] for id in ids] self.support_vectors = support_vectors self.is_compact = False libsvm.svm_destroy_model(model)
def __del__(self): libsvm.svm_destroy_model(self.model)