def save(self, save_path): with commons.PhaseLogger("LIBSVM.save"): svmutil.svm_save_model( save_path + "/" + self._get_class_name() + ".model", self._model) #svmutil.save_svm_model(save_path + "\\" + self._get_class_name() + ".model", self._model) logging.info("[%s]: Save Model Done" % self._get_class_name())
def write_result(core_params, cores_centered, user_param): model_fname = '%s_w%s' % (user_param['tfname'], user_param['width']) outdir = "%s/"%user_param['outdir'] if user_param['outdir'] else "" # if suffix: # model_fname = "%s_%s" % (model_fname, suffix) param_log = "" pmlist = [] for core in core_params: # Save the best model best_dict = max(core_params[core], key = lambda p:p["avg_scc"]) # get predicted vs measured pm = predict_kfold(best_dict['params'], rows=cores_centered[core], numfold=user_param['numfold'], kmers=user_param['kmers']) pm = pd.DataFrame(pm) pm["core"] = core pmlist.append(pm) model = generate_svm_model(cores_centered[core], best_dict["params"], user_param['kmers']) svmutil.svm_save_model('%s%s_%s.model' % (outdir,model_fname,core), model) param_log += "%s: %s\n" % (core,str(best_dict)) pm_df = pd.concat(pmlist) rsq_all = pm_df["measured"].corr(pm_df["predicted"])**2 param_log += "R²: %s\n" % rsq_all with open("%s%s.log" % (outdir,model_fname), 'w') as f: f.write(param_log)
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"))
def get_SVM_trained_classifer(self, training_datafile, classifier_dumpfile): # read all tweets and labels tweet_items = common.get_filtered_training_data(training_datafile) tweets = [] for (words, sentiment) in tweet_items: words_filtered = [ e.lower() for e in words.split() if (common.is_ascii(e)) ] tweets.append((words_filtered, sentiment)) results = helper.get_SVM_feature_vector_and_labels( self.feature_list, tweets) self.feature_vectors = results['feature_vector'] self.labels = results['labels'] problem = svm_problem(self.labels, self.feature_vectors) # '-q' option suppress console output param = svm_parameter('-q') param.kernel_type = LINEAR # param.show() classifier = svm_train(problem, param) svm_save_model(classifier_dumpfile, classifier) return classifier
def save_trained(self, output_file, filename): model_filename = self.get_model_filename(filename) svm_save_model(model_filename, self._model) f = open(model_filename, 'r') lines = f.readlines() output_file.write("%d\n" % len(lines)) for ln in lines: output_file.write(ln) f.close() os.remove(model_filename)
def save_trained ( self, output_file, filename ): model_filename = self.get_model_filename(filename) svm_save_model(model_filename, self._model) f = open(model_filename, 'r') lines = f.readlines() output_file.write("%d\n"%len(lines)) for ln in lines: output_file.write(ln) f.close() os.remove(model_filename)
def svr_training(X_features,Y_classes,classes=[],output='svr_',training_options = '-s 3 -t 0 -b 1'): """ Configure multiple SV Machines based on a one-against-all (1AA) approach Input: - X_features ndarray(float) : Array of instance features (instances on rows) - Y_classes ndarray(int) : Array of classes identification - classes ['int'] : Classes to be used for the 1AA approach Output: - model_classes [] : SVM models for classes given in 'classes' --- """ model_classes = [] training_options = '-s 3 -t 0 -b 1' diro = 'models/' try: os.mkdir(diro) except: pass; for i_class in classes: classe = 'class'+str(i_class) this_class_indx, other_class_indx = sample_selection(Y_classes,i_class) X = X_features[np.concatenate((this_class_indx,other_class_indx))] Y = np.zeros((len(X),1)) Y[:len(this_class_indx)] = 1 Y[len(this_class_indx):] = -1 Y_list,X_list = _convert_arrays2lists(Y,X) model_classes.append(svm_train(Y_list,X_list,training_options)) svm_save_model(diro+output+classe+'.model',model_classes[-1]) np.savetxt(output+classe+'_svr.dat',np.concatenate((Y,X),axis=1),fmt='%f') return model_classes
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"))
tn, fp, fn, tp = confusion_matrix(res_test, y[0], labels=[0,1]).ravel() recall = tp/(tp+fn) recall precision = tp/(tp+fp) precision f1 = precision*recall*2/(precision+recall) f1 fpr, tpr, thresholds = roc_curve(res_test, y[0]) AUC = auc(fpr, tpr) AUC svmutil.svm_save_model("model_all4_classification.bin",model_svm) svmutil.svm_train(res_train+res_test, list(train)+list(test),'-v 10 -t 2 -c 2') """**Classification: All Feature Including EmoInt**""" featurizer = EmoIntFeaturizer() tokenizer = Tokenizer() def get_vector(h): headlines = [] i= 0 for sent_str in h: print(i)
def train(): (data, chords) = load_data() print len(data) model = svmutil.svm_train(chords, data) svmutil.svm_save_model(model_file, model)
def __getstate__(self): f = tempfile.NamedTemporaryFile(delete=False) svmutil.svm_save_model(f.name, self.model) with open(f.name, "rb") as h: return {'data': h.read()} os.remove(f)
neg_loc = np.where(label < 0) pos_x1 = x1[pos_loc] pos_x2 = x2[pos_loc] neg_x1 = x1[neg_loc] neg_x2 = x2[neg_loc] plt.plot(pos_x1, pos_x2, 'b^') plt.plot(neg_x1, neg_x2, 'ro') plt.xlim(0, 1) plt.ylim(0, 1) ax = plt.gca() ax.set_aspect(1) plt.show() y = list(label) x = svm_data_format(x1, x2) prob = svm.svm_problem(y, x) # param = svm.svm_parameter('-t 0 -c 1') # param = svm.svm_parameter('-t 1 -d 2') param = svm.svm_parameter('-t 2') model = svm.svm_train(prob, param) svm.svm_save_model('test.model', model) # test # y0 = [-1] # x0 = [{1: 1, 2: 0}] # p_label, p_acc, p_val = svm.svm_predict(y0, x0, model) # print('p_label = ', p_label) # print('p_acc = ', p_acc) # print('p_val = ', p_val)
yes = data[label_num] no = [] for i in range(len(data) - 1): if i != label_num: no += data[i] no += data[-1][:int(len(data[-1]) * 1.0)] # Too much nothing... y = [1 for i in range(len(yes))] + [-1 for i in range(len(no))] x = yes + no c = list(zip(x, y)) random.shuffle(c) x, y = zip(*c) return y, x def train(y, x): train_len = int(1.0 * len(y)) prob = svmutil.svm_problem(y[:train_len], x[:train_len]) param = svmutil.svm_parameter('-t 2 -c 4 -b 1 -e 1e-12') m = svmutil.svm_train(prob, param) return m # ans = svmutil.svm_predict(y[train_len:], x[train_len:], m) # return ans # print(m) for i in range(len(label)): y, x = load_data(i) ans = train(y, x) svmutil.svm_save_model("model" + str(i), ans) print(ans)
from libsvm.svmutil import svm_train, svm_save_model from ml_ops import search, train, bootstrap import argparse, sys, datetime modes = ['train', 'bootstrap', 'search'] parser = argparse.ArgumentParser() parser.add_argument("-m", "--mode", help="specify the mode [-m train|bootstrap|search]", required=True, choices=modes) args = parser.parse_args() if args.mode == 'train': hists, labels = train() svm = svm_train(labels, hists, '-s 0 -t 0 -c 1') model_name = 'svm_trained_' + str(datetime.datetime.now()).replace(' ', '_') + '.dat' svm_save_model(model_name, svm) if args.mode == 'bootstrap': hists, labels = train() hists, labels = bootstrap(hists, labels) svm = svm_train(labels, hists, '-s 0 -t 0 -c 1') model_name = 'svm_bootstrapped_' + str(datetime.datetime.now()).replace(' ', '_') + '.dat' svm_save_model(model_name, svm) if args.mode == 'search': search()
def test_conversion_from_filesystem(self): libsvm_model_path = tempfile.mktemp(suffix="model.libsvm") svmutil.svm_save_model(libsvm_model_path, self.libsvm_model) spec = libsvm.convert(libsvm_model_path, "data", "target")