本文整理汇总了Python中svmutil.svm_predict函数的典型用法代码示例。如果您正苦于以下问题:Python svm_predict函数的具体用法?Python svm_predict怎么用?Python svm_predict使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了svm_predict函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test
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
示例2: TrainSvmLinear
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
示例3: cons_train_sample_for_cla
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()
示例4: test_model
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) + '%'
示例5: CrossValidate
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)
示例6: predict
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)
示例7: classify
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 "体育"
示例8: kfold
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))
示例9: getSamePeptideClusters
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
示例10: TrainSvmLinear2
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
示例11: calc
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
示例12: train_test_model
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
示例13: detectDoor
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)
示例14: valid
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)
示例15: TrainSvmRbf2
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