From: cttsai Date: Sat, 5 Mar 2011 08:46:26 +0000 (+0000) Subject: - Rename linear.py and linearutil.py to liblinear.py and liblinearutil.py. X-Git-Tag: v180~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a9ef8e271e4d6cb60fd51cda3eea7a2be81f22ba;p=liblinear - Rename linear.py and linearutil.py to liblinear.py and liblinearutil.py. - Add L2R_LR_DUAL to python interface. --- diff --git a/python/README b/python/README index 4b532d3..13a0734 100644 --- a/python/README +++ b/python/README @@ -42,9 +42,9 @@ Quick Start =========== There are two levels of usage. The high-level one uses utility functions -in linearutil.py and the usage is the same as the LIBLINEAR MATLAB interface. +in liblinearutil.py and the usage is the same as the LIBLINEAR MATLAB interface. ->>> from linearutil import * +>>> from liblinearutil import * # Read data in LIBSVM format >>> y, x = svm_read_problem('../heart_scale') >>> m = train(y[:200], x[:200], '-c 4') @@ -68,11 +68,11 @@ in linearutil.py and the usage is the same as the LIBLINEAR MATLAB interface. # Getting online help >>> help(train) -The low-level use directly calls C interfaces imported by linear.py. Note that +The low-level use directly calls C interfaces imported by liblinear.py. Note that all arguments and return values are in ctypes format. You need to handle them carefully. ->>> from linear import * +>>> from liblinear import * >>> prob = problem([1,-1], [{1:1, 3:1}, {1:-1,3:-1}]) >>> param = parameter('-c 4') >>> m = liblinear.train(prob, param) # m is a ctype pointer to a model @@ -83,15 +83,15 @@ carefully. Design Description ================== -There are two files linear.py and linearutil.py, which respectively correspond to +There are two files liblinear.py and liblinearutil.py, which respectively correspond to low-level and high-level use of the interface. -In linear.py, we adopt the Python built-in library "ctypes," so that +In liblinear.py, we adopt the Python built-in library "ctypes," so that Python can directly access C structures and interface functions defined in linear.h. -While advanced users can use structures/functions in linear.py, to -avoid handling ctypes structures, in linearutil.py we provide some easy-to-use +While advanced users can use structures/functions in liblinear.py, to +avoid handling ctypes structures, in liblinearutil.py we provide some easy-to-use functions. The usage is similar to LIBLINEAR MATLAB interface. Data Structures @@ -106,7 +106,7 @@ fields and methods. Before using the data structures, execute the following command to load the LIBLINEAR shared library: - >>> from linear import * + >>> from liblinear import * - class feature_node: @@ -185,7 +185,7 @@ LIBLINEAR shared library: Note that the returned structure of interface functions liblinear.train and liblinear.load_model is a ctypes pointer of model, which is different from the model object returned - by train and load_model in linearutil.py. We provide a + by train and load_model in liblinearutil.py. We provide a function toPyModel for the conversion: >>> model_ptr = liblinear.train(prob, param) @@ -208,7 +208,7 @@ Utility Functions To use utility functions, type - >>> from linearutil import * + >>> from liblinearutil import * The above command loads train() : train an linear model diff --git a/python/linear.py b/python/liblinear.py similarity index 98% rename from python/linear.py rename to python/liblinear.py index e3327f7..5872a4a 100644 --- a/python/linear.py +++ b/python/liblinear.py @@ -20,7 +20,7 @@ else: # Construct constants SOLVER_TYPE = ['L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL',\ - 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR'] + 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR', 'L2R_LR_DUAL'] for i, s in enumerate(SOLVER_TYPE): exec("%s = %d" % (s , i)) PRINT_STRING_FUN = CFUNCTYPE(None, c_char_p) @@ -185,7 +185,7 @@ class parameter(Structure): if self.eps == float('inf'): if self.solver_type in [L2R_LR, L2R_L2LOSS_SVC]: self.eps = 0.01 - elif self.solver_type in [L2R_L2LOSS_SVC_DUAL, L2R_L1LOSS_SVC_DUAL, MCSVM_CS]: + elif self.solver_type in [L2R_L2LOSS_SVC_DUAL, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L2R_LR_DUAL]: self.eps = 0.1 elif self.solver_type in [L1R_L2LOSS_SVC, L1R_LR]: self.eps = 0.01 diff --git a/python/linearutil.py b/python/liblinearutil.py similarity index 97% rename from python/linearutil.py rename to python/liblinearutil.py index 06cd1c3..bf23b5f 100644 --- a/python/linearutil.py +++ b/python/liblinearutil.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from linear import * +from liblinear import * def svm_read_problem(data_file_name): """ @@ -74,19 +74,20 @@ def train(arg1, arg2=None, arg3=None): 'options': -s type : set type of solver (default 1) - 0 -- L2-regularized logistic regression + 0 -- L2-regularized logistic regression (primal) 1 -- L2-regularized L2-loss support vector classification (dual) 2 -- L2-regularized L2-loss support vector classification (primal) 3 -- L2-regularized L1-loss support vector classification (dual) 4 -- multi-class support vector classification by Crammer and Singer 5 -- L1-regularized L2-loss support vector classification 6 -- L1-regularized logistic regression + 7 -- L2-regularized logistic regression (dual) -c cost : set the parameter C (default 1) -e epsilon : set tolerance of termination criterion -s 0 and 2 |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2, where f is the primal function, (default 0.01) - -s 1, 3, and 4 + -s 1, 3, 4, and 7 Dual maximal violation <= eps; similar to liblinear (default 0.1) -s 5 and 6 |f'(w)|_inf <= eps*min(pos,neg)/l*|f'(w0)|_inf,