svm.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from ctypes import *
4 from ctypes.util import find_library
5 import sys
6 import os
7 
8 # For unix the prefix 'lib' is not considered.
9 if find_library('svm'):
10  libsvm = CDLL(find_library('svm'))
11 elif find_library('libsvm'):
12  libsvm = CDLL(find_library('libsvm'))
13 else:
14  if sys.platform == 'win32':
15  libsvm = CDLL(os.path.join(os.path.dirname(__file__),\
16  '../windows/libsvm.dll'))
17  else:
18  libsvm = CDLL(os.path.join(os.path.dirname(__file__),\
19  '../libsvm.so.2'))
20 
21 # Construct constants
22 SVM_TYPE = ['C_SVC', 'NU_SVC', 'ONE_CLASS', 'EPSILON_SVR', 'NU_SVR' ]
23 KERNEL_TYPE = ['LINEAR', 'POLY', 'RBF', 'SIGMOID', 'PRECOMPUTED']
24 for i, s in enumerate(SVM_TYPE): exec("%s = %d" % (s , i))
25 for i, s in enumerate(KERNEL_TYPE): exec("%s = %d" % (s , i))
26 
27 PRINT_STRING_FUN = CFUNCTYPE(None, c_char_p)
28 def print_null(s):
29  return
30 
31 def genFields(names, types):
32  return list(zip(names, types))
33 
34 def fillprototype(f, restype, argtypes):
35  f.restype = restype
36  f.argtypes = argtypes
37 
38 class svm_node(Structure):
39  _names = ["index", "value"]
40  _types = [c_int, c_double]
41  _fields_ = genFields(_names, _types)
42 
43 def gen_svm_nodearray(xi, feature_max=None, isKernel=None):
44  if isinstance(xi, dict):
45  index_range = xi.keys()
46  elif isinstance(xi, (list, tuple)):
47  if not isKernel:
48  xi = [0] + xi # idx should start from 1
49  index_range = range(len(xi))
50  else:
51  raise TypeError('xi should be a dictionary, list or tuple')
52 
53  if feature_max:
54  assert(isinstance(feature_max, int))
55  index_range = filter(lambda j: j <= feature_max, index_range)
56  if not isKernel:
57  index_range = filter(lambda j:xi[j] != 0, index_range)
58 
59  index_range = sorted(index_range)
60  ret = (svm_node * (len(index_range)+1))()
61  ret[-1].index = -1
62  for idx, j in enumerate(index_range):
63  ret[idx].index = j
64  ret[idx].value = xi[j]
65  max_idx = 0
66  if index_range:
67  max_idx = index_range[-1]
68  return ret, max_idx
69 
70 class svm_problem(Structure):
71  _names = ["l", "y", "x"]
72  _types = [c_int, POINTER(c_double), POINTER(POINTER(svm_node))]
73  _fields_ = genFields(_names, _types)
74 
75  def __init__(self, y, x, isKernel=None):
76  if len(y) != len(x):
77  raise ValueError("len(y) != len(x)")
78  self.l = l = len(y)
79 
80  max_idx = 0
81  x_space = self.x_space = []
82  for i, xi in enumerate(x):
83  tmp_xi, tmp_idx = gen_svm_nodearray(xi,isKernel=isKernel)
84  x_space += [tmp_xi]
85  max_idx = max(max_idx, tmp_idx)
86  self.n = max_idx
87 
88  self.y = (c_double * l)()
89  for i, yi in enumerate(y): self.y[i] = yi
90 
91  self.x = (POINTER(svm_node) * l)()
92  for i, xi in enumerate(self.x_space): self.x[i] = xi
93 
94 class svm_parameter(Structure):
95  _names = ["svm_type", "kernel_type", "degree", "gamma", "coef0",
96  "cache_size", "eps", "C", "nr_weight", "weight_label", "weight",
97  "nu", "p", "shrinking", "probability"]
98  _types = [c_int, c_int, c_int, c_double, c_double,
99  c_double, c_double, c_double, c_int, POINTER(c_int), POINTER(c_double),
100  c_double, c_double, c_int, c_int]
101  _fields_ = genFields(_names, _types)
102 
103  def __init__(self, options = None):
104  if options == None:
105  options = ''
106  self.parse_options(options)
107 
108  def show(self):
109  attrs = svm_parameter._names + self.__dict__.keys()
110  values = map(lambda attr: getattr(self, attr), attrs)
111  for attr, val in zip(attrs, values):
112  print(' %s: %s' % (attr, val))
113 
115  self.svm_type = C_SVC;
116  self.kernel_type = RBF
117  self.degree = 3
118  self.gamma = 0
119  self.coef0 = 0
120  self.nu = 0.5
121  self.cache_size = 100
122  self.C = 1
123  self.eps = 0.001
124  self.p = 0.1
125  self.shrinking = 1
126  self.probability = 0
127  self.nr_weight = 0
128  self.weight_label = (c_int*0)()
129  self.weight = (c_double*0)()
130  self.cross_validation = False
131  self.nr_fold = 0
132  self.print_func = None
133 
134  def parse_options(self, options):
135  argv = options.split()
136  self.set_to_default_values()
137  self.print_func = cast(None, PRINT_STRING_FUN)
138  weight_label = []
139  weight = []
140 
141  i = 0
142  while i < len(argv):
143  if argv[i] == "-s":
144  i = i + 1
145  self.svm_type = int(argv[i])
146  elif argv[i] == "-t":
147  i = i + 1
148  self.kernel_type = int(argv[i])
149  elif argv[i] == "-d":
150  i = i + 1
151  self.degree = int(argv[i])
152  elif argv[i] == "-g":
153  i = i + 1
154  self.gamma = float(argv[i])
155  elif argv[i] == "-r":
156  i = i + 1
157  self.coef0 = float(argv[i])
158  elif argv[i] == "-n":
159  i = i + 1
160  self.nu = float(argv[i])
161  elif argv[i] == "-m":
162  i = i + 1
163  self.cache_size = float(argv[i])
164  elif argv[i] == "-c":
165  i = i + 1
166  self.C = float(argv[i])
167  elif argv[i] == "-e":
168  i = i + 1
169  self.eps = float(argv[i])
170  elif argv[i] == "-p":
171  i = i + 1
172  self.p = float(argv[i])
173  elif argv[i] == "-h":
174  i = i + 1
175  self.shrinking = int(argv[i])
176  elif argv[i] == "-b":
177  i = i + 1
178  self.probability = int(argv[i])
179  elif argv[i] == "-q":
180  self.print_func = PRINT_STRING_FUN(print_null)
181  elif argv[i] == "-v":
182  i = i + 1
183  self.cross_validation = 1
184  self.nr_fold = int(argv[i])
185  if self.nr_fold < 2:
186  raise ValueError("n-fold cross validation: n must >= 2")
187  elif argv[i].startswith("-w"):
188  i = i + 1
189  self.nr_weight += 1
190  nr_weight = self.nr_weight
191  weight_label += [int(argv[i-1][2:])]
192  weight += [float(argv[i])]
193  else:
194  raise ValueError("Wrong options")
195  i += 1
196 
197  libsvm.svm_set_print_string_function(self.print_func)
198  self.weight_label = (c_int*self.nr_weight)()
199  self.weight = (c_double*self.nr_weight)()
200  for i in range(self.nr_weight):
201  self.weight[i] = weight[i]
202  self.weight_label[i] = weight_label[i]
203 
204 class svm_model(Structure):
205  _names = ['param', 'nr_class', 'l', 'SV', 'sv_coef', 'rho',
206  'probA', 'probB', 'sv_indices', 'label', 'nSV', 'free_sv']
207  _types = [svm_parameter, c_int, c_int, POINTER(POINTER(svm_node)),
208  POINTER(POINTER(c_double)), POINTER(c_double),
209  POINTER(c_double), POINTER(c_double), POINTER(c_int),
210  POINTER(c_int), POINTER(c_int), c_int]
211  _fields_ = genFields(_names, _types)
212 
213  def __init__(self):
214  self.__createfrom__ = 'python'
215 
216  def __del__(self):
217  # free memory created by C to avoid memory leak
218  if hasattr(self, '__createfrom__') and self.__createfrom__ == 'C':
219  libsvm.svm_free_and_destroy_model(pointer(self))
220 
221  def get_svm_type(self):
222  return libsvm.svm_get_svm_type(self)
223 
224  def get_nr_class(self):
225  return libsvm.svm_get_nr_class(self)
226 
228  return libsvm.svm_get_svr_probability(self)
229 
230  def get_labels(self):
231  nr_class = self.get_nr_class()
232  labels = (c_int * nr_class)()
233  libsvm.svm_get_labels(self, labels)
234  return labels[:nr_class]
235 
236  def get_sv_indices(self):
237  total_sv = self.get_nr_sv()
238  sv_indices = (c_int * total_sv)()
239  libsvm.svm_get_sv_indices(self, sv_indices)
240  return sv_indices[:total_sv]
241 
242  def get_nr_sv(self):
243  return libsvm.svm_get_nr_sv(self)
244 
246  return (libsvm.svm_check_probability_model(self) == 1)
247 
248  def get_sv_coef(self):
249  return [tuple(self.sv_coef[j][i] for j in xrange(self.nr_class - 1))
250  for i in xrange(self.l)]
251 
252  def get_SV(self):
253  result = []
254  for sparse_sv in self.SV[:self.l]:
255  row = dict()
256 
257  i = 0
258  while True:
259  row[sparse_sv[i].index] = sparse_sv[i].value
260  if sparse_sv[i].index == -1:
261  break
262  i += 1
263 
264  result.append(row)
265  return result
266 
267 def toPyModel(model_ptr):
268  """
269  toPyModel(model_ptr) -> svm_model
270 
271  Convert a ctypes POINTER(svm_model) to a Python svm_model
272  """
273  if bool(model_ptr) == False:
274  raise ValueError("Null pointer")
275  m = model_ptr.contents
276  m.__createfrom__ = 'C'
277  return m
278 
279 fillprototype(libsvm.svm_train, POINTER(svm_model), [POINTER(svm_problem), POINTER(svm_parameter)])
280 fillprototype(libsvm.svm_cross_validation, None, [POINTER(svm_problem), POINTER(svm_parameter), c_int, POINTER(c_double)])
281 
282 fillprototype(libsvm.svm_save_model, c_int, [c_char_p, POINTER(svm_model)])
283 fillprototype(libsvm.svm_load_model, POINTER(svm_model), [c_char_p])
284 
285 fillprototype(libsvm.svm_get_svm_type, c_int, [POINTER(svm_model)])
286 fillprototype(libsvm.svm_get_nr_class, c_int, [POINTER(svm_model)])
287 fillprototype(libsvm.svm_get_labels, None, [POINTER(svm_model), POINTER(c_int)])
288 fillprototype(libsvm.svm_get_sv_indices, None, [POINTER(svm_model), POINTER(c_int)])
289 fillprototype(libsvm.svm_get_nr_sv, c_int, [POINTER(svm_model)])
290 fillprototype(libsvm.svm_get_svr_probability, c_double, [POINTER(svm_model)])
291 
292 fillprototype(libsvm.svm_predict_values, c_double, [POINTER(svm_model), POINTER(svm_node), POINTER(c_double)])
293 fillprototype(libsvm.svm_predict, c_double, [POINTER(svm_model), POINTER(svm_node)])
294 fillprototype(libsvm.svm_predict_probability, c_double, [POINTER(svm_model), POINTER(svm_node), POINTER(c_double)])
295 
296 fillprototype(libsvm.svm_free_model_content, None, [POINTER(svm_model)])
297 fillprototype(libsvm.svm_free_and_destroy_model, None, [POINTER(POINTER(svm_model))])
298 fillprototype(libsvm.svm_destroy_param, None, [POINTER(svm_parameter)])
299 
300 fillprototype(libsvm.svm_check_parameter, c_char_p, [POINTER(svm_problem), POINTER(svm_parameter)])
301 fillprototype(libsvm.svm_check_probability_model, c_int, [POINTER(svm_model)])
302 fillprototype(libsvm.svm_set_print_string_function, None, [PRINT_STRING_FUN])
def gen_svm_nodearray(xi, feature_max=None, isKernel=None)
Definition: svm.py:43
__createfrom__
Definition: svm.py:214
def is_probability_model(self)
Definition: svm.py:245
def get_svr_probability(self)
Definition: svm.py:227
def parse_options(self, options)
Definition: svm.py:134
#define max(x, y)
Definition: libsvmread.c:15
def get_nr_sv(self)
Definition: svm.py:242
def get_sv_indices(self)
Definition: svm.py:236
def get_labels(self)
Definition: svm.py:230
def get_svm_type(self)
Definition: svm.py:221
def get_SV(self)
Definition: svm.py:252
def get_nr_class(self)
Definition: svm.py:224
def __del__(self)
Definition: svm.py:216
def set_to_default_values(self)
Definition: svm.py:114
xrange
Definition: subset.py:7
def __init__(self, options=None)
Definition: svm.py:103
def get_sv_coef(self)
Definition: svm.py:248
def fillprototype(f, restype, argtypes)
Definition: svm.py:34
PRINT_STRING_FUN
Definition: svm.py:27
def __init__(self, y, x, isKernel=None)
Definition: svm.py:75
def show(self)
Definition: svm.py:108
def genFields(names, types)
Definition: svm.py:31
def print_null(s)
Definition: svm.py:28
def toPyModel(model_ptr)
Definition: svm.py:267
def __init__(self)
Definition: svm.py:213


ml_classifiers
Author(s): Scott Niekum , Joshua Whitley
autogenerated on Tue May 14 2019 02:28:35