def __init__(self, y, x):
        assert len(y) == len(x)
        self.prob = prob = svmc.new_svm_problem()
        self.size = size = len(y)

        self.y_array = y_array = svmc.new_double(size)
        for i in range(size):
            svmc.double_setitem(y_array, i, y[i])

        self.x_matrix = x_matrix = svmc.svm_node_matrix(size)
        self.data = []
        self.maxlen = 0
        for i in range(size):
            data = _convert_to_svm_node_array(x[i])
            self.data.append(data)
            svmc.svm_node_matrix_set(x_matrix, i, data)
            if type(x[i]) == dict:
                if (len(x[i]) > 0):
                    self.maxlen = max(self.maxlen, max(x[i].keys()))
            else:
                self.maxlen = max(self.maxlen, len(x[i]))

        svmc.svm_problem_l_set(prob, size)
        svmc.svm_problem_y_set(prob, y_array)
        svmc.svm_problem_x_set(prob, x_matrix)
	def __init__(self,y,x):
		assert len(y) == len(x)
		self.prob = prob = svmc.new_svm_problem()
		self.size = size = len(y)

		self.y_array = y_array = svmc.new_double(size)
		for i in range(size):
			svmc.double_setitem(y_array,i,y[i])

		self.x_matrix = x_matrix = svmc.svm_node_matrix(size)
		self.data = []
		self.maxlen = 0;
		for i in range(size):
			data = _convert_to_svm_node_array(x[i])
			self.data.append(data);
			svmc.svm_node_matrix_set(x_matrix,i,data)
			if type(x[i]) == dict:
				if (len(x[i]) > 0):
					self.maxlen = max(self.maxlen,max(x[i].keys()))
			else:
				self.maxlen = max(self.maxlen,len(x[i]))

		svmc.svm_problem_l_set(prob,size)
		svmc.svm_problem_y_set(prob,y_array)
		svmc.svm_problem_x_set(prob,x_matrix)
def cross_validation(prob, param, fold):
    if param.gamma == 0:
        param.gamma = 1.0 / prob.maxlen
    dblarr = svmc.new_double(prob.size)
    svmc.svm_cross_validation(prob.prob, param.param, fold, dblarr)
    ret = _double_array_to_list(dblarr, prob.size)
    svmc.delete_double(dblarr)
    return ret
def _double_array(seq):
    size = len(seq)
    array = svmc.new_double(size)
    i = 0
    for item in seq:
        svmc.double_setitem(array, i, item)
        i = i + 1
    return array
def cross_validation(prob, param, fold):
	if param.gamma == 0:
		param.gamma = 1.0/prob.maxlen
	dblarr = svmc.new_double(prob.size)
	svmc.svm_cross_validation(prob.prob, param.param, fold, dblarr)
	ret = _double_array_to_list(dblarr, prob.size)
	svmc.delete_double(dblarr)
	return ret
def _double_array(seq):
	size = len(seq)
	array = svmc.new_double(size)
	i = 0
	for item in seq:
		svmc.double_setitem(array,i,item)
		i = i + 1
	return array
 def predict_values_raw(self, x):
     #convert x into svm_node, allocate a double array for return
     n = self.nr_class * (self.nr_class - 1) // 2
     data = _convert_to_svm_node_array(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 svm_node, allocate a double array for return
		n = self.nr_class*(self.nr_class-1)//2
		data = _convert_to_svm_node_array(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
예제 #9
0
	def __init__(self, prob, param):
		if param is None:
			raise NotImplementedError, "svm_varma can't load a svm_model from a file (yet)"

		self.prob = prob
		if param.gamma == 0:
			param.gamma = 1.0/prob.maxlen
		msg = svmc.svm_check_parameter(prob.prob,param.param)
		if msg: raise ValueError, msg
		self.model = svmc.svm_train(prob.prob,param.param)

		#setup some classwide variables
		self.nr_class = nr_class = svmc.svm_get_nr_class(self.model)
		self.svm_type = svmc.svm_get_svm_type(self.model)
		#create labels(classes)
		intarr = svmc.new_int(self.nr_class)
		svmc.svm_get_labels(self.model,intarr)
		self.labels = labels = _int_array_to_list(intarr, self.nr_class)
		svmc.delete_int(intarr)
		if len(labels) != 2:
			raise NotImplementedError, "svm_varma doesn't handle problems with more than 2 labels (yet)"
		#check if valid probability model
		self.probability = svmc.svm_check_probability_model(self.model)

		model = self.model
		self.obj = svmc.svm_varma_get_obj(model, 0)
		self.rho = svmc.svm_varma_get_rho(model, 0)
		self.nSV = svmc.svm_varma_get_nSV(model, 0)
		self.total_sv = svmc.svm_varma_get_total_sv(model)

		# XXX: this only work for 2-class problems
		size = self.total_sv
		doublearr = svmc.new_double(size)
		svmc.svm_varma_get_sv_coef(model, 0, doublearr)
		self.sv_coef = _double_array_to_list(doublearr, size)
		svmc.delete_double(doublearr)
		
		# XXX: only works with PRECOMPUTED kernels
		size = self.total_sv
		doublearr = svmc.new_double(size)
		svmc.svm_varma_get_SV(model, doublearr)
		self.SV = _double_array_to_list(doublearr, size)
		svmc.delete_double(doublearr)
예제 #10
0
    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 svm_node, alloc a double array to receive probabilities
        data = _convert_to_svm_node_array(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 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 svm_node, alloc a double array to receive probabilities
		data = _convert_to_svm_node_array(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
예제 #12
0
def get_model_params(model):
    """
    Extract the alpha and b parameters from the SVM model.
    returns (alpha, b)
    """
    rho = svmc.svm_get_model_rho(model.model)
    n = svmc.svm_get_model_num_coefs(model.model)
    coefs_dblarr = svmc.new_double(n)
    perm_intarr = svmc.new_int(n)
    try:
        svmc.svm_get_model_coefs(model.model,coefs_dblarr)
        svmc.svm_get_model_perm(model.model,perm_intarr)
        coefs = np.zeros(n,dtype=float)
        perm = np.zeros(n,dtype=int)
        for i in range(n):
            coefs[i] = svmc.double_getitem(coefs_dblarr,i)
            perm[i] = svmc.int_getitem(perm_intarr,i)
    finally:
        svmc.delete_double(coefs_dblarr)
        svmc.delete_int(perm_intarr)
    return (coefs, perm, rho)
예제 #13
0
def get_model_params(model):
    """
    Extract the alpha and b parameters from the SVM model.
    returns (alpha, b)
    """
    rho = svmc.svm_get_model_rho(model.model)
    n = svmc.svm_get_model_num_coefs(model.model)
    coefs_dblarr = svmc.new_double(n)
    perm_intarr = svmc.new_int(n)
    try:
        svmc.svm_get_model_coefs(model.model,coefs_dblarr)
        svmc.svm_get_model_perm(model.model,perm_intarr)
        coefs = np.zeros(n,dtype=float)
        perm = np.zeros(n,dtype=int)
        for i in range(n):
            coefs[i] = svmc.double_getitem(coefs_dblarr,i)
            perm[i] = svmc.int_getitem(perm_intarr,i)
    finally:
        svmc.delete_double(coefs_dblarr)
        svmc.delete_int(perm_intarr)
    return (coefs, perm, rho)