def __del__(self): if __debug__: debug('CLF_', 'Destroying libsvm.SVMProblem %s' % ` self `) svmc.delete_svm_problem(self.prob) svmc.delete_double(self.y_array) for i in range(self.size): svmc.svm_node_array_destroy(self.data[i]) svmc.svm_node_matrix_destroy(self.x_matrix)
def __del__(self): if __debug__: debug('CLF_', 'Destroying libsvm.SVMProblem %s' % `self`) svmc.delete_svm_problem(self.prob) svmc.delete_double(self.y_array) for i in range(self.size): svmc.svm_node_array_destroy(self.data[i]) svmc.svm_node_matrix_destroy(self.x_matrix)
def predict_values_raw(self, x): #convert x into SVMNode, allocate a double array for return n = self.nr_class * (self.nr_class - 1) // 2 data = seq_to_svm_node(x) dblarr = svmc.new_double(n) svmc.svm_predict_values(self.model, data, dblarr) ret = double_array_to_list(dblarr, n) svmc.delete_double(dblarr) svmc.svm_node_array_destroy(data) return ret
def predict_values_raw(self, x): #convert x into SVMNode, allocate a double array for return n = self.nr_class*(self.nr_class-1)//2 data = seq_to_svm_node(x) dblarr = svmc.new_double(n) svmc.svm_predict_values(self.model, data, dblarr) ret = double_array_to_list(dblarr, n) svmc.delete_double(dblarr) svmc.svm_node_array_destroy(data) return ret
def predict_probability(self, x): #c code will do nothing on wrong type, so we have to check ourself if self.svm_type == NU_SVR or self.svm_type == EPSILON_SVR: raise TypeError, "call get_svr_probability or get_svr_pdf " \ "for probability output of regression" elif self.svm_type == ONE_CLASS: raise TypeError, "probability not supported yet for one-class " \ "problem" #only C_SVC, NU_SVC goes in if not self.probability: raise TypeError, "model does not support probabiliy estimates" #convert x into SVMNode, alloc a double array to receive probabilities data = seq_to_svm_node(x) dblarr = svmc.new_double(self.nr_class) pred = svmc.svm_predict_probability(self.model, data, dblarr) pv = double_array_to_list(dblarr, self.nr_class) svmc.delete_double(dblarr) svmc.svm_node_array_destroy(data) p = {} for i in range(len(self.labels)): p[self.labels[i]] = pv[i] return pred, p
def free_double_array(x): if x != 'NULL' and x != None: svmc.delete_double(x)