예제 #1
0
def train(data, path):
    if os.path.exists(path):
        print 'Model path exists, do nothing.'
        return
    print "Loading features."
    integerizer = tools.integerization.CIntegerization()
    labels = []
    samples = []
    for sent in io.getsent(data):
        for index in range(len(sent)):
            f = feature.extractFeatures(sent, index, integerizer)
            x = int(sent[index][2])
            assert x == 0 or x == 1
            if x == 0: x = -1
            labels.append(x)
            samples.append(f)
    print "Training SVM."
    problem = svm.svm_problem(labels, samples)
    param = svm.svm_parameter()
    param.svm_type = svm.C_SVC
    param.kernel_type = svm.LINEAR  #
    param.C = 1
    #param.degree=2
    param.eps = 1.0
    param.probability = 1
    param.cache_size = 1000
    param.shrinking = 0
    model = svmutil.svm_train(problem, param)
    print "Saving model."
    os.mkdir(path)
    svmutil.svm_save_model(os.path.join(path, "scr"), model)
    integerizer.write(os.path.join(path, "int"))
예제 #2
0
 def __init__(self, probability=False, auto_weighting=True):
     if libsvm is None:
         raise RuntimeError(
             'LibSVM Library not found. Cannot use this classifier.')
     self.param = libsvm.svm_parameter(kernel_type=libsvm.RBF,
                                       probability=probability)
     self.output_probability = probability
     self.auto_weighting = auto_weighting
예제 #3
0
def demo1():
    """
    Set of exercises to better understand workings of SVM
    """
    pdb.set_trace()
    prob = svm.svm_problem([1, -1], [[1, 0, 1], [-1, 0, -1]])
    param = svm.svm_parameter()
    mod = svm.svm_model(prob, param)
예제 #4
0
	def train(self, labeled_featuresets):
		"""
			Trains the classifier on the specified training set. Multiple calls to
			this method invalidates the previous ones.

			The labeled_featuresets parameter must have the format [([feature], label)]
		"""
		self.__features_ids = {}
		self.__last_feature_id = 0
		self.__labels_ids = {}
		self.__labels = []

		y, x = self.__adapt_labeled_featuresets(labeled_featuresets)
		prob  = svm_problem(y, x)
		param = svm_parameter('-c 1000 -q')
		self.__model = svm_train(prob, param)
예제 #5
0
    def train(self, labeled_featuresets):
        """
			Trains the classifier on the specified training set. Multiple calls to
			this method invalidates the previous ones.

			The labeled_featuresets parameter must have the format [([feature], label)]
		"""
        self.__features_ids = {}
        self.__last_feature_id = 0
        self.__labels_ids = {}
        self.__labels = []

        y, x = self.__adapt_labeled_featuresets(labeled_featuresets)
        prob = svm_problem(y, x)
        param = svm_parameter('-c 1000 -q')
        self.__model = svm_train(prob, param)
예제 #6
0
def train(data, path):
   if os.path.exists(path):
      print 'Model path exists, do nothing.'
      return
   print "Loading features."
   integerizer = tools.integerization.CIntegerization()
   labels = []
   samples = []
   for classid, feature in extractfeat.extractFeat(data, integerizer):
      labels.append(classid)
      samples.append(feature)
   print "Training SVM."
   problem = svm.svm_problem(labels, samples)
   param = svm.svm_parameter()
   param.kernel_type = svm.LINEAR#
   param.C=1
   #param.degree=2
   param.eps=1
   param.probability=1
   model = svmutil.svm_train(problem, param)
   print "Saving model."
   os.mkdir(path)
   svmutil.svm_save_model(os.path.join(path, "scr"), model)  
   integerizer.write(os.path.join(path, "int"))
예제 #7
0
파일: wraplibsvm.py 프로젝트: mpsampat/milk
 def __init__(self,probability = False, auto_weighting = True):
     if libsvm is None:
         raise RuntimeError('LibSVM Library not found. Cannot use this classifier.')
     self.param = libsvm.svm_parameter(kernel_type = libsvm.RBF, probability = probability)
     self.output_probability = probability
     self.auto_weighting = auto_weighting
예제 #8
0
 def Training(self, kernel, target, data, IsKernel=False):
     problem = svm.svm_problem(target, data, isKernel=IsKernel)
     parameter = svm.svm_parameter(kernel)
     model = svmutil.svm_train(problem, parameter)
     return model
예제 #9
0
def f(z):
    cc, gamma = z
    param = svm_parameter('-t 2 -g ' + str(gamma) + ' -c ' + str(cc))
    m = svm_train(prob, param)
    p_label_test, p_acc_test, p_val_test = svm_predict(ytest, xtest, m)
    return p_acc_test[0]
예제 #10
0
from libsvm.svm import svm_problem, svm_parameter, gen_svm_nodearray
from libsvm.svmutil import *
x = []
for row in X_train:
    x.append(row)
y = Y_train
from svm import *

prob = svm_problem(y, x)

xtest = []
for row in X_test:
    xtest.append(row)
ytest = Y_test

param = svm_parameter('-t ')
m = svm_train(prob, param)  # m is a ctype pointer to an svm_model
p_label, p_acc, p_val = svm_predict(y, x, m)
p_label_test, p_acc_test, p_val_test = svm_predict(ytest, xtest, m)


def f(z):
    cc, gamma = z
    param = svm_parameter('-t 2 -g ' + str(gamma) + ' -c ' + str(cc))
    m = svm_train(prob, param)
    p_label_test, p_acc_test, p_val_test = svm_predict(ytest, xtest, m)
    return p_acc_test[0]


from scipy.optimize import minimize