svmutil.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from svm import *
4 
5 def svm_read_problem(data_file_name):
6  """
7  svm_read_problem(data_file_name) -> [y, x]
8 
9  Read LIBSVM-format data from data_file_name and return labels y
10  and data instances x.
11  """
12  prob_y = []
13  prob_x = []
14  for line in open(data_file_name):
15  line = line.split(None, 1)
16  # In case an instance with all zero features
17  if len(line) == 1: line += ['']
18  label, features = line
19  xi = {}
20  for e in features.split():
21  ind, val = e.split(":")
22  xi[int(ind)] = float(val)
23  prob_y += [float(label)]
24  prob_x += [xi]
25  return (prob_y, prob_x)
26 
27 def svm_load_model(model_file_name):
28  """
29  svm_load_model(model_file_name) -> model
30 
31  Load a LIBSVM model from model_file_name and return.
32  """
33  model = libsvm.svm_load_model(model_file_name)
34  if not model:
35  print("can't open model file %s" % model_file_name)
36  return None
37  model = toPyModel(model)
38  return model
39 
40 def svm_save_model(model_file_name, model):
41  """
42  svm_save_model(model_file_name, model) -> None
43 
44  Save a LIBSVM model to the file model_file_name.
45  """
46  libsvm.svm_save_model(model_file_name, model)
47 
48 def evaluations(ty, pv):
49  """
50  evaluations(ty, pv) -> (ACC, MSE, SCC)
51 
52  Calculate accuracy, mean squared error and squared correlation coefficient
53  using the true values (ty) and predicted values (pv).
54  """
55  if len(ty) != len(pv):
56  raise ValueError("len(ty) must equal to len(pv)")
57  total_correct = total_error = 0
58  sumv = sumy = sumvv = sumyy = sumvy = 0
59  for v, y in zip(pv, ty):
60  if y == v:
61  total_correct += 1
62  total_error += (v-y)*(v-y)
63  sumv += v
64  sumy += y
65  sumvv += v*v
66  sumyy += y*y
67  sumvy += v*y
68  l = len(ty)
69  ACC = 100.0*total_correct/l
70  MSE = total_error/l
71  try:
72  SCC = ((l*sumvy-sumv*sumy)*(l*sumvy-sumv*sumy))/((l*sumvv-sumv*sumv)*(l*sumyy-sumy*sumy))
73  except:
74  SCC = float('nan')
75  return (ACC, MSE, SCC)
76 
77 def svm_train(arg1, arg2=None, arg3=None):
78  """
79  svm_train(y, x [, 'options']) -> model | ACC | MSE
80  svm_train(prob, [, 'options']) -> model | ACC | MSE
81  svm_train(prob, param) -> model | ACC| MSE
82 
83  Train an SVM model from data (y, x) or an svm_problem prob using
84  'options' or an svm_parameter param.
85  If '-v' is specified in 'options' (i.e., cross validation)
86  either accuracy (ACC) or mean-squared error (MSE) is returned.
87  'options':
88  -s svm_type : set type of SVM (default 0)
89  0 -- C-SVC
90  1 -- nu-SVC
91  2 -- one-class SVM
92  3 -- epsilon-SVR
93  4 -- nu-SVR
94  -t kernel_type : set type of kernel function (default 2)
95  0 -- linear: u'*v
96  1 -- polynomial: (gamma*u'*v + coef0)^degree
97  2 -- radial basis function: exp(-gamma*|u-v|^2)
98  3 -- sigmoid: tanh(gamma*u'*v + coef0)
99  4 -- precomputed kernel (kernel values in training_set_file)
100  -d degree : set degree in kernel function (default 3)
101  -g gamma : set gamma in kernel function (default 1/num_features)
102  -r coef0 : set coef0 in kernel function (default 0)
103  -c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
104  -n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
105  -p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
106  -m cachesize : set cache memory size in MB (default 100)
107  -e epsilon : set tolerance of termination criterion (default 0.001)
108  -h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
109  -b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
110  -wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
111  -v n: n-fold cross validation mode
112  -q : quiet mode (no outputs)
113  """
114  prob, param = None, None
115  if isinstance(arg1, (list, tuple)):
116  assert isinstance(arg2, (list, tuple))
117  y, x, options = arg1, arg2, arg3
118  param = svm_parameter(options)
119  prob = svm_problem(y, x, isKernel=(param.kernel_type == PRECOMPUTED))
120  elif isinstance(arg1, svm_problem):
121  prob = arg1
122  if isinstance(arg2, svm_parameter):
123  param = arg2
124  else:
125  param = svm_parameter(arg2)
126  if prob == None or param == None:
127  raise TypeError("Wrong types for the arguments")
128 
129  if param.kernel_type == PRECOMPUTED:
130  for xi in prob.x_space:
131  idx, val = xi[0].index, xi[0].value
132  if xi[0].index != 0:
133  raise ValueError('Wrong input format: first column must be 0:sample_serial_number')
134  if val <= 0 or val > prob.n:
135  raise ValueError('Wrong input format: sample_serial_number out of range')
136 
137  if param.gamma == 0 and prob.n > 0:
138  param.gamma = 1.0 / prob.n
139  libsvm.svm_set_print_string_function(param.print_func)
140  err_msg = libsvm.svm_check_parameter(prob, param)
141  if err_msg:
142  raise ValueError('Error: %s' % err_msg)
143 
144  if param.cross_validation:
145  l, nr_fold = prob.l, param.nr_fold
146  target = (c_double * l)()
147  libsvm.svm_cross_validation(prob, param, nr_fold, target)
148  ACC, MSE, SCC = evaluations(prob.y[:l], target[:l])
149  if param.svm_type in [EPSILON_SVR, NU_SVR]:
150  print("Cross Validation Mean squared error = %g" % MSE)
151  print("Cross Validation Squared correlation coefficient = %g" % SCC)
152  return MSE
153  else:
154  print("Cross Validation Accuracy = %g%%" % ACC)
155  return ACC
156  else:
157  m = libsvm.svm_train(prob, param)
158  m = toPyModel(m)
159 
160  # If prob is destroyed, data including SVs pointed by m can remain.
161  m.x_space = prob.x_space
162  return m
163 
164 def svm_predict(y, x, m, options=""):
165  """
166  svm_predict(y, x, m [, "options"]) -> (p_labels, p_acc, p_vals)
167 
168  Predict data (y, x) with the SVM model m.
169  "options":
170  -b probability_estimates: whether to predict probability estimates,
171  0 or 1 (default 0); for one-class SVM only 0 is supported.
172 
173  The return tuple contains
174  p_labels: a list of predicted labels
175  p_acc: a tuple including accuracy (for classification), mean-squared
176  error, and squared correlation coefficient (for regression).
177  p_vals: a list of decision values or probability estimates (if '-b 1'
178  is specified). If k is the number of classes, for decision values,
179  each element includes results of predicting k(k-1)/2 binary-class
180  SVMs. For probabilities, each element contains k values indicating
181  the probability that the testing instance is in each class.
182  Note that the order of classes here is the same as 'model.label'
183  field in the model structure.
184  """
185  predict_probability = 0
186  argv = options.split()
187  i = 0
188  while i < len(argv):
189  if argv[i] == '-b':
190  i += 1
191  predict_probability = int(argv[i])
192  else:
193  raise ValueError("Wrong options")
194  i+=1
195 
196  svm_type = m.get_svm_type()
197  is_prob_model = m.is_probability_model()
198  nr_class = m.get_nr_class()
199  pred_labels = []
200  pred_values = []
201 
202  if predict_probability:
203  if not is_prob_model:
204  raise ValueError("Model does not support probabiliy estimates")
205 
206  if svm_type in [NU_SVR, EPSILON_SVR]:
207  print("Prob. model for test data: target value = predicted value + z,\n"
208  "z: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g" % m.get_svr_probability());
209  nr_class = 0
210 
211  prob_estimates = (c_double * nr_class)()
212  for xi in x:
213  xi, idx = gen_svm_nodearray(xi, isKernel=(m.param.kernel_type == PRECOMPUTED))
214  label = libsvm.svm_predict_probability(m, xi, prob_estimates)
215  values = prob_estimates[:nr_class]
216  pred_labels += [label]
217  pred_values += [values]
218  else:
219  if is_prob_model:
220  print("Model supports probability estimates, but disabled in predicton.")
221  if svm_type in (ONE_CLASS, EPSILON_SVR, NU_SVC):
222  nr_classifier = 1
223  else:
224  nr_classifier = nr_class*(nr_class-1)//2
225  dec_values = (c_double * nr_classifier)()
226  for xi in x:
227  xi, idx = gen_svm_nodearray(xi, isKernel=(m.param.kernel_type == PRECOMPUTED))
228  label = libsvm.svm_predict_values(m, xi, dec_values)
229  if(nr_class == 1):
230  values = [1]
231  else:
232  values = dec_values[:nr_classifier]
233  pred_labels += [label]
234  pred_values += [values]
235 
236  ACC, MSE, SCC = evaluations(y, pred_labels)
237  l = len(y)
238  if svm_type in [EPSILON_SVR, NU_SVR]:
239  print("Mean squared error = %g (regression)" % MSE)
240  print("Squared correlation coefficient = %g (regression)" % SCC)
241  else:
242  print("Accuracy = %g%% (%d/%d) (classification)" % (ACC, int(l*ACC/100), l))
243 
244  return pred_labels, (ACC, MSE, SCC), pred_values
245 
def gen_svm_nodearray(xi, feature_max=None, isKernel=None)
Definition: svm.py:43
def svm_train(arg1, arg2=None, arg3=None)
Definition: svmutil.py:77
def svm_save_model(model_file_name, model)
Definition: svmutil.py:40
def svm_load_model(model_file_name)
Definition: svmutil.py:27
def svm_read_problem(data_file_name)
Definition: svmutil.py:5
def svm_predict(y, x, m, options="")
Definition: svmutil.py:164
def evaluations(ty, pv)
Definition: svmutil.py:48
def toPyModel(model_ptr)
Definition: svm.py:258


haf_grasping
Author(s): David Fischinger
autogenerated on Mon Jun 10 2019 13:28:43