from ctypes import *
from ctypes.util import find_library
+from os import path
import sys
-import os
-# For unix the prefix 'lib' is not considered.
-if find_library('linear'):
- liblinear = CDLL(find_library('linear'))
-elif find_library('liblinear'):
- liblinear = CDLL(find_library('liblinear'))
-else:
+try:
+ dirname = path.dirname(path.abspath(__file__))
if sys.platform == 'win32':
- liblinear = CDLL(os.path.join(os.path.dirname(__file__),\
- '../windows/liblinear.dll'))
+ liblinear = CDLL(path.join(dirname, r'..\windows\liblinear.dll'))
+ else:
+ liblinear = CDLL(path.join(dirname, '../liblinear.so.1'))
+except:
+# For unix the prefix 'lib' is not considered.
+ if find_library('linear'):
+ liblinear = CDLL(find_library('linear'))
+ elif find_library('liblinear'):
+ liblinear = CDLL(find_library('liblinear'))
else:
- liblinear = CDLL(os.path.join(os.path.dirname(__file__),\
- '../liblinear.so.1'))
+ raise Exception('LIBLINEAR library not found.')
# Construct constants
SOLVER_TYPE = ['L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL',\
_types = [c_int, c_double]
_fields_ = genFields(_names, _types)
+ def __str__(self):
+ return '%d:%g' % (self.index, self.value)
+
def gen_feature_nodearray(xi, feature_max=None, issparse=True):
if isinstance(xi, dict):
index_range = xi.keys()
options = ''
self.parse_options(options)
- def show(self):
- attrs = parameter._names + self.__dict__.keys()
+ def __str__(self):
+ s = ''
+ attrs = parameter._names + list(self.__dict__.keys())
values = map(lambda attr: getattr(self, attr), attrs)
for attr, val in zip(attrs, values):
- print(' %s: %s' % (attr, val))
+ s += (' %s: %s\n' % (attr, val))
+ s = s.strip()
+
+ return s
def set_to_default_values(self):
self.solver_type = L2R_L2LOSS_SVC_DUAL
self.print_func = None
def parse_options(self, options):
- argv = options.split()
+ if isinstance(options, list):
+ argv = options
+ elif isinstance(options, str):
+ argv = options.split()
+ else:
+ raise TypeError("arg 1 should be a list or a str.")
self.set_to_default_values()
self.print_func = cast(None, PRINT_STRING_FUN)
weight_label = []
fillprototype(liblinear.check_parameter, c_char_p, [POINTER(problem), POINTER(parameter)])
fillprototype(liblinear.check_probability_model, c_int, [POINTER(model)])
fillprototype(liblinear.set_print_string_function, None, [CFUNCTYPE(None, c_char_p)])
-
-
#!/usr/bin/env python
+import os, sys
+sys.path = [os.path.dirname(os.path.abspath(__file__))] + sys.path
from liblinear import *
def svm_read_problem(data_file_name):
def train(arg1, arg2=None, arg3=None):
"""
- train(y, x [, 'options']) -> model | ACC
- train(prob, [, 'options']) -> model | ACC
+ train(y, x [, options]) -> model | ACC
+ train(prob [, options]) -> model | ACC
train(prob, param) -> model | ACC
Train a model from data (y, x) or a problem prob using
If '-v' is specified in 'options' (i.e., cross validation)
either accuracy (ACC) or mean-squared error (MSE) is returned.
- 'options':
+ options:
-s type : set type of solver (default 1)
for multi-class classification
0 -- L2-regularized logistic regression (primal)
m = liblinear.train(prob, param)
m = toPyModel(m)
- # If prob is destroyed, data including SVs pointed by m can remain.
- m.x_space = prob.x_space
return m
def predict(y, x, m, options=""):
"""
- predict(y, x, m [, "options"]) -> (p_labels, p_acc, p_vals)
+ predict(y, x, m [, options]) -> (p_labels, p_acc, p_vals)
Predict data (y, x) with the SVM model m.
- "options":
+ options:
-b probability_estimates: whether to output probability estimates, 0 or 1 (default 0); currently for logistic regression only
-q quiet mode (no outputs)