Exemple #1
0
  def generate_model(self, variant_name, models_folder):
    training_file = variant_name + ".t"
    if self.feature_scaling:
      self.scale_features(variant_name, models_folder)
      training_file += ".scale"
    (y, x) = svm_read_problem(training_file)
    self.m_prob = svm.svm_problem(y, x, self.m_params.kernel_type == PRECOMPUTED)

    libsvm_path = os.environ['LIBSVM_PATH']
    scaled_filename = os.path.abspath(training_file)
    cp = "python grid.py " + scaled_filename
    curdir = os.getcwd()
    os.chdir(libsvm_path + "/tools/")
    result = call_process(cp)
    os.chdir(curdir)
    C,g,rate = [float(l) for l in result.split("\n")[-2].split(" ")]

    print "C: %.8f, gamma: %.8f\n" % (C,g)

    self.m_params.C = C
    self.m_params.gamma = g

    print "\n-----------------------------"
    model = svm.svm_train(self.m_prob, self.m_params)
    print "-----------------------------\n"

    svm_save_model(models_folder + variant_name + ".model", model)
Exemple #2
0
    def generate_model(self, variant_name, models_folder):
        training_file = variant_name + ".t"
        if self.feature_scaling:
            self.scale_features(variant_name, models_folder)
            training_file += ".scale"
        (y, x) = svm_read_problem(training_file)
        self.m_prob = svm.svm_problem(y, x,
                                      self.m_params.kernel_type == PRECOMPUTED)

        libsvm_path = os.environ['LIBSVM_PATH']
        scaled_filename = os.path.abspath(training_file)
        cp = "python grid.py " + scaled_filename
        curdir = os.getcwd()
        os.chdir(libsvm_path + "/tools/")
        result = call_process(cp)
        os.chdir(curdir)
        C, g, rate = [float(l) for l in result.split("\n")[-2].split(" ")]

        print "C: %.8f, gamma: %.8f\n" % (C, g)

        self.m_params.C = C
        self.m_params.gamma = g

        print "\n-----------------------------"
        model = svm.svm_train(self.m_prob, self.m_params)
        print "-----------------------------\n"

        svm_save_model(models_folder + variant_name + ".model", model)
Exemple #3
0
def compute_best_svm_radius(train_matrix, train_labels, val_matrix, val_labels,
                            radius_to_consider):
    """Compute the optimal SVM radius using the provided training and evaluation datasets.

    You should only consider radius values within the radius_to_consider list.
    You should use accuracy as a metric for comparing the different radius values.

    Args:
        train_matrix: The word counts for the training data
        train_labels: The spma or not spam labels for the training data
        val_matrix: The word counts for the validation data
        val_labels: The spam or not spam labels for the validation data
        radius_to_consider: The radius values to consider

    Returns:
        The best radius which maximizes SVM accuracy.
    """
    # *** START CODE HERE ***
    results = []
    for r in radius_to_consider:
        model = svm.svm_train(train_matrix, train_labels, r)
        pred = svm.svm_predict(model, val_matrix, r)
        accuracy = np.mean(pred == val_labels)
        print("Radius: {}, Accuracy: {}".format(r, accuracy))
        record = {"r": r, 'acc': accuracy}
        results.append(record)
    results.sort(key=lambda x: -x['acc'])
    return results[0]['r']
	def __init__(self,arg1,arg2=None):
		if arg2 == None:
			# create model from file
			filename = arg1
			self.model = svmc.svm_load_model(filename)
		else:
			# create model from problem and parameter
			prob,param = arg1,arg2
			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 = 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 = _int_array_to_list(intarr, self.nr_class)
		svmc.delete_int(intarr)
		#check if valid probability model
		self.probability = svmc.svm_check_probability_model(self.model)
Exemple #5
0
def train(xtrain, ytrain):
    a = ada_train(xtrain, ytrain, 10)
    g = gnb_train(xtrain, ytrain)
    s = svm_train(xtrain, ytrain)
    return a, g, s
Exemple #6
0
nn_model.test_neural_network(test_X, test_Y)

<<<<<<< HEAD

rf.random_forest_train(train_X,train_Y)
rf.random_forest_test(test_X, test_Y)



=======
rf.random_forest_train(train_X,train_Y)
rf.random_forest_test(test_X, test_Y)

>>>>>>> d7c5221fc5e365f34a6b61e63a316a5b421943e5
dt.decisiontrain(train_X,train_Y)
dt.decisiontest(test_X, test_Y)

svm.svm_train(train_X,train_Y)
svm.svm_test(test_X,test_Y)



#random forest test for one sample of meal or no meal : not tested yet!
def model1_test_one_sample(test_sample):
    #test_data = pcafeature.get_reduced_test_data(test_sample)
    output = rf.rf_test_one_sample(test_sample)
    #dt.dt_test_one(test_sample)
    print(output)
    return output