本文整理汇总了Python中svm.svm_parameter函数的典型用法代码示例。如果您正苦于以下问题:Python svm_parameter函数的具体用法?Python svm_parameter怎么用?Python svm_parameter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了svm_parameter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, data_dictionary, model_target, kernel=LINEAR, cv_segments=10, **args):
#Create an SVM model object
#Check to see if a threshold has been specified in the function's arguments
try: self.threshold = args['threshold']
except KeyError: self.threshold=2.3711 # if there is no 'threshold' key, then use the default (2.3711)
#Store some object data
model_dict = deepcopy(data_dictionary)
self.model_target = model_target
self.folds = cv_segments
#Label the exceedances in the training set.
model_dict[model_target] = self.Assign_Labels(model_dict[model_target])
#Extract the training labels and training set
self.training_labels = model_dict.pop(model_target)
self.training_set = np.transpose(model_dict.values())
self.headers = model_dict.keys()
#Scale the covariates to [-1,1]
self.Scale_Covariates()
#Generate an SVM model.
self.svm_problem = svm.svm_problem(self.training_labels, self.training_set)
self.svm_params = {'kernel_type' : kernel, 'weight_label' : [0,1], 'weight' : [10,1]}
self.model=svm.svm_model(self.svm_problem, svm.svm_parameter(**self.svm_params))
#Use cross-validation to find the best number of components in the model.
self.Select_Linear_Model(-5, 10)
#Rebuild the model, calculating the probabilities of class membership
self.svm_params['probability']=1
self.model=svm.svm_model(self.svm_problem, svm.svm_parameter(**self.svm_params))
示例2: train_test
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)
示例3: iqr_model_train
def iqr_model_train(matrix_kernel_train, labels_train, idx2clipid,
svm_para = '-w1 50 -t 4 -b 1 -c 1'):
"""
Light-weighted SVM learning module for online IQR
@param matrix_kernel_train: n-by-n square numpy array with kernel values
between training data
@param labels_train: row-wise labels of training data (1 or True indicates
positive, 0 or False otherwise
@param idx2clipid: idx2clipid(row_idx) returns the clipid for the 0-base row
in matrix
@param svm_para: (optional) SVM learning parameter
@rtype: dictionary with 'clipids_SV': list of clipids for support vectors
@return: output as a dictionary with 'clipids_SV'
"""
log = logging.getLogger('iqr_model_train')
# set training inputs
matrix_kernel_train = np.vstack((np.arange(1, len(matrix_kernel_train)+1),
matrix_kernel_train)).T
log.debug("Done matrix_kernel_train")
problem = svm.svm_problem(labels_train.tolist(), matrix_kernel_train.tolist(), isKernel=True)
log.debug("Done problem")
svm_param = svm.svm_parameter(svm_para)
log.debug("Done svm_param")
# train model
model = svmutil.svm_train(problem, svm_param)
log.debug("Done train model")
# release memory
del problem
del svm_param
log.debug("Done release memory")
# check learning failure
if model.l == 0:
raise Exception('svm model learning failure')
log.debug("Done checking learning failure (no failure)")
n_SVs = model.l
clipids_SVs = []
idxs_train_SVs = svmtools.get_SV_idxs_nonlinear_svm(model)
for i in range(n_SVs):
_idx_1base = idxs_train_SVs[i]
_idx_0base = _idx_1base - 1
clipids_SVs.append(idx2clipid[_idx_0base])
model.SV[i][0].value = i+1 # within SVM model, index needs to be 1-base
log.debug("Done collecting support vector IDs")
#svmutil.svm_save_model(filepath_model, model)
output = dict()
output['model'] = model
output['clipids_SVs'] = clipids_SVs
return output
示例4: train
def train(self,labels,data):
'''
Train the classifier.
@param labels: A list of class labels.
@param data: A 2D array or list of feature vectors. One feature vector per row.
'''
# Check the types and convert to np arrays
if isinstance(data,list) or isinstance(data,tuple):
data = np.array(data,dtype=np.double)
labels = np.array(labels,dtype=np.double)
# Preprocess the data
labels,data = self._preprocessor.train(labels,data)
labels,data = self._label_scale.train(labels,data)
# Create the svm parameter data and problem description
param = svm.svm_parameter(svm_type=svm.EPSILON_SVR,kernel_type = svm.RBF, p = self._epsilon, gamma=self._gamma)
prob = svm.svm_problem(labels.tolist(),data.tolist())
# train the svm
self._model = svm.svm_model(prob, param)
示例5: do_one_cv_classify_predeffolds_multi
def do_one_cv_classify_predeffolds_multi(theinput):
c = theinput[0]
gamma = theinput[1]
nf = theinput[2]
output = theinput[3]
input = theinput[4]
useprob = theinput[5]
fold_start = theinput[6]
param = svm.svm_parameter('-c %g -g %g -b %d' % (c,gamma,int(useprob)))
prob = svm.svm_problem(output, input)
target = (c_double * prob.l)()
posclass = output[0]
fold_start_p = (c_int *len(fold_start))()
for i in xrange(len(fold_start)):
fold_start_p[i] = fold_start[i]
libsvm.svm_cross_validation_labeltargets(prob, fold_start_p,param, nf, target)
acc = len([i for i in xrange(len(output)) if output[i] == target[i]])*1.0/prob.l
del target
del fold_start_p
return acc
示例6: train
def train(self, c, g, probability=True, compensation=True,
path=None, filename=None, save=True):
if filename is None:
filename = os.path.splitext(self.getOption('strArffFileName'))[0]
filename += '.model'
if path is None:
path = self.dctEnvPaths['data']
param = svm.svm_parameter(kernel_type=svm.RBF,
C=c, gamma=g,
probability=1 if probability else 0)
labels, samples = self.getData(normalize=True)
# because we train the SVM with dict we need to redefine the zero-insert
self.hasZeroInsert = False
if not self.oClassifier is None:
self.oClassifier.setOption('hasZeroInsert', True)
if compensation:
weight, weight_label = self._calculateCompensation(labels)
param.weight = weight
param.weight_label = weight_label
param.nr_weight = len(weight)
problem = svm.svm_problem(labels, samples)
model = svm.svm_model(problem, param)
if save:
model.save(os.path.join(path, filename))
return problem, model
示例7: do_one_cv_classify
def do_one_cv_classify(theinput):
c = theinput[0]
gamma = theinput[1]
nf = theinput[2]
output = theinput[3]
input = theinput[4]
useprob = theinput[5]
perfmetric = theinput[6]
param = svm.svm_parameter('-c %g -g %g -b %d' % (c,gamma,int(useprob)))
prob = svm.svm_problem(output, input)
target = (c_double * prob.l)()
posclass = output[0]
fold_start = (c_int *1)();
fold_start[0] = -1;
libsvm.svm_cross_validation(prob, fold_start, param, nf, target)
ys = prob.y[:prob.l]
db = array([[ys[i],target[i]] for i in range(prob.l)])
del target
neg = len([x for x in ys if x != posclass])
pos = prob.l-neg
[topacc,topphi,minfpfnratio,topf1,auc,optbias] = optimize_results(db,neg,pos,posval,perfmetric)
return topacc,topphi,minfpfnratio,topf1,auc,optbias
示例8: svm
def svm(y,K,**param_kw):
"""
Solve the SVM problem. Return ``(alpha, b)``
`y`
labels
`K`
precopmuted kernel matrix
Additional keyword arguments are passed on as svm parameters to
the model.
The wrapper is needed to precondition the precomputed matrix for
use with libsvm, and to extract the model parameters and convert
them into the canonical weight vector plus scalar offset. Normally
libsvm hides these model paramters, preferring instead to provide
a high-level model object that can be queried for results.
"""
i = arange(1,len(K)+1).reshape((-1,1))
X = hstack((i, K))
y = asarray(y,dtype=double)
X = asarray(X,dtype=double)
prob = svm_problem(y,X)
param = svm_parameter(kernel_type=PRECOMPUTED,**param_kw)
model = svm_model(prob, param)
return get_alpha_b(model)
示例9: trainSVM
def trainSVM(kernel, labels):
#need to add an id number as the first column of the list
svmKernel = column_stack((arange(1, len(kernel.tolist()) + 1), kernel))
prob = svm_problem(labels.tolist(), svmKernel.tolist(), isKernel=True)
param = svm_parameter('-t 4')
model = svm_train(prob, param)
return model
示例10: Select_Linear_Model
def Select_Linear_Model(self, C_min=-10, C_steps=11):
#Search for the model parameters that give the smallest CV error
C = self.__Linear_Search__(C_min, C_steps, 1)
C = self.__Linear_Search__(np.log2(C)-2, 50, 0.08)
C = self.__Linear_Search__(np.log2(C)-0.5, 50, 0.02)
self.svm_params['C'] = C
self.model = svm.svm_model(self.svm_problem, svm.svm_parameter(**self.svm_params))
示例11: train
def train(self, dataset):
"""
Trains the svm classifier. Converts words to real numbers for training
as SVM expects only numbers.
"""
super(SvmLearner, self).train(dataset)
prob = svm.svm_problem(self.results, self.observations)
param = svm.svm_parameter(kernel_type=svm.LINEAR, C=10, probability=1)
self.model = svm.svm_model(prob, param)
示例12: train
def train(self,trainset):
"""
Trains the SVM.
"""
self.n_classes = len(trainset.metadata['targets'])
# Set LIBSVM parameters
kernel_types = {'linear':libsvm.LINEAR,'polynomial':libsvm.POLY,
'rbf':libsvm.RBF,'sigmoid':libsvm.SIGMOID}
if self.kernel not in kernel_types:
raise ValueError('Invalid kernel: '+self.kernel+'. Should be either \'linear\', \'polynomial\', \'rbf\' or \'sigmoid\'')
if self.label_weights != None:
class_to_id = trainset.metadata['class_to_id']
nr_weight = self.n_classes
weight_label = range(self.n_classes)
weight = [1]*self.n_classes
for k,v in self.label_weights.iteritems():
weight[class_to_id[k]] = v
else:
nr_weight = 0
weight_label = []
weight = []
libsvm_params = libsvm.svm_parameter(svm_type = libsvm.C_SVC,
kernel_type = kernel_types[self.kernel],
degree=self.degree,
gamma=self.gamma,
coef0=self.coef0,
C=self.C,
probability=int(self.output_probabilities),
cache_size=self.cache_size,
eps=self.tolerance,
shrinking=int(self.shrinking),
nr_weight = nr_weight,
weight_label = weight_label,
weight = weight)
# Put training set in the appropriate format:
# if is sparse (i.e. a pair), inputs are converted to dictionaries
# if not, inputs are assumed to be sequences and are kept intact
libsvm_inputs = []
libsvm_targets = []
for input,target in trainset:
if type(input) == tuple:
libsvm_inputs += [dict(zip(input[1],input[0]))]
else:
libsvm_inputs += [input]
libsvm_targets += [float(target)] # LIBSVM requires double-valued targets
libsvm_problem = libsvm.svm_problem(libsvm_targets,libsvm_inputs)
# Train SVM
self.svm = libsvm.svm_model(libsvm_problem,libsvm_params)
示例13: Select_Model
def Select_Model(self, C_min=-10, C_steps=11, gamma_min=-15, gamma_steps=16):
#Search for the model parameters that give the smallest CV error
(C, gamma) = self.__Search__(C_min, C_steps, gamma_min, gamma_steps, 1, 1)
#(C, gamma) = self.__Search__(np.log2(C)-5, 100, np.log2(gamma)-5, 100, 0.1, 0.1)
(C, gamma) = self.__Search__(np.log2(C)-1, 100, np.log2(gamma)-1, 100, 0.02, 0.02)
#(C, gamma) = self.__Search__(np.log2(C)-0.5, 100, np.log2(gamma)-0.5, 100, 0.01, 0.01)
self.svm_params['C'] = C
self.svm_params['gamma'] = gamma
self.model = svm.svm_model(self.svm_problem, svm.svm_parameter(**self.svm_params))
示例14: search
def search(self):
""" iterate successive parameter grid refinement and evaluation; adapted from LIBSVM grid search tool """
jobs = self.calculate_jobs()
scores = []
for line in jobs:
for (c, g) in line:
# run cross-validation for this point
self.setParams(C=2 ** c, gamma=2 ** g)
param = svm_parameter(**self.params)
cvresult = array(cross_validation(self.problem, param, self.crossval))
corr, = where(cvresult == self.targets)
res = (c, g, float(corr.size) / self.targets.size)
scores.append(res)
self._save_points(res)
self._redraw(scores)
scores = array(scores)
best = scores[scores[:, 0].argmax(), 1:]
self.setParams(C=2 ** best[0], gamma=2 ** best[1])
logging.info("best log2C=%12.7g, log2g=%11.7g " % (best[0], best[1]))
param = svm_parameter(**self.params)
return param
示例15: iterGridSearchSVM
def iterGridSearchSVM(self, c_info=None, g_info=None, fold=5,
probability=False, compensation=True):
swap = lambda a,b: (b,a)
if not c_info is None and len(c_info) >= 3:
c_begin, c_end, c_step = c_info[:3]
else:
c_begin, c_end, c_step = -5, 15, 2
if c_end < c_begin:
c_begin, c_end = swap(c_begin, c_end)
c_step = abs(c_step)
if not g_info is None and len(g_info) >= 3:
g_begin, g_end, g_step = g_info[:3]
else:
g_begin, g_end, g_step = -15, 3, 2
if g_end < g_begin:
g_begin, g_end = swap(g_begin, g_end)
g_step = abs(g_step)
labels, samples = self.getData(normalize=True)
#print len(labels), len(samples)
problem = svm.svm_problem(labels, samples)
if compensation:
weight, weight_label = self._calculateCompensation(labels)
n = (c_end - c_begin) / c_step + 1
n *= (g_end - g_begin) / g_step + 1
l2c = c_begin
while l2c <= c_end:
l2g = g_begin
while l2g <= g_end:
param = svm.svm_parameter(kernel_type=svm.RBF,
C=2.**l2c, gamma=2.**l2g,
probability=1 if probability else 0)
if compensation:
param.weight = weight
param.weight_label = weight_label
param.nr_weight = len(weight)
predictions = svm.cross_validation(problem, param, fold)
predictions = map(int, predictions)
#print n,c,g
conf = ConfusionMatrix.from_lists(labels, predictions,
self.l2nl)
yield n,l2c,l2g,conf
l2g += g_step
l2c += c_step