def test(self): count = 0 d = 1 for i in self.images: for j in i.testDataHist: ra = [] la = [] for l in range(len(self.mr)/2):#from the RBF models a,b,c = svmutil.svm_predict([d],[j],self.mr[l*2],'-b 1') ra.append(c) for l in range(len(self.ml)/2):#from the Linear models a,b,c = svmutil.svm_predict([d],[j],self.ml[l*2],'-b 1') la.append(c) self.hmr[count][ra.index(max(ra))]+=1#populating the confusion matricies self.hml[count][la.index(max(la))]+=1 for k in i.testDataVector: ra = [] la = [] for l in range(len(self.mr)/2):#from the RBF models a,b,c = svmutil.svm_predict([d],[k],self.mr[l*2+1],'-b 1') ra.append(c) for l in range(len(self.ml)/2):#from the Linear models a,b,c = svmutil.svm_predict([d],[k],self.ml[l*2+1],'-b 1') la.append(c) self.vmr[count][ra.index(max(ra))]+=1 self.vml[count][la.index(max(la))]+=1 count+=1 d = 0
def TrainSvmLinear(Y, X, sweep_c=range(-2,8)): num_positives = float(Y.count(1)) num_negatives = float(Y.count(-1)) best_c = -1 best_acc = -1 for c_pow in sweep_c: current_c = np.power(2.0,c_pow) prob = svm.svm_problem(Y,X) param = svm.svm_parameter('-v 5 -t 0 -c %f -w-1 %f -w1 %f -q' % (current_c, 100/num_negatives, 100/num_positives)) current_acc = svm.svm_train(prob, param) print '%f, %f' % (current_c, current_acc) if best_acc < current_acc: best_acc = current_acc best_c = current_c # recompute accuracy param = svm.svm_parameter('-t 0 -c %f -w-1 %f -w1 %f -q' % (best_c, 100/num_negatives, 100/num_positives)) svm_model = svm.svm_train(prob, param) p_labs, p_acc, p_vals = svm.svm_predict(Y, X, svm_model, '-q') prob = svm.svm_problem(Y,X) param = svm.svm_parameter('-t 0 -c %f -w-1 %f -w1 %f -q' % (best_c, 100/num_negatives, 100/num_positives)) svm_model = svm.svm_train(prob, param) p_labs, p_acc, p_vals = svm.svm_predict(Y, X, svm_model, '-q') pdb.set_trace() return svm_model
def cons_train_sample_for_cla(filename,indexes,dic_path,glo_aff_path,result_save_path,model_path,LSA_path,LSA_model_path,decom_meas,delete): dic_list = read_dic(dic_path,dtype=str) glo_aff_list = read_list(glo_aff_path) f= file(filename,'r') fs = file(result_save_path,'w') fd = file(dust_save_path,'w') m= svm_load_model(model_path) lsa_m = svm_load_model(LSA_model_path) U = load_lsa_model(LSA_path,"U") for line in f.readlines(): text = line.strip().split(tc_splitTag) if len(text)!=line_length: fd.write(line) continue text_temp="" for i in indexes: text_temp+=str_splitTag+text[i] vec = cons_vec_for_cla(text_temp.strip().split(str_splitTag),dic_list,glo_aff_list) y,x=cons_svm_problem(text[0],vec) p_lab,p_acc,p_sc=svm_predict(y,x,m) if decom_meas==1: weight = cal_weight(p_sc[0][0]) #vec = [value*weight for value in vec ] vec = [0]*len(vec) for key in x[0].keys(): vec[int(key)-1]= weight*float(x[0][key]) vec = pre_doc_svds(vec,U) y,x=cons_svm_problem(text[0],vec) lsa_lab,lsa_acc,lsa_sc = svm_predict(y,x,lsa_m) fs.write(text[0]+"\t"+str(p_sc[0][0])+"\t"+str(lsa_sc[0][0])+"\t"+text[1]+"\t"+text[2]+"\n") else : fs.write(text[0]+"\t"+str(p_sc[0][0])+"\t"+text[1]+"\t"+text[2]+"\n") f.close() fs.close()
def calculate_classification_vector(self, model): """ Calculate classification vector w and the offset b """ # ctypes libsvm bindings # TODO get parameter maybe easier try: self.b = svmutil.svm_predict([0], [[0.0]*self.dim], model, "-q")[2][0][0] except ValueError: self.b = svmutil.svm_predict([0], [[0.0]*self.dim], model)[2][0][0] except IndexError: self._log("Classification failed. " + "Did you specify the parameters correctly?", level=logging.ERROR) self.b = 0 self.w = numpy.zeros(self.dim) self.features = FeatureVector(numpy.atleast_2d(self.w).astype( numpy.float64), self.feature_names) if model.get_labels() == [0, 1]: self.b = -self.b self.w = numpy.zeros(self.dim) for i in range(self.dim): e = [0.0] * self.dim e[i] = 1.0 try: self.w[i] = svmutil.svm_predict([0],[e], model, "-q")[2][0][0] except ValueError: try: self.w[i] = svmutil.svm_predict([0],[e], model)[2][0][0] except IndexError: pass except IndexError: pass if model.get_labels() == [0,1]: self.w[i] = -self.w[i] self.w[i] -= self.b self.features = FeatureVector(numpy.atleast_2d(self.w).astype( numpy.float64), self.feature_names) try: wf = [] for i,feature in enumerate(self.feature_names): if not self.w[i] == 0: wf.append((self.w[i],feature)) wf.sort() w = numpy.array(wf, dtype='|S200') except ValueError: self._log('w could not be converted.', level=logging.WARNING) except IndexError: self._log('There are more feature names than features. \ Please check your feature generation and input data.', level=logging.CRITICAL) self.b = 0 w = numpy.zeros(self.dim) self.w = w # only features without zero multiplier are relevant self.num_retained_features = len(w) self.classifier_information["~~Num_Retained_Features~~"] = \ self.num_retained_features self.print_w = w
def test_model(img_kind): subdir = "data/" model = svmutil.svm_load_model(subdir + img_kind + '.model') print "Finished Loading Model" total_count = 0 correct_count = 0 wrong_count = 0 the_ones = glob.glob(subdir + "f_" + img_kind + "*.jpg") all_of_them = glob.glob(subdir + "f_*_*.jpg") the_others = [] for x in all_of_them: total_count += 1 if the_ones.count(x) < 1: the_others.append(x) for x in the_ones: img = cv.LoadImageM(x) cv.ShowImage("img", img) cv.WaitKey(10) img_features = get_image_features(img, True, img_kind) predict_input_data = [] predict_input_data.append(img_features) (val, val_2, val_3) = svmutil.svm_predict([1], predict_input_data, model) if int(val[0]) == 1: print 'correct' correct_count += 1 else: wrong_count += 1 for x in the_others: img = cv.LoadImageM(x) cv.ShowImage("img", img) cv.WaitKey(10) img_features = get_image_features(img, True, img_kind) predict_input_data = [] predict_input_data.append(img_features) (val, val_2, val_3) = svmutil.svm_predict([1], predict_input_data, model) if int(val[0]) == -1: correct_count += 1 else: wrong_count += 1 print "Total Pictures: " + str(total_count) print "Correct: " + str(correct_count) print "Wrong: " + str(wrong_count) print "Accuracy: " + str(correct_count/float(total_count) * 100) + '%'
def calculate_slack_variables(self, model): """This method calculates from the given SVM model the related slack variables for classification.""" self.t = [] self.num_sv = 0 self.num_nsv = 0 self.inner_margin = 0 self.ti = [] dropped_samples = [] dropped_labels = [] for i in range(self.num_samples): # ctype libsvm bindings try: p = svmutil.svm_predict([0], [ map(float, list(self.samples[i-self.num_nsv]))], model, "-q")[2][0][0] except ValueError: p = svmutil.svm_predict([0], [ map(float, list(self.samples[i-self.num_nsv]))], model)[2][0][0] except IndexError: self._log("Classification failed. " + "Did you specify the parameters correctly?", level=logging.ERROR) p = 0 if model.get_labels() == [0,1]: p = -p p *= 2 * (self.labels[i - self.num_nsv] - 0.5) if p > 1: self.t.append(0) self.ti.append(0) dropped_samples.append(self.samples.pop(i - self.num_nsv)) dropped_labels.append(self.labels.pop(i - self.num_nsv)) self.num_nsv += 1 else: self.t.append(1-p) self.num_sv += 1 if 1-p<1e-5: p = 1 self.ti.append(0) else: self.ti.append(1-p) self.inner_margin +=1 # if self.store_all_samples: for i in range(len(dropped_samples)): self.samples.append(dropped_samples[i]) self.labels.append(dropped_labels[i]) del(dropped_samples) del(dropped_labels)
def detectDoor(self, img, horOffset, verOffset): #print len(img.shape) if len(img.shape) > 2: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) Window = sw.SlidingWindow(64,128,img,horOffset,verOffset) horWin, verWin = Window.getWindows() windows = horWin*verWin print "windows %d " % windows print "windows %d " % horWin print "windows %d " % verWin features = np.zeros((windows,3780),dtype = np.float) ones = np.ones((windows,1),dtype = np.float) labels = ones.ravel() for i in range(0,windows): print i imag = Window.getNextFrame() #cv2.imshow("Hello",imag) #cv2.waitKey(0) #cv2.destroyAllWindows() feature = self.hog.compute(imag) feature = np.transpose(feature); features[i] = feature labelsList = labels.tolist() featuresList = features.tolist() p_label, p_acc, p_val = svm.svm_predict(labelsList, featuresList, self.windowModel) return (p_label,p_acc,p_val)
def valid(self,datasets,opt,opp,method = fold,part_ids = None,seed = None,test_data = None): if seed is None: # If seed is not set. UNIX time is used as seed. seed = time.time() saving_seed = "%s/log/%s.log.seed" % (self._dir,self._name) with open(saving_seed,"w") as fp: # Save used seed value. fp.write("seed:%f\n" % seed) if part_ids is None: part_ids = datasets.pids groups = [(test,train) for test,train in method(part_ids,seed = seed)] for cnt,pdtsts in enumerate(groups): # cnt is number of cluster. if test_data is None: test = False ltest,dtest,itest = test2svm_prob(datasets.mkTest(pdtsts[0])) else: test = True ltest,dtest,itest = test2svm_prob(test_data.mkTest(test_data.pids)) print "start %s validation" % (cnt) ptrn,itrain = train2svm_prob(datasets.mkTrain(pdtsts[1])) #opt = svm.svm_parameter(opt) model = svmutil.svm_train(ptrn,opt) plbl,pacc,pval = svmutil.svm_predict(ltest,dtest,model,opp) # create saving direcotry #self._mkdir(cnt) # create log files self._save_log(itest,plbl,pval,cnt,test) model_name = "%s/model/%s.model.%s" % (self._dir,self._name,cnt)
def getSamePeptideClusters(precMassClusters, scanFDict, svmModel, svmRange, ppmSTD=5, cutOff=0): trueClusters = [] for cluster in precMassClusters: if len(cluster) == 1: trueClusters += [cluster] else: # print 'testing cluster', cluster pairIndex = [] xVals = [] specs = [] for i in range(len(cluster)): specs += [DataFile.getMassIntPairs(scanFDict[cluster[i]]['dta'])] dMatrix = np.ones((len(cluster), len(cluster))) * -2 for i in range(len(cluster)): for j in range(i+1, len(cluster)): epSTD = ppmSTD * 10 ** -6 * scanFDict[cluster[i]]['precMass'] SVMClassificationInfo = SA.getSpectraPairInfoForSVMClassification(specs[i], specs[j], scanFDict[cluster[i]]['precMass'], NMod=0, CMod=0, epsilon=2*epSTD) xVals += [SVMClassificationInfo] pairIndex += [(i, j)] xValsNorm = svmutil.normalize_instances(xVals, svmRange) pLabs = svmutil.svm_predict([0]*len(xValsNorm), xValsNorm, svmModel)[0] # print pLabs for i, pLab in enumerate(pLabs): # Scale distances by 4: totalTICRatio, 1: TotalSharedPeaksRatio dMatrix[pairIndex[i][0]][pairIndex[i][1]] = dMatrix[pairIndex[i][1]][pairIndex[i][0]] = xVals[i][1] if pLab==1 else -1 trueClusters += heirarchicalClusteringAverageLinkage([[scanF] for scanF in cluster], dMatrix, cutOff=cutOff) return trueClusters
def predict_all(request): '''Predicts points in an array''' width = float( request.POST.get("width", "None") ) height = float( request.POST.get("height", "None") ) model = svmutil.svm_load_model('libsvm.model') # Get grid of points to query points = [] for counterY in [ 1.0 / 15.0 * y for y in range(0, 15) ]: for counterX in [ 1.0 / 15.0 * x for x in range(0, 15) ]: points.append([counterX, counterY]) #for counterY in [ 1.0 / 10.0 * x for x in range(0, 10) ]: # for counterX in [ 1.0 / 10.0 * y for y in range(0, 10) ]: # label , acc, val = svmutil.svm_predict( [0], [[counterX, counterY]], model ) # results[i] = [counterX, counterY, label] # i = i + 1 #results["length"] = i # Get labels labels, acc, val = svmutil.svm_predict([0] * len(points), points, model) results = {} for index, value in enumerate(points): results[index] = { "x" : points[index][0], "y" : points[index][1], "label" : labels[index] } results["length"] = len(points) return json(results)
def predict(request): predictX = float( request.POST.get("x", -1) ) predictY = float( request.POST.get("y", -1) ) predictLabel = int( request.POST.get("label", -1) ) if predictX == -1 or predictY == -1 or predictLabel == -1: return django.http.HttpResponse("Missing Params") points = models.Point2d.objects.all() # Storing the information to be presented to SVM labels = [] inputs = [] # For each point, store the information into arrays #for p in points: # labels.append( p.label ) # inputs.append([p.x, p.y]) #prob = svm.svm_problem(labels, inputs) #param = svm.svm_parameter('-t 2 -c 100') #model = svmutil.svm_train(prob, param) #svmutil.svm_save_model('libsvm.model', model) model = svmutil.svm_load_model('libsvm.model') p_label , acc, val = svmutil.svm_predict([0], [[predictX, predictY]], model) data = {'x': predictX, 'y': predictY, 'label': int( p_label[0] ) } return json(data)
def plotSVM(solver, lowerBound, upperBound, step): assert lowerBound < upperBound X = arange(lowerBound, upperBound, step) Y = arange(lowerBound, upperBound, step) X,Y = meshgrid(X,Y) Z = zeros(X.shape) for i in range(len(X)): for j in range(len(Y)): #Classify the result result = int( svm_predict([0.], [[ X[i][j], Y[i][j] ]], solver, '-q')[0][0] ) if result == 0: Z[i][j] = 0 #lower limit else: Z[i][j] = 100 #higher limit plot.imshow(Z) plot.gcf() plot.clim() plot.title("SVM Activation") return plot
def train_test_model(train_datafile, test_datafile): from svmutil import svm_read_problem, svm_train, svm_predict y,x = svm_read_problem(train_datafile) m = svm_train(y,x,'-t 0 -e .01 -m 1000 -h 0') y_test,x_test = svm_read_problem(test_datafile) p_labs,p_acc,p_vals = svm_predict(y_test,x_test,m) return p_labs, p_acc, p_vals
def classify(filename, classLabel=0): str = "/Thu_Life/CS/SVM/data/trainData/Test_SVMFile/singleSVM_TestFile" f = open(str, "wb") t = VSM.TextToVector2(filename) slabel = ("%d ") % classLabel if len(t) > 0: f.write(slabel) for k in range(len(t)): str1 = ("%d:%d ") % (t[k][0], t[k][1]) f.write(str1) f.write("\r\n") else: print "The text can't be classified to the Four Labels!" return "Can't be classified ! " f.close() y, x = svmutil.svm_read_problem(str) model = svmutil.svm_load_model("../SVMTrainFile250.model") label, b, c = svmutil.svm_predict(y, x, model) print "label", label if label[0] == 1: print "类别:财经" return "财经" elif label[0] == 2: print "类别:IT" return "IT" elif label[0] == 3: print "类别:旅游" return "旅游" elif label[0] == 4: print "类别:体育" return "体育"
def prob20(): import random gamma = [1, 10, 100, 1000, 10000] chosen = {1:0, 10:0, 100:0, 1000:0, 10000:0} for _ in range(100): Eout = [] for g in gamma: trainX, testX, trainy, testy = readdat() mul_label_2_bin(trainy, testy, 0) trainX = zip(trainX, trainy) random.shuffle(trainX) trainX, trainy = zip(*trainX) valX = trainX[:1000] valy = trainy[:1000] trainX = trainX[1000:] trainy = trainy[1000:] m = svmutil.svm_train(trainy, trainX, '-s 0 -t 2 -c 0.1 -g %f -h 0'%(g)) p_label, p_acc, p_val = svmutil.svm_predict(valy, valX, m) Eout.append(100.0 - p_acc[0]) chosen[gamma[Eout.index(min(Eout))]] += 1 print "prob20: ", for k in chosen.keys(): print "gamma=%d:%d, " % (k, chosen[k]), print ""
def main(path, k): prabs = [] lns = [] for kk in range(0,k-1): testLabel = [] trainPoint = [] trainLabel = [] testPoint = [] wcCount = 0 for u in os.listdir(path): if u[-2:] == 'WC':r wcCount += 1 filePath = path+u WC = pickle.load(open(filePath, 'rb')) if wcCount % k == 0 + kk: testLabel.append(int(u[1])) testPoint.append(WC) else: trainLabel.append(int(u[1])) trainPoint.append(WC) lns.append(len(testLabel)) prob = svmutil.svm_problem(trainLabel, trainPoint) param = svmutil.svm_parameter('-t 0 -c 4 -b 1 -q') m = svmutil.svm_train(prob, param) svmutil.svm_save_model('n.model', m) p_label, p_acc, p_val = svmutil.svm_predict(testLabel, testPoint, m, '-b 1') prabs.append(p_acc[0])
def CrossValidate(Y, X, param, k_folds=5): rand_idx = range(len(Y)) random.shuffle(rand_idx) idx_groups = SplitIntoK(k_folds, rand_idx) pos_acc = 0 neg_acc = 0 for i in range(k_folds): test_idx = idx_groups[i] exclude_test = [idx_groups[j] for j in range(len(idx_groups)) if i != j] train_idx = list(chain(*exclude_test)) Y_test = [Y[test_i] for test_i in test_idx] X_test = [X[test_i] for test_i in test_idx] Y_train = [Y[train_i] for train_i in train_idx] X_train = [X[train_i] for train_i in train_idx] # recompute accuracy prob = svm.svm_problem(Y_train,X_train) svm_model = svm.svm_train(prob, param) p_labs, p_acc, p_vals = svm.svm_predict(Y_test, X_test, svm_model, '-q') tps = sum([1 for j in range(len(p_labs)) if (p_labs[j]==1 and Y_test[j]==1)]) fns = sum([1 for j in range(len(p_labs)) if (p_labs[j]==-1 and Y_test[j]==1)]) tns = sum([1 for j in range(len(p_labs)) if (p_labs[j]==-1 and Y_test[j]==-1)]) fps = sum([1 for j in range(len(p_labs)) if (p_labs[j]==1 and Y_test[j]==-1)]) pos_acc += tps / float(tps + fns) neg_acc += tns / float(tns + fps) pos_acc = pos_acc / k_folds neg_acc = neg_acc / k_folds return (pos_acc, neg_acc)
def UpdateWordsWithSvm(svm_model, word_results): svm_clf = svm_model[0] min_vals = svm_model[1][0] max_vals = svm_model[1][1] if not word_results: return X_list = [] n_features = -1 for i in range(len(word_results)): word_result = word_results[i] char_bbs = word_result[1] word_score = word_result[0][0,4] features = ComputeWordFeatures(char_bbs, word_score) if n_features < 0: n_features = len(features) X_list.append(features) assert n_features > 0 X_mat = np.vstack(X_list) X_mat = X_mat - min_vals X_mat = X_mat / max_vals X = [dict(zip(range(n_features), x_i)) for x_i in X_mat.tolist()] p_labs, p_acc, p_vals = svm.svm_predict([0]*len(X), X, svm_clf, '-q') labels = svm_clf.get_labels() for i in range(len(word_results)): word_result = word_results[i] if labels[0] < 0: word_result[0][0,4] = -p_vals[i][0] else: word_result[0][0,4] = p_vals[i][0]
def run_predict_svm(nr, model, feats, q): print "Process %i starting for %i rows" % (nr, len(feats)) labels = [] old_time = int(time() * 1000) l = len(feats) for i in range(0,l): if not (i+1) % int(l / 10.): new_time = int(time() * 1000) diff = new_time - old_time print "%i: %i%% (%i ms)" % (nr, int(100 * (i+1) / l), diff) old_time = new_time fs_list = map(lambda x: list(x), feats[i]) # svm uses lists (or dicts) rl = np.ones(len(fs_list)) rl1 = svmutil.svm_predict(rl, fs_list, model, '-q') #-q: quiet labels.append(rl1[0]) # rl1 = (labels, accuracy, another_val) filename = ("labels_%i_%i.npy" % (int(os.times()[-1]), nr)) np.save(filename, labels) q.put(filename) print "Process %i done" % nr
def calc(self,g1,*args): """ takes an unlimited number of grids as inputs and produces a new grid with the result of the model """ if(self.model==None): return None g=[g1] # list of used grids gout=grid.copy_grid(g1) # build an output grid nrows,ncols=gout.size() # store the size of the output for ar in args: g.append(ar) inp=np.zeros(len(g)) inp=inp.tolist() for i in xrange(nrows): for j in xrange(ncols): if(gout.get(i,j)==gout.get_nodata()): continue for k in range(len(g)): if(g[k].get(i,j)==g[k].get_nodata()): gout.set(i,j,gout.get_nodata()) break inp[k]=g[k].get(i,j) inp1=[inp] l,acc,val=su.svm_predict([0]*len(inp1),inp1,self.model) gout.set(i,j,val[0][0]) return gout
def predecir(surface, model): I = pygame.surfarray.array3d(surface) J = I.astype(np.float32).sum(axis = -1); J /= 3.0 J = np.transpose(J) x_mask = [i for i in range(len(J.sum(axis = 0))) if J.sum(axis = 0)[i]] y_mask = [i for i in range(len(J.sum(axis = 1))) if J.sum(axis = 1)[i]] range_x = max(x_mask) - min(x_mask) range_y = max(y_mask) - min(y_mask) if not range_x > range_y: t_mask = y_mask y_mask = x_mask x_mask = t_mask range_x = max(x_mask) - min(x_mask) range_y = max(y_mask) - min(y_mask) min_h = min(x_mask); max_h = max(x_mask); min_v = max(np.ceil(np.mean([max(y_mask), min(y_mask)]) - range_x / 2.0), 1); max_v = min(np.ceil(np.mean([max(y_mask), min(y_mask)]) + range_x / 2.0), surface.get_width()); x = J[min_h: max_h, min_v: max_v] x = sp.misc.imresize(x, [20, 20]) if DEBUG: pl.imshow(x, cmap = pl.cm.gray) pl.show() x = list(x.reshape(-1, order = 'F')) a = sv.svm_predict([-1], [x], model) return LETTERS[int(a[0][0]) - 1]
def clf(x): if hasattr(x, 'tolist'): xx = x.tolist() else: xx = x p_label, _, _ = svm_predict([0], [xx], self.model, '-q') return p_label[0]
def TrainSvmRbf2(Y, X, sweep_c=range(-5,5), sweep_g=range(-5,5)): num_negatives = float(Y.count(-1)) num_positives = float(Y.count(1)) best_c = -1 best_g = -1 best_acc = -1 for c_pow in sweep_c: for g_pow in sweep_g: current_c = np.power(2.0,c_pow) current_g = np.power(2.0,g_pow) prob = svm.svm_problem(Y,X) param = svm.svm_parameter('-t 2 -c %f -g %f -w-1 %f -w1 %f -q' % (current_c, current_g, 100/num_negatives, 100/num_positives)) current_pos_acc, current_neg_acc = CrossValidate(Y, X, param) current_acc = current_pos_acc print 'c = %f, g = %f, cv acc = %f, neg acc = %f' % (current_c, current_g, current_acc, current_neg_acc) if best_acc < current_acc: best_acc = current_acc best_c = current_c best_g = current_g prob = svm.svm_problem(Y,X) param = svm.svm_parameter('-t 2 -c %f -g %f -w-1 %f -w1 %f -q' % (best_c, best_g, 100/num_negatives, 100/num_positives)) svm_model = svm.svm_train(prob, param) p_labs, p_acc, p_vals = svm.svm_predict(Y, X, svm_model, '-q') pdb.set_trace() return svm_model
def _test(self, split, exclude): ''' Tests all items in a split part of the data across everything else. Returns all relevant statistics for F-score evaluation. ''' falseNeg = 0 falsePos = 0 truePos = 0 excludeLabels = [] excludeData = [] for item in exclude: excludeLabels.append(item[1]) excludeData.append(item[0]) svm_m = svm_model(excludeData,excludeLabels, kernel=self.model.__svmparam__.kernel_type, c=self.model.c) split_len = len(split[0]) #for i in xrange(split_len): predict,acc,val = svmutil.svm_predict(split[1],split[0],svm_m.model) #print 'Predicted label: %d\t Real label:%d'%(int(predict[0]),int(split[1][i])) prediction, reality = predict, split[1] return (prediction, reality)
def train_test(): train_subdir = "data/train/" test_subdir = "data/test/" img_kinds = ["happy", "anger", "neutral", "surprise"] models = {} params = "-t 0 -c 3" svm_params = { "happy": params, "anger": params, "neutral": params, "surprise": params} #train the models print 'BUILDING TRAIN MODELS' for img_kind in img_kinds: print "\t" + img_kind problem = build_problem(img_kind, train_subdir) param = svm.svm_parameter(svm_params[img_kind]) models[img_kind] = svmutil.svm_train(problem, param) print '================================' #for each image in test set let's see what is the answe total_count = 0 correct_count = 0 wrong_count = 0 print 'TESTING MODELS' for img_kind in img_kinds: images = glob.glob(test_subdir + "f_" + img_kind + "*.jpg") for image in images: print "\t" + image image_data = cv.LoadImage(image) # Let's see what are the results from the models results = {} for kind in img_kinds: test_data = get_image_features(image_data, True, kind) predict_input_data = [] predict_input_data.append(test_data) # do svm query (val, val_2, label) = svmutil.svm_predict([1] ,predict_input_data, models[kind]) results[kind] = label[0][0] sorted_results = sorted(results.iteritems(), key=operator.itemgetter(1)) result = sorted_results[len(sorted_results)-1][0] total_count += 1 if result == img_kind: print 'YES :' + result correct_count += 1 else: print 'NO :' + result print sorted_results wrong_count += 1 print '-----------------------' print '================================' print "Total Pictures: " + str(total_count) print "Correct: " + str(correct_count) print "Wrong: " + str(wrong_count) print "Accuracy: " + str(correct_count/float(total_count) * 100)
def kfold(data, labels, k): try: import svmutil except: return 0 prabs = [] for xxx in range(0, 10): picks = np.random.choice(len(data), len(data) / k, replace=False) testLabel = labels[picks] testPoint = data[picks] trainPoint = data[np.setdiff1d(range(0, len(data)), picks)] trainLabel = labels[np.setdiff1d(range(0, len(data)), picks)] trainLabel = trainLabel.tolist() trainPoint = trainPoint.tolist() prob = svmutil.svm_problem(trainLabel, trainPoint) param = svmutil.svm_parameter('-t 3 -c 4 -b 1 -q') testLabel = testLabel.tolist() testPoint = testPoint.tolist() m = svmutil.svm_train(prob, param) svmutil.svm_save_model('n.model', m) p_label, p_acc, p_val = svmutil.svm_predict(testLabel, testPoint, m, '-b 1') prabs.append(p_acc[0]) print sum(prabs) / float(len(prabs)) print 'std' + str(np.std(prabs)) return sum(prabs) / float(len(prabs))
def TrainSvmLinear2(Y, X, sweep_c=range(-2,18)): num_positives = float(Y.count(1)) num_negatives = float(Y.count(-1)) best_c = -1 best_acc = -1 for c_pow in sweep_c: current_c = np.power(2.0,c_pow) param = svm.svm_parameter('-t 0 -c %f -w-1 %f -w1 %f -q' % (current_c, 100/num_negatives, 100/num_positives)) current_pos_acc, current_neg_acc = CrossValidate(Y, X, param) current_acc = current_pos_acc print '%f, %f, %f' % (current_c, current_acc, current_neg_acc) if best_acc < current_acc: best_acc = current_acc best_c = current_c prob = svm.svm_problem(Y,X) param = svm.svm_parameter('-t 0 -c %f -w-1 %f -w1 %f -q' % (best_c, 100/num_negatives, 100/num_positives)) svm_model = svm.svm_train(prob, param) p_labs, p_acc, p_vals = svm.svm_predict(Y, X, svm_model, '-q') return svm_model
def get(self, tag="貓咪", max_tag_id=None): if tag == "": tag = "貓咪" p = Pool(10) if self.prefix == "ajax": medias, next_ = util.search_by_tag(tag, 3, max_tag_id) else: medias, next_ = util.search_by_tag(tag, 5, max_tag_id) fs = p.map(util.features, medias) p_label, _, _ = libsvm.svm_predict([1] * len(fs), fs, model) for (m, f) in zip(medias, fs): print(m["caption"]["text"]) print(f) if self.prefix == "ajax": medias = map(lambda (m, l): Media(m, l).__dict__, zip(medias, p_label)) self.write(json.dumps({ "max_tag_id": next_, "medias": medias })) else: medias = map(lambda (m, l): Media(m, l), zip(medias, p_label)) if self.prefix == "demo1": self.render("demo1.html", medias=medias, tag_name=tag, max_tag_id=next_) elif self.prefix == "demo2": self.render("demo2.html", medias=medias, tag_name=tag, max_tag_id=next_) else: self.render("main.html", medias=medias, tag_name=tag, max_tag_id=next_) p.close() p.join()
def test(test_file, model_file, fold_num, mapping=None, multilabel=None, debug=False): ''' Returns predicted labels, prediction values and the as the the test labels (potentially remapped). Requires a test instance file and a corresponding model file. Remaps the labels in the test file (optional), classifies the test instances against the model. ''' if multilabel: temp_labels, instances = alt_read_problem(test_file) temp_labels = [[mapping[l] for l in label] for label in temp_labels] labels = [] for temp_labs in temp_labels: if multilabel[1] in temp_labs: labels.append(multilabel[1]) else: assert len(set(temp_labs)) == 1, "Something appears to be wrong with the intermediate mapping. There's still more than one label present for an instance: {0}".format(temp_labs) labels.append([l for l in temp_labs if l != multilabel[1]][0]) else: labels, instances = svm_read_problem(test_file) labels = reMap(labels, mapping) # Exclude instances which have 0 as their label labels, instances = zip(*[(label, instance) for label, instance in zip(labels, instances) if label != 0]) if debug: with open(os.path.basename(test_file) + '.remap', 'w') as fout: for label, instance in zip(labels, instances): output = '{0} '.format(str(label)) for idx, val in instance.items(): output += '{0}:{1} '.format(str(idx), str(val)) output = output.strip() + '\n' fout.write(output) model = svm_load_model(model_file) print '---testing' if classifier == 'liblinear': pred_labels, ACC, pred_values, label_order = svm_predict(labels, instances, model) elif classifier == 'libsvm': pred_labels, (ACC, MSC, SCC), pred_values = svm_predict(labels, instances, model, options='-b 1') label_order = model.get_labels() return pred_labels, pred_values, label_order, labels
def predict(V, yy): m = svmutil.svm_load_model("sample.model") x = [list(map(lambda z: z * 10, list(t))) for t in V] y = [0 if t < 0 else 1 for t in yy] p_label, p_acc, p_val = svmutil.svm_predict(y, x, m) print(y) print(p_label) print(x[10])
def update(self, data): if not data[dc.IMAGE] is None: self.last_frame_RGB = np.copy(data[dc.IMAGE]) # rotate image according to VIO rows, cols, ch = self.last_frame_RGB.shape M = cv2.getRotationMatrix2D( (cols / 2, rows / 2), 90 + data[dc.CAMERA_ROTATION][2] * 180 / math.pi, 1) rotated_frame = cv2.warpAffine(self.last_frame_RGB, M, (cols, rows)) gray = cv2.cvtColor(rotated_frame, cv2.COLOR_BGR2GRAY) # cv2.imshow("SIGNINPUT", self.last_frame_RGB ) cv2.imshow("ROT", rotated_frame) # cv2.waitKey(1) # print(data[dc.CAMERA_ROTATION]*180/3.14) rois_stage1 = self.cascade_class.detectMultiScale(gray, 1.25, minNeighbors=1, minSize=(36, 24), maxSize=(180, 240)) if len(rois_stage1) > 0: self.roi = None self.sign_height = -1 best_prob = 0 for r in rois_stage1: r_hog = self.compute_hog(gray, r, self.hog) p_labels, p_acc, p_vals = svmutil.svm_predict( [1], r_hog.transpose().tolist(), self.svm, '-b 1') print p_vals if p_vals[0][0] > 0.5: # cv2.rectangle(output_img, (r[0], r[1]), (r[0] + r[2], r[1] + r[3]), (255, 0, 0), 2) # cv2.imshow("Stage2", output_img) if p_vals[0][0] > best_prob: best_prob = p_vals[0][0] self.roi = r if self.roi is not None: self.sign_height = self._get_sign_height(rotated_frame)
def main(): timer = timer_c() res = numpy.array([], dtype='i') mat = numpy.empty((0, len(key) * ntracks), dtype='f') with open('../data/dbf/2017.dbf', 'r') as dbf: history = json.load(dbf) seven = history['layout'].index('PCHG') print 'seven at %d.' % (seven) first = None for code, table in history['record'].iteritems(): if len(table) < 180: continue if first is None or first < table[89][0]: first = table[89][0] for code, table in history['record'].iteritems(): # skip the stock has less than 180 rows of record if len(table) < 180: continue # skip the stock has suspended in 90 trading days if table[89][0] != first: continue # form mat & res for i in xrange(nsample): # mat++ row = [] for r in xrange(i + 1, i + ntracks + 1): for offset in key: row.append(table[r][offset]) mat = numpy.append(mat, numpy.array([row], dtype='f'), axis=0) # res++ hg = table[i][seven] + 10.0 hg = min(hg, 19.99) hg = max(hg, 0.0) res = numpy.append(res, int(hg / (20.0 / nlabels))) print 'mat & res loaded successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # timer.reset() lsvm = svmutil.svm_train(res.tolist(), mat.tolist(), '-s 0 -t 0 -g 1.00 -c 1000000.00 -b 0 -q') svmutil.svm_save_model('temp.svm', lsvm) print 'svm trained successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) p_labels, p_acc, p_vals = svmutil.svm_predict(res.tolist(), mat.tolist(), lsvm, '')
def calculate_slack_variables(self, model): """This method calculates from the given SVM model the related slack variables for classification.""" self.t = [] self.num_sv = 0 self.num_nsv = 0 self.inner_margin = 0 self.ti = [] dropped_samples = [] dropped_labels = [] for i in range(self.num_samples): # ctype libsvm bindings try: p = svmutil.svm_predict( [0], [map(float, list(self.samples[i - self.num_nsv]))], model)[2][0][0] except: self._log( "Classification failed. Did you specify the parameters correctly?", level=logging.ERROR) p = 0 if model.get_labels() == [0, 1]: p = -p p = 2 * (self.labels[i - self.num_nsv] - 0.5) * p if p > 1: self.t.append(0) self.ti.append(0) dropped_samples.append(self.samples.pop(i - self.num_nsv)) dropped_labels.append(self.labels.pop(i - self.num_nsv)) self.num_nsv += 1 else: self.t.append(1 - p) self.num_sv += 1 if 1 - p < 1e-5: p = 1 self.ti.append(0) else: self.ti.append(1 - p) self.inner_margin += 1 #if self.store_all_samples: for i in range(len(dropped_samples)): self.samples.append(dropped_samples[i]) self.labels.append(dropped_labels[i]) del (dropped_samples) del (dropped_labels)
def prob15(): trainX, testX, trainy, testy = readdat() mul_label_2_bin(trainy, testy, 0) m = svmutil.svm_train(trainy, trainX, '-s 0 -t 0 -c 0.01 -h 0') p_label, p_acc, p_val = svmutil.svm_predict(testy, testX, m) sv_coef = [sv_c[0] for sv_c in m.get_sv_coef()] sv = [] for v in m.get_SV(): t = [] for i in range(1, 3): if i in v: t.append(v[i]) else: t.append(0) sv.append(t) w = np.dot(np.array(sv).T, np.array(sv_coef)) print("prob15: |w| = %f" % np.sqrt(w[0] ** 2 + w[1] ** 2))
def predict(trainFileName, testFileName, cgp={ 'c': 1024, 'g': 16, 'p': 0.015625 }): from svmutil import svm_read_problem, svm_train, svm_predict y, x = svm_read_problem(trainFileName) yt, xt = svm_read_problem(testFileName) model = svm_train( y, x, '-s 4 -t 2 -c %f -g %f -p %f' % (cgp['c'], cgp['g'], cgp['p'])) p_label, p_acc, p_val = svm_predict(yt, xt, model) return (p_label, p_acc, p_val)
def compute(p, op, e, target, problem, story, order): vec = makesets.vector(p, e, problem, story, target) op_label, op_acc, op_val = svmutil.svm_predict([-1], [vec], multi, '-q -b 1') op_val = op_val[0] if op == '+': val = op_val[0] if op == '-': val = op_val[1] if op == '*': val = op_val[2] if op == '/': val = op_val[3] if op == '=': val = op_val[1] c = makesets.combine(p[1], e[1], op) return (val, c, op_val)
def GetCode_login(picpath):#识别登陆验证码 code=[];model=svmutil.svm_load_model(modelpath_login_register)#打开训练文件 im=Image.open(picpath).convert('L')#打开图片,并转化为灰度图 im=im.point([0]*150+[1]*(256-150),'1')#二值化 im=de_noise(im)#降噪 Imgs=Crop_login(im)#切割后的图片列表 for oldimg in Imgs: img=createNewimage(oldimg,oldimg.size) pixel_cnt_list=GetFeature(img,'img') tempath=os.path.join(os.getcwd(),'temp.txt')#临时文件,用于存储将要识别的图片的特征 with open(tempath,'w') as f: f.writelines(GetFeatureStr(pixel_cnt_list,0)) y0,x0=svmutil.svm_read_problem(tempath) os.remove(tempath) p_label,p_acc,p_val=svmutil.svm_predict(y0,x0,model,'-q') code.append(int(p_label[0])) code=''.join(list(map(chr,code))) return code
def run(tweek, history): logstr = '' # w, x, y = history.grab(nsample, ntracks, tweek + 1) x = xfilter(x) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) print 'svm trained successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) # w, x, y = history.grab(1, ntracks, tweek) x = xfilter(x) timer.reset() p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec.' % (str( float('{0:.3f}'.format(timer.lag())))) pos = np.argmax(np.array(p_labels)) return p_labels[pos], y[pos]
def predict(self, document): """ tag the new data basde on a given model :param document: document object :returns: list of predictions """ logger.info('Start to predict') y,x = self.fit(document) if Svm._auto_config: file_name = self._auto_load('file', self.model_file) m = svmutil.svm_load_model(file_name) pkg_resources.cleanup_resources() else : m = svmutil.svm_load_model(self.model_file) y_pred, p_acc, p_val = svmutil.svm_predict(y, x, m, "-q") return y_pred
def test_audio_svm(path, start, length, FPS, machine): i = 0 v = 0 for this_start in range(start, start + length, 30): j = 0 test_list_mfcc = [] result = [] print(this_start) audio, video = load_movie(path, this_start, 30, FPS) audio_array = audio.to_soundarray() audio_array = (audio_array[:, 0] + audio_array[:, 1]) / 2 mfcc_structure = psf.mfcc(audio_array, samplerate=16000, winlen=0.576, winstep=0.576, nfft=16384, numcep=26, nfilt=52) mfcc_structure = utils.normalize(mfcc_structure) mfcc_structure = np.asarray(mfcc_structure) r = int(len(mfcc_structure[:,0])) for k in range(0, r): s = mfcc_structure[k,:] test_list_mfcc.append(s) test_list_mfcc = vectorize(test_list_mfcc) for t in test_list_mfcc: t = t.reshape(1, 26) print(t) label, acc, val = svm.svm_predict([], t, machine, '-b 1') result.append(label) for res in result: res = res[0] i = i + 1 j = j + 1 if(res < 0.5): print("Segment " + str(i) + " is non-violent.") video.save_frame("Output/SVM/Non-Violent/Image/frame" + str(i) +".jpeg", t = (j - 1) * 0.566) wav.write("Output/SVM/Non-Violent/Sound/frame" + str(i) + ".wav", FPS, audio_array[int((j - 1) * FPS * 0.566):int(j * FPS * 0.566)]) else: v = v + 1 print("Segment " + str(i) + " is violent.") video.save_frame("Output/SVM/Violent/Image/frame" + str(i) +".jpeg", t = (j - 1) * 0.566) wav.write("Output/SVM/Violent/Sound/frame" + str(i) + ".wav", FPS, audio_array[int((j - 1) * FPS * 0.566):int(j * FPS * 0.566)]) video.close() audio.close() print("Amount of violence: " + str(v / i * 100) + "%")
def run(history, tweek, pref): # timer = timer_c() lsvm = svmutil.svm_load_model('temp.svm') print 'svm loaded successfully in %s sec.' % (str(float('{0:.3f}'.format(timer.lag())))) # w, x, y = history.grab(1, ntracks, tweek-1, pref) x = xfilter(x) timer.reset() p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec with %d samples.' % (str(float('{0:.3f}'.format(timer.lag()))), len(w)) plotdraw(p_labels, y) # foo = [] for i in xrange(len(w)): foo.append((w[i], p_labels[i], y[i])) foo.sort(key = lambda tup: (tup[1])) for row in foo: print '%s y\' = %.6f, y = %.6f' % row
def test_number_svm_model(number): """使用测试集测试模型 :param number: 要测试的数字 :type number int """ feature_file = test_feature_file.format(number) yt, xt = svm_read_problem(feature_file) print "yt: {}".format(yt) model = svm_load_model(svm_model_path) p_label, p_acc, p_val = svm_predict(yt, xt, model) for cnt, item in enumerate(p_label): print "result: {}".format(item), # 每十个打印一行 if cnt % 10 == 0: print
def run(tday, history): logstr = '' # w, x, y = history.grab(nsample, ntracks, tday+1) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) #svmutil.svm_save_model('benchmark.svm', lsvm) #lsvm = svmutil.svm_load_model('benchmark.svm') print 'svm trained successfully in %s sec.' % (str(float('{0:.3f}'.format(timer.lag())))) # timer = timer_c() w, x, y = history.grab(1, ntracks, tday) p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') print 'svm predicted successfully in %s sec.' % (str(float('{0:.3f}'.format(timer.lag())))) foo = [] for i in xrange(len(y)): foo.append((p_labels[i], y[i])) foo.sort(key = lambda tup: (-tup[0])) return [row[1] for row in foo]
def svm_encontrar_melhor(X, Y, Xval, Yval, C, G, K): """ Retorna o melhor valor para os parâmetros custo e gamma do SVM radial. Parâmetros ---------- X : matriz com os dados de treinamento y : vetor com classes de cada dados de treinamento Xval : matriz com os dados de validação yval : vetor com as classes dos dados de validação C : lista com valores para custo G : lista com valores para gamma K : inteiro indicando o kernel a ser usado Retorno ------- custo, gamma : os melhores valores para os parêmetros custo e gamma. """ #inicializa as variáveis que deverão ser retornadas pela função custo = 1000 gamma = 1000 acuracia = 0.0 #Acc acuracia_atual = 0.0 #Actual acc for cost in C: for gamm in G: model = svm_train(Y, X, '-c %f -t %d -g %f -q' % (cost, K, gamm)) acuracia_atual = svm_predict(Yval, Xval, model)[1][0] if acuracia < acuracia_atual: acuracia = acuracia_atual custo = cost gamma = gamm return custo, gamma
def Problem12(X_train, y_train): '''the input is same as the problem 11 but in this problem , we need set the digit 8 as positive and others are negative and evaluate the ein as the param c changes''' label_train = list(2 * (np.array(y_train) == 8) - 1) #ein=list() nSV = list() Q = 2 coef0 = 1 for t in range(-5, 5, 2): c = 10**t model = svmutil.svm_train( label_train, X_train, '-t 1 -g 1 -c %s -d %s -r %s' % (c, Q, coef0)) predict_label, _, _ = svmutil.svm_predict(label_train, X_train, model) error = np.mean(np.array(predict_label) != np.array(label_train)) print(error) nSV.append(model.get_nr_sv()) return nSV
def _evaluation_test_helper_no_probability(self, labels, allow_slow): # Generate some random data. # This unit test should not rely on scikit learn for test data. x, y = [], [] random.seed(42) for _ in range(50): x.append([ random.gauss(200, 30), random.gauss(-100, 22), random.gauss(100, 42) ]) y.append(random.choice(labels)) # make sure first label is seen first, second is seen second, and so on. for i, val in enumerate(labels): y[i] = val column_names = ["x1", "x2", "x3"] prob = svmutil.svm_problem(y, x) df = pd.DataFrame(x, columns=column_names) for param1 in self.non_kernel_parameters: for param2 in self.kernel_parameters: param_str = " ".join([self.base_param, param1, param2]) print("PARAMS: ", param_str) param = svm_parameter(param_str) model = svm_train(prob, param) # Get predictions with probabilities as dictionaries (df["prediction"], _, _) = svm_predict(y, x, model, " -q") spec = libsvm.convert(model, column_names, "target") if _is_macos() and _macos_version() >= (10, 13): metrics = evaluate_classifier(spec, df, verbose=False) self.assertEquals(metrics["num_errors"], 0) if not allow_slow: break if not allow_slow: break
def _evaluation_test_helper_with_probability(self, labels, allow_slow): import copy df = pd.DataFrame(self.x, columns=self.column_names) y = copy.copy(self.y) for i, val in enumerate(labels): y[i] = val probability_param = "-b 1" for param1 in self.non_kernel_parameters: for param2 in self.kernel_parameters: param_str = " ".join( [self.base_param, param1, param2, probability_param]) # print("PARAMS: ", param_str) param = svm_parameter(param_str) model = svm_train(self.prob, param) # Get predictions with probabilities as dictionaries (df["prediction"], _, probability_lists) = svm_predict(y, self.x, model, probability_param + " -q") probability_dicts = [ dict(zip([1, 2], cur_vals)) for cur_vals in probability_lists ] df["probabilities"] = probability_dicts spec = libsvm.convert(model, self.column_names, "target", "probabilities") if _is_macos() and _macos_version() >= (10, 13): metrics = evaluate_classifier_with_probabilities( spec, df, verbose=False) self.assertEquals(metrics["num_key_mismatch"], 0) self.assertLess(metrics["max_probability_error"], 0.00001) if not allow_slow: break if not allow_slow: break
def valid(self, datasets, opt, opp, method=fold, part_ids=None, seed=None, test_data=None): if seed is None: # If seed is not set. UNIX time is used as seed. seed = time.time() saving_seed = "%s/log/%s.log.seed" % (self._dir, self._name) with open(saving_seed, "w") as fp: # Save used seed value. fp.write("seed:%f\n" % seed) if part_ids is None: part_ids = datasets.pids groups = [(test, train) for test, train in method(part_ids, seed=seed)] for cnt, pdtsts in enumerate(groups): # cnt is number of cluster. if test_data is None: test = False ltest, dtest, itest = test2svm_prob(datasets.mkTest(pdtsts[0])) else: test = True ltest, dtest, itest = test2svm_prob( test_data.mkTest(test_data.pids)) print "start %s validation" % (cnt) ptrn, itrain = train2svm_prob(datasets.mkTrain(pdtsts[1])) #opt = svm.svm_parameter(opt) model = svmutil.svm_train(ptrn, opt) plbl, pacc, pval = svmutil.svm_predict(ltest, dtest, model, opp) # create saving direcotry #self._mkdir(cnt) # create log files self._save_log(itest, plbl, pval, cnt, test) model_name = "%s/model/%s.model.%s" % (self._dir, self._name, cnt)
def run(history, tweek, pref): # w, x, y = history.grab(nsample, ntracks, tweek, pref) x = xfilter(x) timer = timer_c() lsvm = svmutil.svm_train(y, x, options) #print 'svm trained successfully in %s sec with %d samples.' % (str(float('{0:.3f}'.format(timer.lag()))), len(w)) # w, x, y = history.grab(1, ntracks, tweek - 1, pref) x = xfilter(x) timer.reset() p_labels, p_acc, p_vals = svmutil.svm_predict(y, x, lsvm, '') #print 'svm predicted successfully in %s sec with %d samples.' % (str(float('{0:.3f}'.format(timer.lag()))), len(w)) # foo = [] for i in xrange(len(w)): foo.append((w[i], p_labels[i], y[i])) foo.sort(key=lambda tup: (-tup[1])) for i in xrange(3): print '%s y\' = %.6f, y = %.6f' % foo[i]
def test_input_names(self): data = load_boston() df = pd.DataFrame({'input': data['data'].tolist()}) # Default values spec = libsvm.convert(self.libsvm_model) (df['prediction'], _, _) = svmutil.svm_predict(data['target'], data['data'].tolist(), self.libsvm_model) metrics = evaluate_regressor(spec, df) self.assertAlmostEquals(metrics['max_error'], 0) # One extra parameters. This is legal/possible. num_inputs = len(data['data'][0]) spec = libsvm.convert(self.libsvm_model, input_length=num_inputs+1) # Not enought input names. input_names=['this', 'is', 'not', 'enought', 'names'] with self.assertRaises(ValueError): libsvm.convert(self.libsvm_model, input_names=input_names) with self.assertRaises(ValueError): libsvm.convert(self.libsvm_model, input_length=num_inputs-1)
def _evaluation_test_helper_with_probability(self, labels, allow_slow): import copy df = pd.DataFrame(self.x, columns=self.column_names) y = copy.copy(self.y) for i, val in enumerate(labels): y[i] = val probability_param = '-b 1' for param1 in self.non_kernel_parameters: for param2 in self.kernel_parameters: param_str = ' '.join( [self.base_param, param1, param2, probability_param]) # print("PARAMS: ", param_str) param = svm_parameter(param_str) model = svm_train(self.prob, param) # Get predictions with probabilities as dictionaries (df['prediction'], _, probability_lists) = svm_predict(y, self.x, model, probability_param + ' -q') probability_dicts = [ dict(zip([1, 2], cur_vals)) for cur_vals in probability_lists ] df['probabilities'] = probability_dicts spec = libsvm.convert(model, self.column_names, 'target', 'probabilities') metrics = evaluate_classifier_with_probabilities(spec, df, verbose=False) self.assertEquals(metrics['num_key_mismatch'], 0) self.assertLess(metrics['max_probability_error'], 0.00001) if not allow_slow: break if not allow_slow: break
def compute_auc(gram_matrix, data, k=10, C=1.0): kv = cross_validation.StratifiedKFold(labels, n_folds=k) s = 0.0 for train_index, test_index in kv: gm_train = gram_matrix[train_index, :] gm_train = gm_train[:, train_index] data_train = data[train_index] # libSVM wants the distances from test instances to all train instances as input # see http://stackoverflow.com/questions/10978261/libsvm-precomputed-kernels gm_test = gram_matrix[test_index, :] gm_test = gm_test[:, train_index] #! data_test = data[test_index] # Have to use libsvm directly here, because of a bug in sklearn with precomputed gram matrices x = [] for i in range(len(gm_train)): l = gm_train[i].tolist() l.insert(0, i + 1) x.append(l) prob = svmutil.svm_problem(data_train.tolist(), x, isKernel=True) param = svmutil.svm_parameter("-t 4 -c %.410f -q" % C) m = svmutil.svm_train(prob, param) xx = [] for i in range(len(gm_test)): t = gm_test[i].tolist() t.insert(0, i + 1) xx.append(t) p_label, p_acc, p_val = svmutil.svm_predict(data_test.tolist(), xx, m) fpr, tpr, thresholds = roc_curve(data_test, p_val, pos_label=1.0) AUC = roc_auc_score(data_test, p_val) s += AUC return s / k
def svm_with_diff_kernel(train_label, train_data, test_label, test_data): ''' Use 'svm_train' function with training label, data and different kernel to train a svm classify model. Then apply the trained model on testing label and data. The kernel you need to try is listing as follow: 1. linear kernel 2. polynomial kernel 3. radial basis function kernel Please keep other parameter options as default. No return value is needed ''' m = svm_train(train_label, train_data, '-t 2') """ m = svm_train(train_label, train_data, '-t 0') m = svm_train(train_label, train_data, '-t 1') """ p_label, p_acc, p_val = svm_predict(test_label, test_data, m)
def spiderweb_identify(self, model_name): model = lib.svm_load_model(model_name) regions = np.where(self.im_bw == 255) moment1_feature = self.moment1_mat[regions] moment2_feature = self.moment2_mat[regions] intensity_feature = self.img_eq[regions] # intensity_features = intensity_feature/255.0 features = np.vstack( (moment1_feature, moment2_feature, intensity_feature)) features = features.T features = features.tolist() labels = [1] * len(features) p_label, _, _ = lib.svm_predict(labels, features, model, '-q') self.labels = p_label
def getPredictionStats(rddEntry): if len(rddEntry[1]) < 2: return (0, 0, 0, 0) # Options to be passed to SVM for training svm_options = '-s 0 -t 2 -c 10' model = svm_train(rddEntry[1][0][0], rddEntry[1][0][1], svm_options) labels, acc, values = svm_predict(rddEntry[1][1][0], rddEntry[1][1][1], model) numFailedPredictions, expectedFailedPredictions, numFalseAlarms, numGoodRecords = 0, 0, 0, 0 for idx, prediction in enumerate(labels): if rddEntry[1][1][0][idx] == 1: expectedFailedPredictions += 1 if prediction == rddEntry[1][1][0][idx]: numFailedPredictions += 1 else: numGoodRecords += 1 if prediction != rddEntry[1][1][0][idx]: numFalseAlarms += 1 return (numFailedPredictions, expectedFailedPredictions, numFalseAlarms, numGoodRecords)
def classify(self, args): """ classifies the computed video descriptors into one of the activity classes. """ test_inputs = np.load('test_data.npz')['inputs'] test_labels = np.load('test_data.npz')[ 'outputs'] #dummy labels (all are ones) k_train, k_test = \ compute_kernel_matrices(self.train_inputs, test_inputs) m = svmutil.svm_train(self.train_outputs.tolist(), k_train.tolist(), ' -t 4 -q -s 0 -b 1 -c %f' % 100) pl, pac, p_val = svmutil.svm_predict(test_labels.tolist(), k_test.tolist(), m, '-b 1') m_labels = svmutil.get_labels(m) probs = np.array(p_val)[:, np.array(m_labels).argsort()] probs_crr = np.concatenate( (probs[:, 0:4], probs[:, 5:7], probs[:, 4:5]), 1) labels = [ 'Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral' ] #flist = os.listdir(args['videos_path']) flist = self.filelist f = open('activity_recognition_test_results.txt', 'wb') probs_crr = np.around(probs_crr, decimals=3) pl_crr = probs_crr.argmax(1) for i in range(len(flist)): f.write(flist[i][:-4] + '\t' + labels[pl_crr[i]] + '\t' + '\t'.join(map(str, probs_crr[i])) + '\n') f.close()
def prob1617(): Ein = [] alpha = [] for i in range(0, 10, 2): trainX, testX, trainy, testy = readdat() mul_label_2_bin(trainy, testy, i) m = svmutil.svm_train(trainy, trainX, '-s 0 -t 1 -c 0.01 -d 2 -g 1 -r 1 -h 0') p_label, p_acc, p_val = svmutil.svm_predict(testy, testX, m) sigma_alpha = sum([abs(sv_c[0]) for sv_c in m.get_sv_coef()]) Ein.append("prob16: %d v.s. not %d, E_in = %f%%" % (i, i, 100.0-p_acc[0])) alpha.append("prob17: %d v.s. not %d, sigma_alpha = %f" % (i, i, sigma_alpha)) for s in Ein: print(s) for s in alpha: print(s)
def svm_predict(problem_filepath, model_filepath): """ Using LibSVM to predict result of a problem Returns ------- (ids, labels) """ # Reading a problem ids, x = svmutil.svm_read_problem(problem_filepath) print "len(x) = ", len(x) # Preparing a model model = svmutil.svm_load_model(model_filepath) # Predicting y = [-2] * len(x) p_label, p_acc, p_val = svmutil.svm_predict(y, x, model) return (ids, p_label)
def get(self, tag="貓咪", max_tag_id=None): if tag == "": tag = "貓咪" p = Pool(10) if self.prefix == "ajax": medias, next_ = util.search_by_tag(tag, 3, max_tag_id) else: medias, next_ = util.search_by_tag(tag, 5, max_tag_id) fs = p.map(util.features, medias) p_label, _, _ = libsvm.svm_predict([1] * len(fs), fs, model) for (m, f) in zip(medias, fs): print(m["caption"]["text"]) print(f) if self.prefix == "ajax": medias = map(lambda (m, l): Media(m, l).__dict__, zip(medias, p_label)) self.write(json.dumps({"max_tag_id": next_, "medias": medias})) else: medias = map(lambda (m, l): Media(m, l), zip(medias, p_label)) if self.prefix == "demo1": self.render("demo1.html", medias=medias, tag_name=tag, max_tag_id=next_) elif self.prefix == "demo2": self.render("demo2.html", medias=medias, tag_name=tag, max_tag_id=next_) else: self.render("main.html", medias=medias, tag_name=tag, max_tag_id=next_) p.close() p.join()
def svm_with_diff_kernel(train_label, train_data, test_label, test_data): ''' Use 'svm_train' function with training label, data and different kernel to train a svm classify model. Then apply the trained model on testing label and data. The kernel you need to try is listing as follow: 1. linear kernel 2. polynomial kernel 3. radial basis function kernel Please keep other parameter options as default. No return value is needed ''' ''' -t kernel: set type of kernel function (default 3). 0: linear kernel; 1: polynomial kernel; 2: radial basis function kernel. ''' k = [0, 1, 2] for kernel in k: libsvm_options = '-t ' + str(kernel) model = svm_train(train_label, train_data, libsvm_options) predicted_label, test_acc, decision_values = svm_predict( test_label, test_data, model)