def predict(self): """ Predicts tags in the given data It reports the accuracy if the data is fully labeled @type data_test: SequenceData @param data_test: the test data set @return: the predicted labels, the accuracy, the f1_score """ # keep starting timestamp start_time = time.time() assert (not self.feature_extractor.is_training), "In order to predict, is_training should be False" # Extract features on all instances (labeled or unlabeled) of the test set # pass them to liblinearutil for prediction pred_labels, (acc, _, _), _ = liblinearutil.predict(self.label_list_test, self.features_list_test, self.__liblinear_model, "-q") # print some useful information if not self.quiet: num_seconds = int(math.ceil(time.time() - start_time)) # estimate prediction time print("Prediction time: {0}".format(str(datetime.timedelta(seconds=num_seconds)))) # convert predicted labels from integer IDs to strings. pred_labels = self.convert_prediction(pred_labels, self.data_test) # for i, label in enumerate(pred_labels): # pred_labels[i] = self.feature_extractor.get_label_string(label) self.__save_prediction_to_file(self.data_test, pred_labels) return pred_labels, acc
def model_training(x,y,x_test,y_test): prob = liblinearutil.problem(y, x) start = time.process_time() # sklearn.svm.libsvm.cross_validation(x,y) # sklearn.svm.libsvm.fit(x,y) param = liblinearutil.parameter('-q') m = liblinearutil.train(prob, param) train_time = time.process_time() - start start2 = time.process_time() pred_labels, (acc, MSE, SCC), pred_values = liblinearutil.predict(y_test, x_test, m) predict_time = time.process_time() - start2 # print(time.process_time() - start) return train_time,predict_time
def model_training(n, i): meth = method[i] y, x = liblinearutil.svm_read_problem("other_method/kmeans_linear/%s/train_%s" % ( name, meth)) y_test, x_test = liblinearutil.svm_read_problem("other_method/kmeans_linear/%s/test_%s" % ( name, meth)) prob = liblinearutil.problem(y, x) temp_result = np.empty((14)) for idx, val in enumerate(cost): param = liblinearutil.parameter(' -q -c %f' % (val)) m = liblinearutil.train(prob, param) pred_labels, (temp_result[idx], MSE, SCC), pred_values = liblinearutil.predict(y_test, x_test, m) return (i, f_idx, n, temp_result)
def model_training(): meth = method[0] y, x = liblinearutil.svm_read_problem("other_method/kmeans_linear/%s/train_%s" % ( name, meth)) y_test, x_test = liblinearutil.svm_read_problem("other_method/kmeans_linear/%s/test_%s" % ( name, meth)) prob = liblinearutil.problem(y, x) temp_result = np.zeros((12)) # print(x.shape(1)) for idx, val in enumerate(cost): start = time.time() param = liblinearutil.parameter(' -q -c %f' % (val)) m = liblinearutil.train(prob, param) pred_labels, (temp_result[idx], MSE, SCC), pred_values = liblinearutil.predict(y_test, x_test, m) # print(temp_result) t2 = time.time()-start return np.max(temp_result),t2
def model_training(): x, y = load_svmlight_file("svm/BudgetedSVM/original/%s/train" % (name)) x_test, y_test = load_svmlight_file("svm/BudgetedSVM/original/%s/test" % (name)) # x = np.asarray(x) # x_test = np.asarray(x_test) # scaler = StandardScaler().fit(x) # # x = scaler.transform(x) prob = liblinearutil.problem(y, x) temp_result = np.empty((14)) # x_test = scaler.transform(x_test) param = liblinearutil.parameter(' -q ') start = time.process_time() m = liblinearutil.train(prob, param) print(time.process_time() - start) start2 = time.process_time() pred_labels, (acc, MSE, SCC), pred_values = liblinearutil.predict(y_test, x_test, m) print(time.process_time() - start2, acc) exit()