Beispiel #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"))
Beispiel #2
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)
Beispiel #3
0
 def train(self, features, labels):
     labels,names = normaliselabels(labels)
     if self.auto_weighting:
         nlabels = labels.max() + 1
         self.param.nr_weight = int(nlabels)
         self.param.weight_label = range(nlabels)
         self.param.weight = [(labels != i).mean() for i in xrange(nlabels)]
     problem = libsvm.svm_problem(labels.astype(float), features)
     model = libsvm.svm_model(problem, self.param)
     return libsvmModel(model, names, self.output_probability)
Beispiel #4
0
 def train(self, features, labels):
     labels, names = normaliselabels(labels)
     if self.auto_weighting:
         nlabels = labels.max() + 1
         self.param.nr_weight = int(nlabels)
         self.param.weight_label = list(range(nlabels))
         self.param.weight = [(labels != i).mean() for i in range(nlabels)]
     problem = libsvm.svm_problem(labels.astype(float), features)
     model = libsvm.svm_model(problem, self.param)
     return libsvmModel(model, names, self.output_probability)
	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)
    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)
Beispiel #7
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"))
Beispiel #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
Beispiel #9
0
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        label = float(row[0])
        Y_test.append(label)
Y_test = np.array(Y_test)
Y_test -= 1

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))