├── liblinear-1.8 ├── python │ ├── Makefile │ ├── liblinearutil.py │ ├── liblinear.py │ └── README ├── train ├── predict ├── tron.o ├── linear.o ├── blas │ ├── blas.a │ ├── daxpy.o │ ├── ddot.o │ ├── dnrm2.o │ ├── dscal.o │ ├── Makefile │ ├── blas.h │ ├── dscal.c │ ├── daxpy.c │ ├── ddot.c │ ├── dnrm2.c │ └── blasp.h ├── ruby │ ├── linear.o │ ├── liblinear.bundle │ ├── liblinear_wrap.o │ ├── test_cross_validation.rb │ ├── Makefile │ ├── linear_cv.rb │ ├── test_file.rb │ ├── test_model.rb │ ├── test.rb │ ├── liblinear_test.rb │ ├── liblinear.i │ └── linear.rb ├── windows │ ├── train.exe │ ├── predict.exe │ ├── train.mexw64 │ ├── liblinear.dll │ ├── predict.mexw64 │ ├── libsvmread.mexw64 │ └── libsvmwrite.mexw64 ├── matlab │ ├── run.m │ ├── linear_model_matlab.h │ ├── make.m │ ├── Makefile │ ├── libsvmwrite.c │ ├── linear_model_matlab.c │ ├── libsvmread.c │ ├── README │ ├── predict.c │ └── train.c ├── linear.def ├── tron.h ├── Makefile ├── Makefile.win ├── COPYRIGHT ├── linear.h ├── predict.c ├── tron.cpp ├── train.c └── README ├── Gemfile ├── AUTHORS ├── History.txt ├── Manifest.txt ├── ext ├── extconf.rb ├── blas.h ├── tron.h ├── dscal.c ├── daxpy.c ├── ddot.c ├── dnrm2.c ├── linear.h ├── tron.cpp └── blasp.h ├── COPYING ├── lib ├── linear_cv.rb └── linear.rb ├── Rakefile ├── liblinear-ruby-swig.gemspec └── README.rdoc /liblinear-1.8/python/Makefile: -------------------------------------------------------------------------------- 1 | all = lib 2 | 3 | lib: 4 | cd ..; make lib; cd - 5 | -------------------------------------------------------------------------------- /liblinear-1.8/train: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/train -------------------------------------------------------------------------------- /liblinear-1.8/predict: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/predict -------------------------------------------------------------------------------- /liblinear-1.8/tron.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/tron.o -------------------------------------------------------------------------------- /liblinear-1.8/linear.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/linear.o -------------------------------------------------------------------------------- /liblinear-1.8/blas/blas.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/blas/blas.a -------------------------------------------------------------------------------- /liblinear-1.8/blas/daxpy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/blas/daxpy.o -------------------------------------------------------------------------------- /liblinear-1.8/blas/ddot.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/blas/ddot.o -------------------------------------------------------------------------------- /liblinear-1.8/blas/dnrm2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/blas/dnrm2.o -------------------------------------------------------------------------------- /liblinear-1.8/blas/dscal.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/blas/dscal.o -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec 4 | 5 | group :development do 6 | gem "rake" 7 | end 8 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/linear.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/ruby/linear.o -------------------------------------------------------------------------------- /liblinear-1.8/windows/train.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/windows/train.exe -------------------------------------------------------------------------------- /liblinear-1.8/windows/predict.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/windows/predict.exe -------------------------------------------------------------------------------- /liblinear-1.8/windows/train.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/windows/train.mexw64 -------------------------------------------------------------------------------- /liblinear-1.8/matlab/run.m: -------------------------------------------------------------------------------- 1 | [y,xt] = libsvmread('../heart_scale'); 2 | model=train(y, xt) 3 | [l,a]=predict(y, xt, model); 4 | 5 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/liblinear.bundle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/ruby/liblinear.bundle -------------------------------------------------------------------------------- /liblinear-1.8/ruby/liblinear_wrap.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/ruby/liblinear_wrap.o -------------------------------------------------------------------------------- /liblinear-1.8/windows/liblinear.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/windows/liblinear.dll -------------------------------------------------------------------------------- /liblinear-1.8/windows/predict.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/windows/predict.mexw64 -------------------------------------------------------------------------------- /liblinear-1.8/windows/libsvmread.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/windows/libsvmread.mexw64 -------------------------------------------------------------------------------- /liblinear-1.8/windows/libsvmwrite.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomz/liblinear-ruby-swig/HEAD/liblinear-1.8/windows/libsvmwrite.mexw64 -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Tom Zeng (Ruby SWIG interface to LIBLINEAR) 2 | Chih-Jen Lin and Machine Learning Group at National Taiwan University (developers of LIBLINEAR) 3 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/linear_model_matlab.h: -------------------------------------------------------------------------------- 1 | const char *model_to_matlab_structure(mxArray *plhs[], struct model *model_); 2 | const char *matlab_matrix_to_model(struct model *model_, const mxArray *matlab_struct); 3 | -------------------------------------------------------------------------------- /History.txt: -------------------------------------------------------------------------------- 1 | 2009-05-10 Tom Zeng (tom.z.zeng@gmail.com) 2 | * initial check-in into github 3 | 4 | 2009-07-10 Tom Zeng (tom.z.zeng@gmail.com) 5 | * incorporated the _convert_to_feature_node_array fix from the LIBSVM development group 6 | -------------------------------------------------------------------------------- /Manifest.txt: -------------------------------------------------------------------------------- 1 | History.txt 2 | COPYING 3 | AUTHORS 4 | Manifest.txt 5 | README.rdoc 6 | Rakefile 7 | lib/linear.rb 8 | lib/linear_cv.rb 9 | ext/liblinear_wrap.cxx 10 | ext/linear.cpp 11 | ext/linear.h 12 | ext/tron.h 13 | ext/tron.cpp 14 | ext/extconf.rb 15 | ext/blas.h 16 | ext/blasp.h 17 | ext/daxpy.c 18 | ext/ddot.c 19 | ext/dnrm2.c 20 | ext/dscal.c 21 | -------------------------------------------------------------------------------- /liblinear-1.8/blas/Makefile: -------------------------------------------------------------------------------- 1 | AR = ar rcv 2 | RANLIB = ranlib 3 | 4 | HEADERS = blas.h blas.h blasp.h 5 | FILES = dnrm2.o daxpy.o ddot.o dscal.o 6 | 7 | CFLAGS = $(OPTFLAGS) 8 | FFLAGS = $(OPTFLAGS) 9 | 10 | blas: $(FILES) $(HEADERS) 11 | $(AR) blas.a $(FILES) 12 | $(RANLIB) blas.a 13 | 14 | clean: 15 | - rm -f *.o 16 | - rm -f *.a 17 | - rm -f *~ 18 | 19 | .c.o: 20 | $(CC) $(CFLAGS) -c $*.c 21 | 22 | 23 | -------------------------------------------------------------------------------- /liblinear-1.8/linear.def: -------------------------------------------------------------------------------- 1 | LIBRARY liblinear 2 | EXPORTS 3 | train @1 4 | cross_validation @2 5 | save_model @3 6 | load_model @4 7 | get_nr_feature @5 8 | get_nr_class @6 9 | get_labels @7 10 | predict_values @8 11 | predict @9 12 | predict_probability @10 13 | free_and_destroy_model @11 14 | free_model_content @12 15 | destroy_param @13 16 | check_parameter @14 17 | check_probability_model @15 18 | set_print_string_function @16 19 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/make.m: -------------------------------------------------------------------------------- 1 | % This make.m is used under Windows 2 | 3 | mex -O -largeArrayDims -c ..\blas\*.c -outdir ..\blas 4 | mex -O -largeArrayDims -c ..\linear.cpp 5 | mex -O -largeArrayDims -c ..\tron.cpp 6 | mex -O -largeArrayDims -c linear_model_matlab.c -I..\ 7 | mex -O -largeArrayDims train.c -I..\ tron.obj linear.obj linear_model_matlab.obj ..\blas\*.obj 8 | mex -O -largeArrayDims predict.c -I..\ tron.obj linear.obj linear_model_matlab.obj ..\blas\*.obj 9 | mex -O -largeArrayDims libsvmread.c 10 | mex -O -largeArrayDims libsvmwrite.c 11 | -------------------------------------------------------------------------------- /ext/extconf.rb: -------------------------------------------------------------------------------- 1 | require 'mkmf' 2 | CONFIG["LDSHARED"] = case RUBY_PLATFORM 3 | when /darwin/ 4 | "g++ -dynamic -bundle -undefined suppress -flat_namespace" 5 | else 6 | "g++ -shared" 7 | end 8 | $CFLAGS = "#{ENV['CFLAGS']} -Wall -O3 " 9 | if CONFIG["MAJOR"].to_i >= 1 && CONFIG["MINOR"].to_i >= 8 10 | $CFLAGS << " -DHAVE_DEFINE_ALLOC_FUNCTION" 11 | end 12 | create_makefile('liblinear-ruby-swig/liblinear') 13 | =begin 14 | extra_mk = <<-eos 15 | 16 | eos 17 | 18 | File.open("Makefile", "a") do |mf| 19 | mf.puts extra_mk 20 | end 21 | =end 22 | 23 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/test_cross_validation.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'linear_cv' 4 | 5 | Liblinear::info_on = 0 6 | 7 | param = LParameter.new 8 | param.C = 1 9 | param.solver_type = L2R_L2LOSS_SVC_DUAL 10 | param.eps= 0.1 11 | param.nr_weight = 0 12 | bias = 1 13 | 14 | labels,samples = read_file("../heart_scale") 15 | do_cross_validation(samples, labels, param, 10) 16 | 17 | #labels,samples = read_file("../data/multiclass/vowel.scale") 18 | #do_cross_validation(samples, labels, param, 10) 19 | 20 | #labels,samples = read_file("../data/multiclass/segment.scale") 21 | #do_cross_validation(samples, labels, param, 10) 22 | 23 | #labels,samples = read_file("../data/multiclass/shuttle.scale") 24 | #do_cross_validation(samples, labels, param, 10) 25 | -------------------------------------------------------------------------------- /ext/blas.h: -------------------------------------------------------------------------------- 1 | /* blas.h -- C header file for BLAS Ver 1.0 */ 2 | /* Jesse Bennett March 23, 2000 */ 3 | 4 | /** barf [ba:rf] 2. "He suggested using FORTRAN, and everybody barfed." 5 | 6 | - From The Shogakukan DICTIONARY OF NEW ENGLISH (Second edition) */ 7 | 8 | #ifndef BLAS_INCLUDE 9 | #define BLAS_INCLUDE 10 | 11 | /* Data types specific to BLAS implementation */ 12 | typedef struct { float r, i; } fcomplex; 13 | typedef struct { double r, i; } dcomplex; 14 | typedef int blasbool; 15 | 16 | #include "blasp.h" /* Prototypes for all BLAS functions */ 17 | 18 | #define FALSE 0 19 | #define TRUE 1 20 | 21 | /* Macro functions */ 22 | #define MIN(a,b) ((a) <= (b) ? (a) : (b)) 23 | #define MAX(a,b) ((a) >= (b) ? (a) : (b)) 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /liblinear-1.8/blas/blas.h: -------------------------------------------------------------------------------- 1 | /* blas.h -- C header file for BLAS Ver 1.0 */ 2 | /* Jesse Bennett March 23, 2000 */ 3 | 4 | /** barf [ba:rf] 2. "He suggested using FORTRAN, and everybody barfed." 5 | 6 | - From The Shogakukan DICTIONARY OF NEW ENGLISH (Second edition) */ 7 | 8 | #ifndef BLAS_INCLUDE 9 | #define BLAS_INCLUDE 10 | 11 | /* Data types specific to BLAS implementation */ 12 | typedef struct { float r, i; } fcomplex; 13 | typedef struct { double r, i; } dcomplex; 14 | typedef int blasbool; 15 | 16 | #include "blasp.h" /* Prototypes for all BLAS functions */ 17 | 18 | #define FALSE 0 19 | #define TRUE 1 20 | 21 | /* Macro functions */ 22 | #define MIN(a,b) ((a) <= (b) ? (a) : (b)) 23 | #define MAX(a,b) ((a) >= (b) ? (a) : (b)) 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /ext/tron.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRON_H 2 | #define _TRON_H 3 | 4 | class function 5 | { 6 | public: 7 | virtual double fun(double *w) = 0 ; 8 | virtual void grad(double *w, double *g) = 0 ; 9 | virtual void Hv(double *s, double *Hs) = 0 ; 10 | 11 | virtual int get_nr_variable(void) = 0 ; 12 | virtual ~function(void){} 13 | }; 14 | 15 | class TRON 16 | { 17 | public: 18 | TRON(const function *fun_obj, double eps = 0.1, int max_iter = 1000); 19 | ~TRON(); 20 | 21 | void tron(double *w); 22 | void set_print_string(void (*i_print) (const char *buf)); 23 | 24 | private: 25 | int trcg(double delta, double *g, double *s, double *r); 26 | double norm_inf(int n, double *x); 27 | 28 | double eps; 29 | int max_iter; 30 | function *fun_obj; 31 | void info(const char *fmt,...); 32 | void (*tron_print_string)(const char *buf); 33 | }; 34 | #endif 35 | -------------------------------------------------------------------------------- /liblinear-1.8/tron.h: -------------------------------------------------------------------------------- 1 | #ifndef _TRON_H 2 | #define _TRON_H 3 | 4 | class function 5 | { 6 | public: 7 | virtual double fun(double *w) = 0 ; 8 | virtual void grad(double *w, double *g) = 0 ; 9 | virtual void Hv(double *s, double *Hs) = 0 ; 10 | 11 | virtual int get_nr_variable(void) = 0 ; 12 | virtual ~function(void){} 13 | }; 14 | 15 | class TRON 16 | { 17 | public: 18 | TRON(const function *fun_obj, double eps = 0.1, int max_iter = 1000); 19 | ~TRON(); 20 | 21 | void tron(double *w); 22 | void set_print_string(void (*i_print) (const char *buf)); 23 | 24 | private: 25 | int trcg(double delta, double *g, double *s, double *r); 26 | double norm_inf(int n, double *x); 27 | 28 | double eps; 29 | int max_iter; 30 | function *fun_obj; 31 | void info(const char *fmt,...); 32 | void (*tron_print_string)(const char *buf); 33 | }; 34 | #endif 35 | -------------------------------------------------------------------------------- /liblinear-1.8/Makefile: -------------------------------------------------------------------------------- 1 | CXX ?= g++ 2 | CC ?= gcc 3 | CFLAGS = -Wall -Wconversion -O3 -fPIC 4 | LIBS = blas/blas.a 5 | SHVER = 1 6 | #LIBS = -lblas 7 | 8 | all: train predict 9 | 10 | lib: linear.o tron.o blas/blas.a 11 | $(CXX) -shared -dynamiclib linear.o tron.o blas/blas.a -o liblinear.so.$(SHVER) 12 | 13 | train: tron.o linear.o train.c blas/blas.a 14 | $(CXX) $(CFLAGS) -o train train.c tron.o linear.o $(LIBS) 15 | 16 | predict: tron.o linear.o predict.c blas/blas.a 17 | $(CXX) $(CFLAGS) -o predict predict.c tron.o linear.o $(LIBS) 18 | 19 | tron.o: tron.cpp tron.h 20 | $(CXX) $(CFLAGS) -c -o tron.o tron.cpp 21 | 22 | linear.o: linear.cpp linear.h 23 | $(CXX) $(CFLAGS) -c -o linear.o linear.cpp 24 | 25 | blas/blas.a: 26 | cd blas; make OPTFLAGS='$(CFLAGS)' CC='$(CC)'; 27 | 28 | clean: 29 | cd blas; make clean 30 | cd matlab; make clean 31 | rm -f *~ tron.o linear.o train predict liblinear.so.$(SHVER) 32 | -------------------------------------------------------------------------------- /liblinear-1.8/Makefile.win: -------------------------------------------------------------------------------- 1 | #You must ensure nmake.exe, cl.exe, link.exe are in system path. 2 | #VCVARS32.bat 3 | #Under dosbox prompt 4 | #nmake -f Makefile.win 5 | 6 | ########################################## 7 | CXXC = cl.exe 8 | CFLAGS = -nologo -O2 -EHsc -I. -D __WIN32__ -D _CRT_SECURE_NO_DEPRECATE 9 | TARGET = windows 10 | 11 | all: $(TARGET)\train.exe $(TARGET)\predict.exe 12 | 13 | $(TARGET)\train.exe: tron.obj linear.obj train.c blas\*.c 14 | $(CXX) $(CFLAGS) -Fe$(TARGET)\train.exe tron.obj linear.obj train.c blas\*.c 15 | 16 | $(TARGET)\predict.exe: tron.obj linear.obj predict.c blas\*.c 17 | $(CXX) $(CFLAGS) -Fe$(TARGET)\predict.exe tron.obj linear.obj predict.c blas\*.c 18 | 19 | linear.obj: linear.cpp linear.h 20 | $(CXX) $(CFLAGS) -c linear.cpp 21 | 22 | tron.obj: tron.cpp tron.h 23 | $(CXX) $(CFLAGS) -c tron.cpp 24 | 25 | lib: linear.cpp linear.h linear.def tron.obj 26 | $(CXX) $(CFLAGS) -LD linear.cpp tron.obj blas\*.c -Fe$(TARGET)\liblinear -link -DEF:linear.def 27 | 28 | clean: 29 | -erase /Q *.obj $(TARGET)\. 30 | 31 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | == LICENSE: 2 | 3 | (The MIT License) 4 | 5 | Copyright (c) 2009 Tom Zeng 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining 8 | a copy of this software and associated documentation files (the 9 | 'Software'), to deal in the Software without restriction, including 10 | without limitation the rights to use, copy, modify, merge, publish, 11 | distribute, sublicense, and/or sell copies of the Software, and to 12 | permit persons to whom the Software is furnished to do so, subject to 13 | the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/Makefile: -------------------------------------------------------------------------------- 1 | CXX ?= g++ 2 | SWIG ?= swig 3 | 4 | UNAME := $(shell uname) 5 | # running ruby -e 'print $:[-2]' to get the ruby.h location 6 | RUBY_INCLUDEDIR := $(shell ruby -e 'print $$:[-2]') 7 | 8 | ifeq ($(UNAME), Linux) 9 | LDFLAGS = -shared 10 | EXT = so 11 | endif 12 | ifeq ($(UNAME), Darwin) 13 | LDFLAGS = -framework Ruby -bundle 14 | EXT = bundle 15 | endif 16 | #Windows: see ../README ../Makefile.win 17 | 18 | ifdef EXT 19 | CFLAGS = -O3 -I$(RUBY_INCLUDEDIR) -I.. 20 | 21 | all: liblinear.$(EXT) 22 | 23 | else 24 | 25 | all: ; @echo "Only Linux and OS X are supported, please add your OS in Makefile" 26 | 27 | endif 28 | 29 | liblinear.$(EXT): liblinear_wrap.o linear.o 30 | $(CXX) $(LDFLAGS) -o liblinear.$(EXT) liblinear_wrap.o linear.o ../tron.o ../blas/blas.a 31 | 32 | liblinear_wrap.o: liblinear_wrap.c ../linear.h 33 | $(CXX) $(CFLAGS) -fPIC -c liblinear_wrap.cxx 34 | 35 | liblinear_wrap.c: liblinear.i 36 | $(SWIG) -ruby -c++ liblinear.i 37 | 38 | linear.o: ../linear.cpp ../linear.h 39 | $(CXX) $(CFLAGS) -fPIC -c ../linear.cpp 40 | 41 | clean: 42 | rm -f *~ *.o *.so linear.o *.bundle 43 | 44 | moreclean: clean 45 | rm -f liblinear_wrap.c?? 46 | -------------------------------------------------------------------------------- /lib/linear_cv.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'linear' 4 | 5 | def do_cross_validation(prob_x, prob_y, param, nr_fold) 6 | puts "Do cross validation for a given Linear problem." 7 | prob_l = prob_y.size 8 | total_correct = 0 9 | total_error = sumv = sumy = sumvv = sumyy = sumvy = 0.0 10 | prob = LProblem.new(prob_y, prob_x, 0) 11 | target = cross_validation(prob, param, nr_fold) 12 | for i in (0..prob_l-1) 13 | if false 14 | v = target[i] 15 | y = prob_y[i] 16 | sumv = sumv + v 17 | sumy = sumy + y 18 | sumvv = sumvv + v * v 19 | sumyy = sumyy + y * y 20 | sumvy = sumvy + v * y 21 | total_error = total_error + (v-y) * (v-y) 22 | else 23 | v = target[i] 24 | if v == prob_y[i] 25 | total_correct = total_correct + 1 26 | end 27 | end 28 | end 29 | 30 | if false 31 | puts "Cross Validation Mean squared error = #{total_error / prob_l}" 32 | puts "Cross Validation Squared correlation coefficient = #{((prob_l * sumvy - sumv * sumy) * (prob_l * sumvy - sumv * sumy)) / ((prob_l * sumvv - sumv * sumv) * (prob_l * sumyy - sumy * sumy))}" 33 | else 34 | puts "Cross Validation Accuracy = #{100.0 * total_correct / prob_l}%" 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/linear_cv.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'linear' 4 | 5 | def do_cross_validation(prob_x, prob_y, param, nr_fold) 6 | puts "Do cross validation for a given Linear problem." 7 | prob_l = prob_y.size 8 | total_correct = 0 9 | total_error = sumv = sumy = sumvv = sumyy = sumvy = 0.0 10 | prob = LProblem.new(prob_y, prob_x, 0) 11 | target = cross_validation(prob, param, nr_fold) 12 | for i in (0..prob_l-1) 13 | if false 14 | v = target[i] 15 | y = prob_y[i] 16 | sumv = sumv + v 17 | sumy = sumy + y 18 | sumvv = sumvv + v * v 19 | sumyy = sumyy + y * y 20 | sumvy = sumvy + v * y 21 | total_error = total_error + (v-y) * (v-y) 22 | else 23 | v = target[i] 24 | if v == prob_y[i] 25 | total_correct = total_correct + 1 26 | end 27 | end 28 | end 29 | 30 | if false 31 | puts "Cross Validation Mean squared error = #{total_error / prob_l}" 32 | puts "Cross Validation Squared correlation coefficient = #{((prob_l * sumvy - sumv * sumy) * (prob_l * sumvy - sumv * sumy)) / ((prob_l * sumvv - sumv * sumv) * (prob_l * sumyy - sumy * sumy))}" 33 | else 34 | puts "Cross Validation Accuracy = #{100.0 * total_correct / prob_l}%" 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /ext/dscal.c: -------------------------------------------------------------------------------- 1 | #include "blas.h" 2 | 3 | int dscal_(int *n, double *sa, double *sx, int *incx) 4 | { 5 | long int i, m, nincx, nn, iincx; 6 | double ssa; 7 | 8 | /* scales a vector by a constant. 9 | uses unrolled loops for increment equal to 1. 10 | jack dongarra, linpack, 3/11/78. 11 | modified 3/93 to return if incx .le. 0. 12 | modified 12/3/93, array(1) declarations changed to array(*) */ 13 | 14 | /* Dereference inputs */ 15 | nn = *n; 16 | iincx = *incx; 17 | ssa = *sa; 18 | 19 | if (nn > 0 && iincx > 0) 20 | { 21 | if (iincx == 1) /* code for increment equal to 1 */ 22 | { 23 | m = nn-4; 24 | for (i = 0; i < m; i += 5) 25 | { 26 | sx[i] = ssa * sx[i]; 27 | sx[i+1] = ssa * sx[i+1]; 28 | sx[i+2] = ssa * sx[i+2]; 29 | sx[i+3] = ssa * sx[i+3]; 30 | sx[i+4] = ssa * sx[i+4]; 31 | } 32 | for ( ; i < nn; ++i) /* clean-up loop */ 33 | sx[i] = ssa * sx[i]; 34 | } 35 | else /* code for increment not equal to 1 */ 36 | { 37 | nincx = nn * iincx; 38 | for (i = 0; i < nincx; i += iincx) 39 | sx[i] = ssa * sx[i]; 40 | } 41 | } 42 | 43 | return 0; 44 | } /* dscal_ */ 45 | -------------------------------------------------------------------------------- /liblinear-1.8/blas/dscal.c: -------------------------------------------------------------------------------- 1 | #include "blas.h" 2 | 3 | int dscal_(int *n, double *sa, double *sx, int *incx) 4 | { 5 | long int i, m, nincx, nn, iincx; 6 | double ssa; 7 | 8 | /* scales a vector by a constant. 9 | uses unrolled loops for increment equal to 1. 10 | jack dongarra, linpack, 3/11/78. 11 | modified 3/93 to return if incx .le. 0. 12 | modified 12/3/93, array(1) declarations changed to array(*) */ 13 | 14 | /* Dereference inputs */ 15 | nn = *n; 16 | iincx = *incx; 17 | ssa = *sa; 18 | 19 | if (nn > 0 && iincx > 0) 20 | { 21 | if (iincx == 1) /* code for increment equal to 1 */ 22 | { 23 | m = nn-4; 24 | for (i = 0; i < m; i += 5) 25 | { 26 | sx[i] = ssa * sx[i]; 27 | sx[i+1] = ssa * sx[i+1]; 28 | sx[i+2] = ssa * sx[i+2]; 29 | sx[i+3] = ssa * sx[i+3]; 30 | sx[i+4] = ssa * sx[i+4]; 31 | } 32 | for ( ; i < nn; ++i) /* clean-up loop */ 33 | sx[i] = ssa * sx[i]; 34 | } 35 | else /* code for increment not equal to 1 */ 36 | { 37 | nincx = nn * iincx; 38 | for (i = 0; i < nincx; i += iincx) 39 | sx[i] = ssa * sx[i]; 40 | } 41 | } 42 | 43 | return 0; 44 | } /* dscal_ */ 45 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | task :default => ["sync_files","make_gem"] 2 | 3 | EXT = "lib/liblinear?.#{RbConfig::CONFIG["DLEXT"]}" 4 | 5 | task :make_gem => EXT 6 | 7 | file EXT => ["ext/extconf.rb", "ext/liblinear_wrap.cxx", "ext/linear.cpp", "ext/linear.h", "ext/tron.h", "ext/tron.cpp", "ext/blas.h", "ext/blasp.h", "ext/dscal.c", "ext/dnrm2.c", "ext/ddot.c", "ext/daxpy.c"] do 8 | Dir.chdir "ext" do 9 | ruby "extconf.rb" 10 | sh "make" 11 | cp "liblinear.bundle","../lib/" 12 | end 13 | end 14 | 15 | task :clean do 16 | Dir.chdir "ext" do 17 | sh "make clean" 18 | end 19 | end 20 | 21 | task :sync_files do 22 | cp "liblinear-1.8/linear.h","ext/" 23 | cp "liblinear-1.8/linear.cpp","ext/" 24 | cp "liblinear-1.8/tron.h","ext/" 25 | cp "liblinear-1.8/tron.cpp","ext/" 26 | cp "liblinear-1.8/ruby/liblinear_wrap.cxx","ext/" 27 | cp "liblinear-1.8/blas/blas.h","ext/" 28 | cp "liblinear-1.8/blas/blasp.h","ext/" 29 | cp "liblinear-1.8/blas/dscal.c","ext/" 30 | cp "liblinear-1.8/blas/dnrm2.c","ext/" 31 | cp "liblinear-1.8/blas/ddot.c","ext/" 32 | cp "liblinear-1.8/blas/daxpy.c","ext/" 33 | cp "liblinear-1.8/ruby/linear.rb","lib/" 34 | cp "liblinear-1.8/ruby/linear_cv.rb","lib/" 35 | end 36 | 37 | task :test do 38 | puts "done" 39 | end 40 | -------------------------------------------------------------------------------- /liblinear-ruby-swig.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = %q{liblinear-ruby-swig} 3 | s.version = "0.2.1.1" 4 | 5 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 6 | s.authors = ["Tom Zeng"] 7 | s.date = %q{2010-03-27} 8 | s.description = %q{Ruby wrapper of LIBLINEAR using SWIG} 9 | s.email = %q{tom.z.zeng@gmail.com} 10 | s.extensions = ["ext/extconf.rb"] 11 | s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"] 12 | s.files = ["History.txt", "COPYING", "AUTHORS", "Manifest.txt", "README.rdoc", "Rakefile", "lib/linear.rb", "lib/linear_cv.rb", "ext/liblinear_wrap.cxx", "ext/linear.cpp", "ext/linear.h", "ext/extconf.rb", "ext/tron.h", "ext/tron.cpp", "ext/blas.h", "ext/blasp.h", "ext/dscal.c", "ext/dnrm2.c", "ext/ddot.c", "ext/daxpy.c"] 13 | s.has_rdoc = false 14 | s.homepage = %q{http://www.tomzconsulting.com} 15 | s.rdoc_options = ["--main", "README.rdoc"] 16 | s.require_paths = ["lib"] 17 | s.rubygems_version = %q{1.3.1} 18 | s.summary = %q{Ruby wrapper of LIBLINEAR using SWIG} 19 | 20 | if s.respond_to? :specification_version then 21 | current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION 22 | s.specification_version = 2 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/test_file.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'rubygems' 3 | require 'linear' 4 | Liblinear::info_on = 0 5 | 6 | solvers = [ L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR ] 7 | snames= [ 'L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL', 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR' ] 8 | 9 | labels,samples = read_file("../heart_scale") 10 | 11 | pa = LParameter.new 12 | pa.C = 1 13 | pa.solver_type = L2R_L2LOSS_SVC_DUAL 14 | pa.eps= 0.1 15 | pa.nr_weight = 0 16 | #pa.weight_label = [] 17 | #pa.weight = [] 18 | bias = 1 19 | 20 | sp = LProblem.new(labels,samples,bias) 21 | m = LModel.new(sp, pa) 22 | 23 | ec = 0 24 | labels.each_index { |i| 25 | if pa.solver_type == L2R_LR and not m.probability.nil? 26 | pred, probs = m.predict_probability(samples[i]) 27 | # puts "Got #{pred} and #{probs.join(',')} for sample: [#{samples[i].inspect}] Label: #{labels[i]} Pred: #{pred}" 28 | else 29 | pred = m.predict(samples[i]) 30 | # puts "Got #{pred} for sample: [#{samples[i].inspect}] Label: #{labels[i]}" 31 | end 32 | ec += 1 if labels[i] != pred 33 | } 34 | puts "Solver #{snames[pa.solver_type]} made #{ec} errors, success rate: #{(samples.size-ec).to_f/samples.size.to_f * 100}%" 35 | 36 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/test_model.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'rubygems' 3 | require 'linear' 4 | Liblinear::info_on = 1 5 | 6 | solvers = [ L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR ] 7 | snames= [ 'L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL', 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR' ] 8 | 9 | data_file = "../heart_scale" 10 | labels,samples = read_file(data_file) 11 | 12 | pa = LParameter.new 13 | pa.C = 1 14 | pa.solver_type = L2R_L2LOSS_SVC_DUAL 15 | pa.eps= 0.1 16 | pa.nr_weight = 0 17 | #pa.weight_label = [] 18 | #pa.weight = [] 19 | bias = 1 20 | 21 | #sp = LProblem.new(labels,samples,bias) 22 | #m = LModel.new(sp, pa) 23 | m = LModel.new("#{data_file}.model") 24 | 25 | ec = 0 26 | labels.each_index { |i| 27 | if pa.solver_type == L2R_LR and not m.probability.nil? 28 | pred, probs = m.predict_probability(samples[i]) 29 | # puts "Got #{pred} and #{probs.join(',')} for sample: [#{samples[i].inspect}] Label: #{labels[i]} Pred: #{pred}" 30 | else 31 | pred = m.predict(samples[i]) 32 | # puts "Got #{pred} for sample: [#{samples[i].inspect}] Label: #{labels[i]}" 33 | end 34 | ec += 1 if labels[i] != pred 35 | } 36 | puts "Solver #{snames[pa.solver_type]} made #{ec} errors, success rate: #{(samples.size-ec).to_f/samples.size.to_f * 100}%" 37 | 38 | -------------------------------------------------------------------------------- /ext/daxpy.c: -------------------------------------------------------------------------------- 1 | #include "blas.h" 2 | 3 | int daxpy_(int *n, double *sa, double *sx, int *incx, double *sy, 4 | int *incy) 5 | { 6 | long int i, m, ix, iy, nn, iincx, iincy; 7 | register double ssa; 8 | 9 | /* constant times a vector plus a vector. 10 | uses unrolled loop for increments equal to one. 11 | jack dongarra, linpack, 3/11/78. 12 | modified 12/3/93, array(1) declarations changed to array(*) */ 13 | 14 | /* Dereference inputs */ 15 | nn = *n; 16 | ssa = *sa; 17 | iincx = *incx; 18 | iincy = *incy; 19 | 20 | if( nn > 0 && ssa != 0.0 ) 21 | { 22 | if (iincx == 1 && iincy == 1) /* code for both increments equal to 1 */ 23 | { 24 | m = nn-3; 25 | for (i = 0; i < m; i += 4) 26 | { 27 | sy[i] += ssa * sx[i]; 28 | sy[i+1] += ssa * sx[i+1]; 29 | sy[i+2] += ssa * sx[i+2]; 30 | sy[i+3] += ssa * sx[i+3]; 31 | } 32 | for ( ; i < nn; ++i) /* clean-up loop */ 33 | sy[i] += ssa * sx[i]; 34 | } 35 | else /* code for unequal increments or equal increments not equal to 1 */ 36 | { 37 | ix = iincx >= 0 ? 0 : (1 - nn) * iincx; 38 | iy = iincy >= 0 ? 0 : (1 - nn) * iincy; 39 | for (i = 0; i < nn; i++) 40 | { 41 | sy[iy] += ssa * sx[ix]; 42 | ix += iincx; 43 | iy += iincy; 44 | } 45 | } 46 | } 47 | 48 | return 0; 49 | } /* daxpy_ */ 50 | -------------------------------------------------------------------------------- /ext/ddot.c: -------------------------------------------------------------------------------- 1 | #include "blas.h" 2 | 3 | double ddot_(int *n, double *sx, int *incx, double *sy, int *incy) 4 | { 5 | long int i, m, nn, iincx, iincy; 6 | double stemp; 7 | long int ix, iy; 8 | 9 | /* forms the dot product of two vectors. 10 | uses unrolled loops for increments equal to one. 11 | jack dongarra, linpack, 3/11/78. 12 | modified 12/3/93, array(1) declarations changed to array(*) */ 13 | 14 | /* Dereference inputs */ 15 | nn = *n; 16 | iincx = *incx; 17 | iincy = *incy; 18 | 19 | stemp = 0.0; 20 | if (nn > 0) 21 | { 22 | if (iincx == 1 && iincy == 1) /* code for both increments equal to 1 */ 23 | { 24 | m = nn-4; 25 | for (i = 0; i < m; i += 5) 26 | stemp += sx[i] * sy[i] + sx[i+1] * sy[i+1] + sx[i+2] * sy[i+2] + 27 | sx[i+3] * sy[i+3] + sx[i+4] * sy[i+4]; 28 | 29 | for ( ; i < nn; i++) /* clean-up loop */ 30 | stemp += sx[i] * sy[i]; 31 | } 32 | else /* code for unequal increments or equal increments not equal to 1 */ 33 | { 34 | ix = 0; 35 | iy = 0; 36 | if (iincx < 0) 37 | ix = (1 - nn) * iincx; 38 | if (iincy < 0) 39 | iy = (1 - nn) * iincy; 40 | for (i = 0; i < nn; i++) 41 | { 42 | stemp += sx[ix] * sy[iy]; 43 | ix += iincx; 44 | iy += iincy; 45 | } 46 | } 47 | } 48 | 49 | return stemp; 50 | } /* ddot_ */ 51 | -------------------------------------------------------------------------------- /liblinear-1.8/blas/daxpy.c: -------------------------------------------------------------------------------- 1 | #include "blas.h" 2 | 3 | int daxpy_(int *n, double *sa, double *sx, int *incx, double *sy, 4 | int *incy) 5 | { 6 | long int i, m, ix, iy, nn, iincx, iincy; 7 | register double ssa; 8 | 9 | /* constant times a vector plus a vector. 10 | uses unrolled loop for increments equal to one. 11 | jack dongarra, linpack, 3/11/78. 12 | modified 12/3/93, array(1) declarations changed to array(*) */ 13 | 14 | /* Dereference inputs */ 15 | nn = *n; 16 | ssa = *sa; 17 | iincx = *incx; 18 | iincy = *incy; 19 | 20 | if( nn > 0 && ssa != 0.0 ) 21 | { 22 | if (iincx == 1 && iincy == 1) /* code for both increments equal to 1 */ 23 | { 24 | m = nn-3; 25 | for (i = 0; i < m; i += 4) 26 | { 27 | sy[i] += ssa * sx[i]; 28 | sy[i+1] += ssa * sx[i+1]; 29 | sy[i+2] += ssa * sx[i+2]; 30 | sy[i+3] += ssa * sx[i+3]; 31 | } 32 | for ( ; i < nn; ++i) /* clean-up loop */ 33 | sy[i] += ssa * sx[i]; 34 | } 35 | else /* code for unequal increments or equal increments not equal to 1 */ 36 | { 37 | ix = iincx >= 0 ? 0 : (1 - nn) * iincx; 38 | iy = iincy >= 0 ? 0 : (1 - nn) * iincy; 39 | for (i = 0; i < nn; i++) 40 | { 41 | sy[iy] += ssa * sx[ix]; 42 | ix += iincx; 43 | iy += iincy; 44 | } 45 | } 46 | } 47 | 48 | return 0; 49 | } /* daxpy_ */ 50 | -------------------------------------------------------------------------------- /liblinear-1.8/blas/ddot.c: -------------------------------------------------------------------------------- 1 | #include "blas.h" 2 | 3 | double ddot_(int *n, double *sx, int *incx, double *sy, int *incy) 4 | { 5 | long int i, m, nn, iincx, iincy; 6 | double stemp; 7 | long int ix, iy; 8 | 9 | /* forms the dot product of two vectors. 10 | uses unrolled loops for increments equal to one. 11 | jack dongarra, linpack, 3/11/78. 12 | modified 12/3/93, array(1) declarations changed to array(*) */ 13 | 14 | /* Dereference inputs */ 15 | nn = *n; 16 | iincx = *incx; 17 | iincy = *incy; 18 | 19 | stemp = 0.0; 20 | if (nn > 0) 21 | { 22 | if (iincx == 1 && iincy == 1) /* code for both increments equal to 1 */ 23 | { 24 | m = nn-4; 25 | for (i = 0; i < m; i += 5) 26 | stemp += sx[i] * sy[i] + sx[i+1] * sy[i+1] + sx[i+2] * sy[i+2] + 27 | sx[i+3] * sy[i+3] + sx[i+4] * sy[i+4]; 28 | 29 | for ( ; i < nn; i++) /* clean-up loop */ 30 | stemp += sx[i] * sy[i]; 31 | } 32 | else /* code for unequal increments or equal increments not equal to 1 */ 33 | { 34 | ix = 0; 35 | iy = 0; 36 | if (iincx < 0) 37 | ix = (1 - nn) * iincx; 38 | if (iincy < 0) 39 | iy = (1 - nn) * iincy; 40 | for (i = 0; i < nn; i++) 41 | { 42 | stemp += sx[ix] * sy[iy]; 43 | ix += iincx; 44 | iy += iincy; 45 | } 46 | } 47 | } 48 | 49 | return stemp; 50 | } /* ddot_ */ 51 | -------------------------------------------------------------------------------- /ext/dnrm2.c: -------------------------------------------------------------------------------- 1 | #include /* Needed for fabs() and sqrt() */ 2 | #include "blas.h" 3 | 4 | double dnrm2_(int *n, double *x, int *incx) 5 | { 6 | long int ix, nn, iincx; 7 | double norm, scale, absxi, ssq, temp; 8 | 9 | /* DNRM2 returns the euclidean norm of a vector via the function 10 | name, so that 11 | 12 | DNRM2 := sqrt( x'*x ) 13 | 14 | -- This version written on 25-October-1982. 15 | Modified on 14-October-1993 to inline the call to SLASSQ. 16 | Sven Hammarling, Nag Ltd. */ 17 | 18 | /* Dereference inputs */ 19 | nn = *n; 20 | iincx = *incx; 21 | 22 | if( nn > 0 && iincx > 0 ) 23 | { 24 | if (nn == 1) 25 | { 26 | norm = fabs(x[0]); 27 | } 28 | else 29 | { 30 | scale = 0.0; 31 | ssq = 1.0; 32 | 33 | /* The following loop is equivalent to this call to the LAPACK 34 | auxiliary routine: CALL SLASSQ( N, X, INCX, SCALE, SSQ ) */ 35 | 36 | for (ix=(nn-1)*iincx; ix>=0; ix-=iincx) 37 | { 38 | if (x[ix] != 0.0) 39 | { 40 | absxi = fabs(x[ix]); 41 | if (scale < absxi) 42 | { 43 | temp = scale / absxi; 44 | ssq = ssq * (temp * temp) + 1.0; 45 | scale = absxi; 46 | } 47 | else 48 | { 49 | temp = absxi / scale; 50 | ssq += temp * temp; 51 | } 52 | } 53 | } 54 | norm = scale * sqrt(ssq); 55 | } 56 | } 57 | else 58 | norm = 0.0; 59 | 60 | return norm; 61 | 62 | } /* dnrm2_ */ 63 | -------------------------------------------------------------------------------- /liblinear-1.8/blas/dnrm2.c: -------------------------------------------------------------------------------- 1 | #include /* Needed for fabs() and sqrt() */ 2 | #include "blas.h" 3 | 4 | double dnrm2_(int *n, double *x, int *incx) 5 | { 6 | long int ix, nn, iincx; 7 | double norm, scale, absxi, ssq, temp; 8 | 9 | /* DNRM2 returns the euclidean norm of a vector via the function 10 | name, so that 11 | 12 | DNRM2 := sqrt( x'*x ) 13 | 14 | -- This version written on 25-October-1982. 15 | Modified on 14-October-1993 to inline the call to SLASSQ. 16 | Sven Hammarling, Nag Ltd. */ 17 | 18 | /* Dereference inputs */ 19 | nn = *n; 20 | iincx = *incx; 21 | 22 | if( nn > 0 && iincx > 0 ) 23 | { 24 | if (nn == 1) 25 | { 26 | norm = fabs(x[0]); 27 | } 28 | else 29 | { 30 | scale = 0.0; 31 | ssq = 1.0; 32 | 33 | /* The following loop is equivalent to this call to the LAPACK 34 | auxiliary routine: CALL SLASSQ( N, X, INCX, SCALE, SSQ ) */ 35 | 36 | for (ix=(nn-1)*iincx; ix>=0; ix-=iincx) 37 | { 38 | if (x[ix] != 0.0) 39 | { 40 | absxi = fabs(x[ix]); 41 | if (scale < absxi) 42 | { 43 | temp = scale / absxi; 44 | ssq = ssq * (temp * temp) + 1.0; 45 | scale = absxi; 46 | } 47 | else 48 | { 49 | temp = absxi / scale; 50 | ssq += temp * temp; 51 | } 52 | } 53 | } 54 | norm = scale * sqrt(ssq); 55 | } 56 | } 57 | else 58 | norm = 0.0; 59 | 60 | return norm; 61 | 62 | } /* dnrm2_ */ 63 | -------------------------------------------------------------------------------- /liblinear-1.8/COPYRIGHT: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2007-2011 The LIBLINEAR Project. 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 16 | 3. Neither name of copyright holders nor the names of its contributors 17 | may be used to endorse or promote products derived from this software 18 | without specific prior written permission. 19 | 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 25 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/Makefile: -------------------------------------------------------------------------------- 1 | # This Makefile is used under Linux 2 | 3 | MATLABDIR ?= /usr/local/matlab 4 | CXX ?= g++ 5 | #CXX = g++-3.3 6 | CC ?= gcc 7 | CFLAGS = -Wall -Wconversion -O3 -fPIC -I$(MATLABDIR)/extern/include -I.. 8 | 9 | MEX = $(MATLABDIR)/bin/mex 10 | MEX_OPTION = CC\#$(CXX) CXX\#$(CXX) CFLAGS\#"$(CFLAGS)" CXXFLAGS\#"$(CFLAGS)" 11 | # comment the following line if you use MATLAB on a 32-bit computer 12 | MEX_OPTION += -largeArrayDims 13 | MEX_EXT = $(shell $(MATLABDIR)/bin/mexext) 14 | 15 | OCTAVEDIR ?= /usr/include/octave 16 | OCTAVE_MEX = env CC=$(CXX) mkoctfile 17 | OCTAVE_MEX_OPTION = --mex 18 | OCTAVE_MEX_EXT = mex 19 | OCTAVE_CFLAGS = -Wall -O3 -fPIC -I$(OCTAVEDIR) -I.. 20 | 21 | all: matlab 22 | 23 | matlab: binary 24 | 25 | octave: 26 | @make MEX="$(OCTAVE_MEX)" MEX_OPTION="$(OCTAVE_MEX_OPTION)" \ 27 | MEX_EXT="$(OCTAVE_MEX_EXT)" CFLAGS="$(OCTAVE_CFLAGS)" \ 28 | binary 29 | 30 | binary: train.$(MEX_EXT) predict.$(MEX_EXT) libsvmread.$(MEX_EXT) libsvmwrite.$(MEX_EXT) 31 | 32 | train.$(MEX_EXT): train.c ../linear.h ../tron.o ../linear.o linear_model_matlab.o ../blas/blas.a 33 | $(MEX) $(MEX_OPTION) train.c ../tron.o ../linear.o linear_model_matlab.o ../blas/blas.a 34 | 35 | predict.$(MEX_EXT): predict.c ../linear.h ../tron.o ../linear.o linear_model_matlab.o ../blas/blas.a 36 | $(MEX) $(MEX_OPTION) predict.c ../tron.o ../linear.o linear_model_matlab.o ../blas/blas.a 37 | 38 | libsvmread.$(MEX_EXT): libsvmread.c 39 | $(MEX) $(MEX_OPTION) libsvmread.c 40 | 41 | libsvmwrite.$(MEX_EXT): libsvmwrite.c 42 | $(MEX) $(MEX_OPTION) libsvmwrite.c 43 | 44 | linear_model_matlab.o: linear_model_matlab.c ../linear.h 45 | $(CXX) $(CFLAGS) -c linear_model_matlab.c 46 | 47 | ../linear.o: 48 | cd ..; make linear.o 49 | 50 | ../tron.o: 51 | cd ..; make tron.o 52 | 53 | ../blas/blas.a: 54 | cd ../blas; make OPTFLAGS='$(CFLAGS)' CC='$(CC)'; 55 | 56 | clean: 57 | cd ../blas; make clean 58 | rm -f *~ *.o *.mex* *.obj ../linear.o ../tron.o 59 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/test.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby1.8 2 | require 'rubygems' 3 | require 'linear' 4 | Liblinear::info_on = 1 5 | 6 | puts "TEST of the Ruby LIBLINEAR bindings" 7 | puts "------------------------------------" 8 | pa = LParameter.new 9 | pa.C = 1 10 | pa.solver_type = L2R_LR 11 | pa.eps= 0.1 12 | pa.nr_weight = 0 13 | #pa.weight_label = [] 14 | #pa.weight = [] 15 | bias = 1 16 | 17 | labels_m = [1, 2, 1, 2, 3] 18 | labels_2 = [1, 1, -1, -1, 1] 19 | samples = [ 20 | {1=>0,2=>0.1,3=>0.2,4=>0,5=>0}, 21 | {1=>0,2=>0.1,3=>0.3,4=>-1.2,5=>0}, 22 | {1=>0.4,2=>0,3=>0,4=>0,5=>0}, 23 | {1=>0,2=>0.1,3=>0,4=>1.4,5=>0.5}, 24 | {1=>-0.1,2=>-0.2,3=>0.1,4=>1.1,5=>0.1} 25 | ] 26 | solvers = [ L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR ] 27 | snames= [ 'L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL', 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR' ] 28 | #solvers = [ L2_LR ] 29 | #snames= [ 'L2_LR' ] 30 | #solvers = [ L2LOSS_SVM_DUAL, L1LOSS_SVM_DUAL, MCSVM_CS ] 31 | #snames= [ 'L2LOSS_SVM_DUAL', 'L1LOSS_SVM_DUAL', 'MCSVM_CS' ] 32 | solvers.each_index { |j| 33 | 34 | if solvers[j] == MCSVM_CS 35 | labels = labels_m 36 | else 37 | labels = labels_2 38 | end 39 | sp = LProblem.new(labels,samples,bias) 40 | pa.solver_type = solvers[j] 41 | m = LModel.new(sp, pa) 42 | # m = LModel.new("../heart_scale.model") 43 | # m.save(snames[j]+".model") 44 | ec = 0 45 | labels.each_index { |i| 46 | if pa.solver_type == L2R_LR and not m.probability.nil? 47 | pred, probs = m.predict_probability(samples[i]) 48 | puts "Got #{pred} and #{probs.join(',')} for sample: [#{samples[i]}] Label: #{labels[i]} Pred: #{pred} Kernel: #{snames[j]}" 49 | else 50 | pred = m.predict(samples[i]) 51 | puts "Got #{pred} for sample: [#{samples[i]}] Label: #{labels[i]} Kernel: #{snames[j]}" 52 | end 53 | ec += 1 if labels[i] != pred 54 | } 55 | puts "Kernel #{snames[j]} made #{ec} errors" 56 | } 57 | -------------------------------------------------------------------------------- /ext/linear.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIBLINEAR_H 2 | #define _LIBLINEAR_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | struct feature_node 9 | { 10 | int index; 11 | double value; 12 | }; 13 | 14 | struct problem 15 | { 16 | int l, n; 17 | int *y; 18 | struct feature_node **x; 19 | double bias; /* < 0 if no bias term */ 20 | }; 21 | 22 | enum { L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL }; /* solver_type */ 23 | 24 | struct parameter 25 | { 26 | int solver_type; 27 | 28 | /* these are for training only */ 29 | double eps; /* stopping criteria */ 30 | double C; 31 | int nr_weight; 32 | int *weight_label; 33 | double* weight; 34 | }; 35 | 36 | struct model 37 | { 38 | struct parameter param; 39 | int nr_class; /* number of classes */ 40 | int nr_feature; 41 | double *w; 42 | int *label; /* label of each class */ 43 | double bias; 44 | }; 45 | 46 | struct model* train(const struct problem *prob, const struct parameter *param); 47 | void cross_validation(const struct problem *prob, const struct parameter *param, int nr_fold, int *target); 48 | 49 | int predict_values(const struct model *model_, const struct feature_node *x, double* dec_values); 50 | int predict(const struct model *model_, const struct feature_node *x); 51 | int predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates); 52 | 53 | int save_model(const char *model_file_name, const struct model *model_); 54 | struct model *load_model(const char *model_file_name); 55 | 56 | int get_nr_feature(const struct model *model_); 57 | int get_nr_class(const struct model *model_); 58 | void get_labels(const struct model *model_, int* label); 59 | 60 | void free_model_content(struct model *model_ptr); 61 | void free_and_destroy_model(struct model **model_ptr_ptr); 62 | void destroy_param(struct parameter *param); 63 | 64 | const char *check_parameter(const struct problem *prob, const struct parameter *param); 65 | int check_probability_model(const struct model *model); 66 | void set_print_string_function(void (*print_func) (const char*)); 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | 72 | #endif /* _LIBLINEAR_H */ 73 | 74 | -------------------------------------------------------------------------------- /liblinear-1.8/linear.h: -------------------------------------------------------------------------------- 1 | #ifndef _LIBLINEAR_H 2 | #define _LIBLINEAR_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | struct feature_node 9 | { 10 | int index; 11 | double value; 12 | }; 13 | 14 | struct problem 15 | { 16 | int l, n; 17 | int *y; 18 | struct feature_node **x; 19 | double bias; /* < 0 if no bias term */ 20 | }; 21 | 22 | enum { L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL }; /* solver_type */ 23 | 24 | struct parameter 25 | { 26 | int solver_type; 27 | 28 | /* these are for training only */ 29 | double eps; /* stopping criteria */ 30 | double C; 31 | int nr_weight; 32 | int *weight_label; 33 | double* weight; 34 | }; 35 | 36 | struct model 37 | { 38 | struct parameter param; 39 | int nr_class; /* number of classes */ 40 | int nr_feature; 41 | double *w; 42 | int *label; /* label of each class */ 43 | double bias; 44 | }; 45 | 46 | struct model* train(const struct problem *prob, const struct parameter *param); 47 | void cross_validation(const struct problem *prob, const struct parameter *param, int nr_fold, int *target); 48 | 49 | int predict_values(const struct model *model_, const struct feature_node *x, double* dec_values); 50 | int predict(const struct model *model_, const struct feature_node *x); 51 | int predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates); 52 | 53 | int save_model(const char *model_file_name, const struct model *model_); 54 | struct model *load_model(const char *model_file_name); 55 | 56 | int get_nr_feature(const struct model *model_); 57 | int get_nr_class(const struct model *model_); 58 | void get_labels(const struct model *model_, int* label); 59 | 60 | void free_model_content(struct model *model_ptr); 61 | void free_and_destroy_model(struct model **model_ptr_ptr); 62 | void destroy_param(struct parameter *param); 63 | 64 | const char *check_parameter(const struct problem *prob, const struct parameter *param); 65 | int check_probability_model(const struct model *model); 66 | void set_print_string_function(void (*print_func) (const char*)); 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | 72 | #endif /* _LIBLINEAR_H */ 73 | 74 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/liblinear_test.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'rubygems' 3 | require 'linear' 4 | require 'pp' 5 | 6 | Liblinear::info_on = 1 7 | 8 | # a three-class problem 9 | labels = [0, 1, 1, 1] 10 | #samples = [[0, 0], [0, 1], [1, 0], [1, 1]] 11 | samples = [{1=>0, 2=>0}, {1=>0, 2=>1}, {1=>1, 2=>0}, {1=>1, 2=>1}] 12 | bias = 1 13 | problem = LProblem.new(labels, samples, bias) 14 | size = samples.size 15 | 16 | solvers = [ L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR ] 17 | snames= [ 'L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL', 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR' ] 18 | 19 | param = LParameter.new('C' => 1,'nr_weight' => 2,'weight_label' => [1,0],'weight' => [10,1]) 20 | for k in solvers 21 | param.solver_type = k 22 | model = LModel.new(problem,param) 23 | model.save(snames[k]+".model") 24 | errors = 0 25 | for i in (0..size-1) 26 | prediction = model.predict(samples[i]) 27 | probability = model.predict_probability(samples[i]) 28 | if (labels[i] != prediction) 29 | errors = errors + 1 30 | end 31 | end 32 | puts "##########################################" 33 | puts " solver #{snames[k]}: error rate = #{errors} / #{size}" 34 | puts "##########################################" 35 | end 36 | 37 | param = LParameter.new('solver_type' => L2R_LR, 'C'=>10) 38 | model = LModel.new(problem, param) 39 | puts "##########################################" 40 | puts " Decision values of predicting #{samples[0].inspect}" 41 | puts "##########################################" 42 | 43 | puts "Numer of Classes:" + model.get_nr_class().to_s 44 | d = model.predict_values(samples[0]) 45 | #puts d.inspect 46 | for i in model.get_labels 47 | for j in model.get_labels 48 | if j>i 49 | puts "{#{i}, #{j}} = #{d[i][j]}" 50 | end 51 | end 52 | end 53 | 54 | param = LParameter.new('solver_type' => L2R_LR, 'C'=>10)#, 'probability' => 1) 55 | model = LModel.new(problem, param) 56 | pred_label, pred_probability = model.predict_probability(samples[1]) 57 | puts "##########################################" 58 | puts " Probability estimate of predicting #{samples[1].inspect}" 59 | puts "##########################################" 60 | puts "predicted class: #{pred_label}" 61 | for i in model.get_labels 62 | puts "prob(label=#{i}) = #{pred_probability[i]}" 63 | end 64 | 65 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/libsvmwrite.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "mex.h" 5 | 6 | #if MX_API_VER < 0x07030000 7 | typedef int mwIndex; 8 | #endif 9 | 10 | void exit_with_help() 11 | { 12 | mexPrintf( 13 | "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n" 14 | ); 15 | } 16 | 17 | void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat) 18 | { 19 | FILE *fp = fopen(filename,"w"); 20 | int i, k, low, high, l; 21 | mwIndex *ir, *jc; 22 | int label_vector_row_num; 23 | double *samples, *labels; 24 | mxArray *instance_mat_col; // instance sparse matrix in column format 25 | 26 | if(fp ==NULL) 27 | { 28 | mexPrintf("can't open output file %s\n",filename); 29 | return; 30 | } 31 | 32 | // transpose instance matrix 33 | { 34 | mxArray *prhs[1], *plhs[1]; 35 | prhs[0] = mxDuplicateArray(instance_mat); 36 | if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")) 37 | { 38 | mexPrintf("Error: cannot transpose instance matrix\n"); 39 | return; 40 | } 41 | instance_mat_col = plhs[0]; 42 | mxDestroyArray(prhs[0]); 43 | } 44 | 45 | // the number of instance 46 | l = (int) mxGetN(instance_mat_col); 47 | label_vector_row_num = (int) mxGetM(label_vec); 48 | 49 | if(label_vector_row_num!=l) 50 | { 51 | mexPrintf("Length of label vector does not match # of instances.\n"); 52 | return; 53 | } 54 | 55 | // each column is one instance 56 | labels = mxGetPr(label_vec); 57 | samples = mxGetPr(instance_mat_col); 58 | ir = mxGetIr(instance_mat_col); 59 | jc = mxGetJc(instance_mat_col); 60 | 61 | for(i=0;i require 'rubygems' 33 | irb(main):002:0> require 'linear' 34 | irb(main):003:0> pa = LParameter.new 35 | irb(main):004:0> pa.solver_type = MCSVM_CS 36 | irb(main):005:0> pa.eps = 0.1 37 | irb(main):006:0> bias = 1 38 | irb(main):007:0> labels = [1, 2, 1, 2, 3] 39 | irb(main):008:0> samples = [ 40 | irb(main):009:1* {1=>0,2=>0.1,3=>0.2,4=>0,5=>0}, 41 | irb(main):010:1* {1=>0,2=>0.1,3=>0.3,4=>-1.2,5=>0}, 42 | irb(main):011:1* {1=>0.4,2=>0,3=>0,4=>0,5=>0}, 43 | irb(main):012:1* {1=>0,2=>0.1,3=>0,4=>1.4,5=>0.5}, 44 | irb(main):013:1* {1=>-0.1,2=>-0.2,3=>0.1,4=>1.1,5=>0.1} 45 | irb(main):014:1> ] 46 | irb(main):016:0> sp = LProblem.new(labels,samples,bias) 47 | irb(main):017:0> m = LModel.new(sp, pa) 48 | irb(main):018:0> pred = m.predict({1=>1,2=>0.1,3=>0.2,4=>0,5=>0}) 49 | => 1 50 | irb(main):019:0> pred = m.predict({1=>0,2=>0.1,3=>0.2,4=>0,5=>0}) 51 | => 2 52 | irb(main):020:0> pred = m.predict({1=>0,2=>0.1,3=>0.2,4=>0,5=>0}) 53 | => 2 54 | irb(main):025:0> pred = m.predict({1=>0.4,2=>0,3=>0,4=>0,5=>0}) 55 | => 1 56 | irb(main):021:0> pred = m.predict({1=>-0.1,2=>-0.2,3=>0.1,4=>1.1,5=>0.1}) 57 | => 3 58 | irb(main):022:0> m.save("test.model") 59 | 60 | 61 | For more examples see test*.rb in the liblinear-ruby-swig/liblinear-1.8/ruby 62 | directory 63 | 64 | == AUTHOR: 65 | 66 | Tom Zeng 67 | - http://twitter.com/tomzeng 68 | - http://www.tomzconsulting.com 69 | - http://www.linkedin.com/in/tomzeng 70 | - tom.z.zeng _at_ gmail _dot_ com 71 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/liblinear.i: -------------------------------------------------------------------------------- 1 | %module liblinear 2 | %{ 3 | #include "linear.h" 4 | %} 5 | 6 | struct feature_node 7 | { 8 | int index; 9 | double value; 10 | }; 11 | 12 | struct problem 13 | { 14 | int l, n; 15 | int *y; 16 | struct feature_node **x; 17 | double bias; /* < 0 if no bias term */ 18 | }; 19 | 20 | enum { L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL }; /* solver_type */ 21 | 22 | struct parameter 23 | { 24 | int solver_type; 25 | 26 | /* these are for training only */ 27 | double eps; /* stopping criteria */ 28 | double C; 29 | int nr_weight; 30 | int *weight_label; 31 | double* weight; 32 | }; 33 | 34 | struct model 35 | { 36 | struct parameter param; 37 | int nr_class; /* number of classes */ 38 | int nr_feature; 39 | double *w; 40 | int *label; /* label of each class (label[n]) */ 41 | double bias; 42 | }; 43 | 44 | struct model* train(const struct problem *prob, const struct parameter *param); 45 | void cross_validation(const struct problem *prob, const struct parameter *param, int nr_fold, int *target); 46 | 47 | int predict_values(const struct model *model_, const struct feature_node *x, double* dec_values); 48 | int predict(const struct model *model_, const struct feature_node *x); 49 | int predict_probability(const struct model *model_, const struct feature_node *x, double* prob_estimates); 50 | 51 | int save_model(const char *model_file_name, const struct model *model_); 52 | struct model *load_model(const char *model_file_name); 53 | 54 | int get_nr_feature(const struct model *model_); 55 | int get_nr_class(const struct model *model_); 56 | void get_labels(const struct model *model_, int* label); 57 | 58 | void free_model_content(struct model *model_ptr); 59 | void free_and_destroy_model(struct model **model_ptr_ptr); 60 | void destroy_param(struct parameter *param); 61 | 62 | const char *check_parameter(const struct problem *prob, const struct parameter *param); 63 | int check_probability_model(const struct model *model); 64 | void set_print_string_function(void (*print_func) (const char*)); 65 | 66 | %include carrays.i 67 | %array_functions(int,int) 68 | %array_functions(double,double) 69 | 70 | 71 | %inline %{ 72 | extern int info_on; 73 | 74 | struct feature_node *feature_node_array(int size) 75 | { 76 | return (struct feature_node *)malloc(sizeof(struct feature_node)*size); 77 | } 78 | 79 | void feature_node_array_set(struct feature_node *array, int i, int index, double value) 80 | { 81 | array[i].index = index; 82 | array[i].value = value; 83 | } 84 | 85 | void feature_node_array_destroy(struct feature_node *array) 86 | { 87 | free(array); 88 | } 89 | 90 | struct feature_node **feature_node_matrix(int size) 91 | { 92 | return (struct feature_node **)malloc(sizeof(struct feature_node *)*size); 93 | } 94 | 95 | void feature_node_matrix_set(struct feature_node **matrix, int i, struct feature_node* array) 96 | { 97 | matrix[i] = array; 98 | } 99 | 100 | void set_bias_index(struct feature_node **matrix, int size, int maxlen, int *len_array) 101 | { 102 | int i; 103 | for(i=0; iindex = maxlen+1; 105 | } 106 | 107 | void feature_node_matrix_destroy(struct feature_node **matrix) 108 | { 109 | free(matrix); 110 | } 111 | 112 | %} 113 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/linear_model_matlab.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "linear.h" 4 | 5 | #include "mex.h" 6 | 7 | #if MX_API_VER < 0x07030000 8 | typedef int mwIndex; 9 | #endif 10 | 11 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 12 | 13 | #define NUM_OF_RETURN_FIELD 6 14 | 15 | static const char *field_names[] = { 16 | "Parameters", 17 | "nr_class", 18 | "nr_feature", 19 | "bias", 20 | "Label", 21 | "w", 22 | }; 23 | 24 | const char *model_to_matlab_structure(mxArray *plhs[], struct model *model_) 25 | { 26 | int i; 27 | int nr_w; 28 | double *ptr; 29 | mxArray *return_model, **rhs; 30 | int out_id = 0; 31 | int n, w_size; 32 | 33 | rhs = (mxArray **)mxMalloc(sizeof(mxArray *)*NUM_OF_RETURN_FIELD); 34 | 35 | // Parameters 36 | // for now, only solver_type is needed 37 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 38 | ptr = mxGetPr(rhs[out_id]); 39 | ptr[0] = model_->param.solver_type; 40 | out_id++; 41 | 42 | // nr_class 43 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 44 | ptr = mxGetPr(rhs[out_id]); 45 | ptr[0] = model_->nr_class; 46 | out_id++; 47 | 48 | if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) 49 | nr_w=1; 50 | else 51 | nr_w=model_->nr_class; 52 | 53 | // nr_feature 54 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 55 | ptr = mxGetPr(rhs[out_id]); 56 | ptr[0] = model_->nr_feature; 57 | out_id++; 58 | 59 | // bias 60 | rhs[out_id] = mxCreateDoubleMatrix(1, 1, mxREAL); 61 | ptr = mxGetPr(rhs[out_id]); 62 | ptr[0] = model_->bias; 63 | out_id++; 64 | 65 | if(model_->bias>=0) 66 | n=model_->nr_feature+1; 67 | else 68 | n=model_->nr_feature; 69 | 70 | w_size = n; 71 | // Label 72 | if(model_->label) 73 | { 74 | rhs[out_id] = mxCreateDoubleMatrix(model_->nr_class, 1, mxREAL); 75 | ptr = mxGetPr(rhs[out_id]); 76 | for(i = 0; i < model_->nr_class; i++) 77 | ptr[i] = model_->label[i]; 78 | } 79 | else 80 | rhs[out_id] = mxCreateDoubleMatrix(0, 0, mxREAL); 81 | out_id++; 82 | 83 | // w 84 | rhs[out_id] = mxCreateDoubleMatrix(nr_w, w_size, mxREAL); 85 | ptr = mxGetPr(rhs[out_id]); 86 | for(i = 0; i < w_size*nr_w; i++) 87 | ptr[i]=model_->w[i]; 88 | out_id++; 89 | 90 | /* Create a struct matrix contains NUM_OF_RETURN_FIELD fields */ 91 | return_model = mxCreateStructMatrix(1, 1, NUM_OF_RETURN_FIELD, field_names); 92 | 93 | /* Fill struct matrix with input arguments */ 94 | for(i = 0; i < NUM_OF_RETURN_FIELD; i++) 95 | mxSetField(return_model,0,field_names[i],mxDuplicateArray(rhs[i])); 96 | /* return */ 97 | plhs[0] = return_model; 98 | mxFree(rhs); 99 | 100 | return NULL; 101 | } 102 | 103 | const char *matlab_matrix_to_model(struct model *model_, const mxArray *matlab_struct) 104 | { 105 | int i, num_of_fields; 106 | int nr_w; 107 | double *ptr; 108 | int id = 0; 109 | int n, w_size; 110 | mxArray **rhs; 111 | 112 | num_of_fields = mxGetNumberOfFields(matlab_struct); 113 | rhs = (mxArray **) mxMalloc(sizeof(mxArray *)*num_of_fields); 114 | 115 | for(i=0;inr_class=0; 119 | nr_w=0; 120 | model_->nr_feature=0; 121 | model_->w=NULL; 122 | model_->label=NULL; 123 | 124 | // Parameters 125 | ptr = mxGetPr(rhs[id]); 126 | model_->param.solver_type = (int)ptr[0]; 127 | id++; 128 | 129 | // nr_class 130 | ptr = mxGetPr(rhs[id]); 131 | model_->nr_class = (int)ptr[0]; 132 | id++; 133 | 134 | if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS) 135 | nr_w=1; 136 | else 137 | nr_w=model_->nr_class; 138 | 139 | // nr_feature 140 | ptr = mxGetPr(rhs[id]); 141 | model_->nr_feature = (int)ptr[0]; 142 | id++; 143 | 144 | // bias 145 | ptr = mxGetPr(rhs[id]); 146 | model_->bias = (int)ptr[0]; 147 | id++; 148 | 149 | if(model_->bias>=0) 150 | n=model_->nr_feature+1; 151 | else 152 | n=model_->nr_feature; 153 | w_size = n; 154 | 155 | ptr = mxGetPr(rhs[id]); 156 | model_->label=Malloc(int, model_->nr_class); 157 | for(i=0; inr_class; i++) 158 | model_->label[i]=(int)ptr[i]; 159 | id++; 160 | 161 | ptr = mxGetPr(rhs[id]); 162 | model_->w=Malloc(double, w_size*nr_w); 163 | for(i = 0; i < w_size*nr_w; i++) 164 | model_->w[i]=ptr[i]; 165 | id++; 166 | mxFree(rhs); 167 | 168 | return NULL; 169 | } 170 | 171 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/libsvmread.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "mex.h" 8 | 9 | #if MX_API_VER < 0x07030000 10 | typedef int mwIndex; 11 | #endif 12 | #ifndef max 13 | #define max(x,y) (((x)>(y))?(x):(y)) 14 | #endif 15 | #ifndef min 16 | #define min(x,y) (((x)<(y))?(x):(y)) 17 | #endif 18 | 19 | void exit_with_help() 20 | { 21 | mexPrintf( 22 | "Usage: [label_vector, instance_matrix] = libsvmread('filename');\n" 23 | ); 24 | } 25 | 26 | static void fake_answer(mxArray *plhs[]) 27 | { 28 | plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); 29 | plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL); 30 | } 31 | 32 | static char *line; 33 | static int max_line_len; 34 | 35 | static char* readline(FILE *input) 36 | { 37 | int len; 38 | 39 | if(fgets(line,max_line_len,input) == NULL) 40 | return NULL; 41 | 42 | while(strrchr(line,'\n') == NULL) 43 | { 44 | max_line_len *= 2; 45 | line = (char *) realloc(line, max_line_len); 46 | len = (int) strlen(line); 47 | if(fgets(line+len,max_line_len-len,input) == NULL) 48 | break; 49 | } 50 | return line; 51 | } 52 | 53 | // read in a problem (in libsvm format) 54 | void read_problem(const char *filename, mxArray *plhs[]) 55 | { 56 | int max_index, min_index, inst_max_index, i; 57 | long elements, k; 58 | FILE *fp = fopen(filename,"r"); 59 | int l = 0; 60 | char *endptr; 61 | mwIndex *ir, *jc; 62 | double *labels, *samples; 63 | 64 | if(fp == NULL) 65 | { 66 | mexPrintf("can't open input file %s\n",filename); 67 | fake_answer(plhs); 68 | return; 69 | } 70 | 71 | max_line_len = 1024; 72 | line = (char *) malloc(max_line_len*sizeof(char)); 73 | 74 | max_index = 0; 75 | min_index = 1; // our index starts from 1 76 | elements = 0; 77 | while(readline(fp) != NULL) 78 | { 79 | char *idx, *val; 80 | // features 81 | int index = 0; 82 | 83 | inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has start from 0 84 | strtok(line," \t"); // label 85 | while (1) 86 | { 87 | idx = strtok(NULL,":"); // index:value 88 | val = strtok(NULL," \t"); 89 | if(val == NULL) 90 | break; 91 | 92 | errno = 0; 93 | index = (int) strtol(idx,&endptr,10); 94 | if(endptr == idx || errno != 0 || *endptr != '\0' || index <= inst_max_index) 95 | { 96 | mexPrintf("Wrong input format at line %d\n",l+1); 97 | fake_answer(plhs); 98 | return; 99 | } 100 | else 101 | inst_max_index = index; 102 | 103 | min_index = min(min_index, index); 104 | elements++; 105 | } 106 | max_index = max(max_index, inst_max_index); 107 | l++; 108 | } 109 | rewind(fp); 110 | 111 | // y 112 | plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL); 113 | // x^T 114 | if (min_index <= 0) 115 | plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL); 116 | else 117 | plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL); 118 | 119 | labels = mxGetPr(plhs[0]); 120 | samples = mxGetPr(plhs[1]); 121 | ir = mxGetIr(plhs[1]); 122 | jc = mxGetJc(plhs[1]); 123 | 124 | k=0; 125 | for(i=0;i start from 0 156 | 157 | errno = 0; 158 | samples[k] = strtod(val,&endptr); 159 | if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) 160 | { 161 | mexPrintf("Wrong input format at line %d\n",i+1); 162 | fake_answer(plhs); 163 | return; 164 | } 165 | ++k; 166 | } 167 | } 168 | jc[l] = k; 169 | 170 | fclose(fp); 171 | free(line); 172 | 173 | { 174 | mxArray *rhs[1], *lhs[1]; 175 | rhs[0] = plhs[1]; 176 | if(mexCallMATLAB(1, lhs, 1, rhs, "transpose")) 177 | { 178 | mexPrintf("Error: cannot transpose problem\n"); 179 | fake_answer(plhs); 180 | return; 181 | } 182 | plhs[1] = lhs[0]; 183 | } 184 | } 185 | 186 | void mexFunction( int nlhs, mxArray *plhs[], 187 | int nrhs, const mxArray *prhs[] ) 188 | { 189 | if(nrhs == 1) 190 | { 191 | char filename[256]; 192 | 193 | mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1); 194 | 195 | if(filename == NULL) 196 | { 197 | mexPrintf("Error: filename is NULL\n"); 198 | return; 199 | } 200 | 201 | read_problem(filename, plhs); 202 | } 203 | else 204 | { 205 | exit_with_help(); 206 | fake_answer(plhs); 207 | return; 208 | } 209 | } 210 | 211 | -------------------------------------------------------------------------------- /liblinear-1.8/predict.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "linear.h" 7 | 8 | struct feature_node *x; 9 | int max_nr_attr = 64; 10 | 11 | struct model* model_; 12 | int flag_predict_probability=0; 13 | 14 | void exit_input_error(int line_num) 15 | { 16 | fprintf(stderr,"Wrong input format at line %d\n", line_num); 17 | exit(1); 18 | } 19 | 20 | static char *line = NULL; 21 | static int max_line_len; 22 | 23 | static char* readline(FILE *input) 24 | { 25 | int len; 26 | 27 | if(fgets(line,max_line_len,input) == NULL) 28 | return NULL; 29 | 30 | while(strrchr(line,'\n') == NULL) 31 | { 32 | max_line_len *= 2; 33 | line = (char *) realloc(line,max_line_len); 34 | len = (int) strlen(line); 35 | if(fgets(line+len,max_line_len-len,input) == NULL) 36 | break; 37 | } 38 | return line; 39 | } 40 | 41 | void do_predict(FILE *input, FILE *output, struct model* model_) 42 | { 43 | int correct = 0; 44 | int total = 0; 45 | 46 | int nr_class=get_nr_class(model_); 47 | double *prob_estimates=NULL; 48 | int j, n; 49 | int nr_feature=get_nr_feature(model_); 50 | if(model_->bias>=0) 51 | n=nr_feature+1; 52 | else 53 | n=nr_feature; 54 | 55 | if(flag_predict_probability) 56 | { 57 | int *labels; 58 | 59 | if(!check_probability_model(model_)) 60 | { 61 | fprintf(stderr, "probability output is only supported for logistic regression\n"); 62 | exit(1); 63 | } 64 | 65 | labels=(int *) malloc(nr_class*sizeof(int)); 66 | get_labels(model_,labels); 67 | prob_estimates = (double *) malloc(nr_class*sizeof(double)); 68 | fprintf(output,"labels"); 69 | for(j=0;j=max_nr_attr-2) // need one more for index = -1 95 | { 96 | max_nr_attr *= 2; 97 | x = (struct feature_node *) realloc(x,max_nr_attr*sizeof(struct feature_node)); 98 | } 99 | 100 | idx = strtok(NULL,":"); 101 | val = strtok(NULL," \t"); 102 | 103 | if(val == NULL) 104 | break; 105 | errno = 0; 106 | x[i].index = (int) strtol(idx,&endptr,10); 107 | if(endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index) 108 | exit_input_error(total+1); 109 | else 110 | inst_max_index = x[i].index; 111 | 112 | errno = 0; 113 | x[i].value = strtod(val,&endptr); 114 | if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) 115 | exit_input_error(total+1); 116 | 117 | // feature indices larger than those in training are not used 118 | if(x[i].index <= nr_feature) 119 | ++i; 120 | } 121 | 122 | if(model_->bias>=0) 123 | { 124 | x[i].index = n; 125 | x[i].value = model_->bias; 126 | i++; 127 | } 128 | x[i].index = -1; 129 | 130 | if(flag_predict_probability) 131 | { 132 | int j; 133 | predict_label = predict_probability(model_,x,prob_estimates); 134 | fprintf(output,"%d",predict_label); 135 | for(j=0;jnr_class;j++) 136 | fprintf(output," %g",prob_estimates[j]); 137 | fprintf(output,"\n"); 138 | } 139 | else 140 | { 141 | predict_label = predict(model_,x); 142 | fprintf(output,"%d\n",predict_label); 143 | } 144 | 145 | if(predict_label == target_label) 146 | ++correct; 147 | ++total; 148 | } 149 | printf("Accuracy = %g%% (%d/%d)\n",(double) correct/total*100,correct,total); 150 | if(flag_predict_probability) 151 | free(prob_estimates); 152 | } 153 | 154 | void exit_with_help() 155 | { 156 | printf( 157 | "Usage: predict [options] test_file model_file output_file\n" 158 | "options:\n" 159 | "-b probability_estimates: whether to output probability estimates, 0 or 1 (default 0)\n" 160 | ); 161 | exit(1); 162 | } 163 | 164 | int main(int argc, char **argv) 165 | { 166 | FILE *input, *output; 167 | int i; 168 | 169 | // parse options 170 | for(i=1;i=argc) 187 | exit_with_help(); 188 | 189 | input = fopen(argv[i],"r"); 190 | if(input == NULL) 191 | { 192 | fprintf(stderr,"can't open input file %s\n",argv[i]); 193 | exit(1); 194 | } 195 | 196 | output = fopen(argv[i+2],"w"); 197 | if(output == NULL) 198 | { 199 | fprintf(stderr,"can't open output file %s\n",argv[i+2]); 200 | exit(1); 201 | } 202 | 203 | if((model_=load_model(argv[i+1]))==0) 204 | { 205 | fprintf(stderr,"can't open model file %s\n",argv[i+1]); 206 | exit(1); 207 | } 208 | 209 | x = (struct feature_node *) malloc(max_nr_attr*sizeof(struct feature_node)); 210 | do_predict(input, output, model_); 211 | free_and_destroy_model(&model_); 212 | free(line); 213 | free(x); 214 | fclose(input); 215 | fclose(output); 216 | return 0; 217 | } 218 | 219 | -------------------------------------------------------------------------------- /ext/tron.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "tron.h" 6 | 7 | #ifndef min 8 | template static inline T min(T x,T y) { return (x static inline T max(T x,T y) { return (x>y)?x:y; } 13 | #endif 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | 19 | extern double dnrm2_(int *, double *, int *); 20 | extern double ddot_(int *, double *, int *, double *, int *); 21 | extern int daxpy_(int *, double *, double *, int *, double *, int *); 22 | extern int dscal_(int *, double *, double *, int *); 23 | 24 | #ifdef __cplusplus 25 | } 26 | #endif 27 | 28 | static void default_print(const char *buf) 29 | { 30 | fputs(buf,stdout); 31 | fflush(stdout); 32 | } 33 | 34 | void TRON::info(const char *fmt,...) 35 | { 36 | char buf[BUFSIZ]; 37 | va_list ap; 38 | va_start(ap,fmt); 39 | vsprintf(buf,fmt,ap); 40 | va_end(ap); 41 | (*tron_print_string)(buf); 42 | } 43 | 44 | TRON::TRON(const function *fun_obj, double eps, int max_iter) 45 | { 46 | this->fun_obj=const_cast(fun_obj); 47 | this->eps=eps; 48 | this->max_iter=max_iter; 49 | tron_print_string = default_print; 50 | } 51 | 52 | TRON::~TRON() 53 | { 54 | } 55 | 56 | void TRON::tron(double *w) 57 | { 58 | // Parameters for updating the iterates. 59 | double eta0 = 1e-4, eta1 = 0.25, eta2 = 0.75; 60 | 61 | // Parameters for updating the trust region size delta. 62 | double sigma1 = 0.25, sigma2 = 0.5, sigma3 = 4; 63 | 64 | int n = fun_obj->get_nr_variable(); 65 | int i, cg_iter; 66 | double delta, snorm, one=1.0; 67 | double alpha, f, fnew, prered, actred, gs; 68 | int search = 1, iter = 1, inc = 1; 69 | double *s = new double[n]; 70 | double *r = new double[n]; 71 | double *w_new = new double[n]; 72 | double *g = new double[n]; 73 | 74 | for (i=0; ifun(w); 78 | fun_obj->grad(w, g); 79 | delta = dnrm2_(&n, g, &inc); 80 | double gnorm1 = delta; 81 | double gnorm = gnorm1; 82 | 83 | if (gnorm <= eps*gnorm1) 84 | search = 0; 85 | 86 | iter = 1; 87 | 88 | while (iter <= max_iter && search) 89 | { 90 | cg_iter = trcg(delta, g, s, r); 91 | 92 | memcpy(w_new, w, sizeof(double)*n); 93 | daxpy_(&n, &one, s, &inc, w_new, &inc); 94 | 95 | gs = ddot_(&n, g, &inc, s, &inc); 96 | prered = -0.5*(gs-ddot_(&n, s, &inc, r, &inc)); 97 | fnew = fun_obj->fun(w_new); 98 | 99 | // Compute the actual reduction. 100 | actred = f - fnew; 101 | 102 | // On the first iteration, adjust the initial step bound. 103 | snorm = dnrm2_(&n, s, &inc); 104 | if (iter == 1) 105 | delta = min(delta, snorm); 106 | 107 | // Compute prediction alpha*snorm of the step. 108 | if (fnew - f - gs <= 0) 109 | alpha = sigma3; 110 | else 111 | alpha = max(sigma1, -0.5*(gs/(fnew - f - gs))); 112 | 113 | // Update the trust region bound according to the ratio of actual to predicted reduction. 114 | if (actred < eta0*prered) 115 | delta = min(max(alpha, sigma1)*snorm, sigma2*delta); 116 | else if (actred < eta1*prered) 117 | delta = max(sigma1*delta, min(alpha*snorm, sigma2*delta)); 118 | else if (actred < eta2*prered) 119 | delta = max(sigma1*delta, min(alpha*snorm, sigma3*delta)); 120 | else 121 | delta = max(delta, min(alpha*snorm, sigma3*delta)); 122 | 123 | info("iter %2d act %5.3e pre %5.3e delta %5.3e f %5.3e |g| %5.3e CG %3d\n", iter, actred, prered, delta, f, gnorm, cg_iter); 124 | 125 | if (actred > eta0*prered) 126 | { 127 | iter++; 128 | memcpy(w, w_new, sizeof(double)*n); 129 | f = fnew; 130 | fun_obj->grad(w, g); 131 | 132 | gnorm = dnrm2_(&n, g, &inc); 133 | if (gnorm <= eps*gnorm1) 134 | break; 135 | } 136 | if (f < -1.0e+32) 137 | { 138 | info("warning: f < -1.0e+32\n"); 139 | break; 140 | } 141 | if (fabs(actred) <= 0 && prered <= 0) 142 | { 143 | info("warning: actred and prered <= 0\n"); 144 | break; 145 | } 146 | if (fabs(actred) <= 1.0e-12*fabs(f) && 147 | fabs(prered) <= 1.0e-12*fabs(f)) 148 | { 149 | info("warning: actred and prered too small\n"); 150 | break; 151 | } 152 | } 153 | 154 | delete[] g; 155 | delete[] r; 156 | delete[] w_new; 157 | delete[] s; 158 | } 159 | 160 | int TRON::trcg(double delta, double *g, double *s, double *r) 161 | { 162 | int i, inc = 1; 163 | int n = fun_obj->get_nr_variable(); 164 | double one = 1; 165 | double *d = new double[n]; 166 | double *Hd = new double[n]; 167 | double rTr, rnewTrnew, alpha, beta, cgtol; 168 | 169 | for (i=0; iHv(d, Hd); 185 | 186 | alpha = rTr/ddot_(&n, d, &inc, Hd, &inc); 187 | daxpy_(&n, &alpha, d, &inc, s, &inc); 188 | if (dnrm2_(&n, s, &inc) > delta) 189 | { 190 | info("cg reaches trust region boundary\n"); 191 | alpha = -alpha; 192 | daxpy_(&n, &alpha, d, &inc, s, &inc); 193 | 194 | double std = ddot_(&n, s, &inc, d, &inc); 195 | double sts = ddot_(&n, s, &inc, s, &inc); 196 | double dtd = ddot_(&n, d, &inc, d, &inc); 197 | double dsq = delta*delta; 198 | double rad = sqrt(std*std + dtd*(dsq-sts)); 199 | if (std >= 0) 200 | alpha = (dsq - sts)/(std + rad); 201 | else 202 | alpha = (rad - std)/dtd; 203 | daxpy_(&n, &alpha, d, &inc, s, &inc); 204 | alpha = -alpha; 205 | daxpy_(&n, &alpha, Hd, &inc, r, &inc); 206 | break; 207 | } 208 | alpha = -alpha; 209 | daxpy_(&n, &alpha, Hd, &inc, r, &inc); 210 | rnewTrnew = ddot_(&n, r, &inc, r, &inc); 211 | beta = rnewTrnew/rTr; 212 | dscal_(&n, &beta, d, &inc); 213 | daxpy_(&n, &one, r, &inc, d, &inc); 214 | rTr = rnewTrnew; 215 | } 216 | 217 | delete[] d; 218 | delete[] Hd; 219 | 220 | return(cg_iter); 221 | } 222 | 223 | double TRON::norm_inf(int n, double *x) 224 | { 225 | double dmax = fabs(x[0]); 226 | for (int i=1; i= dmax) 228 | dmax = fabs(x[i]); 229 | return(dmax); 230 | } 231 | 232 | void TRON::set_print_string(void (*print_string) (const char *buf)) 233 | { 234 | tron_print_string = print_string; 235 | } 236 | -------------------------------------------------------------------------------- /liblinear-1.8/tron.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "tron.h" 6 | 7 | #ifndef min 8 | template static inline T min(T x,T y) { return (x static inline T max(T x,T y) { return (x>y)?x:y; } 13 | #endif 14 | 15 | #ifdef __cplusplus 16 | extern "C" { 17 | #endif 18 | 19 | extern double dnrm2_(int *, double *, int *); 20 | extern double ddot_(int *, double *, int *, double *, int *); 21 | extern int daxpy_(int *, double *, double *, int *, double *, int *); 22 | extern int dscal_(int *, double *, double *, int *); 23 | 24 | #ifdef __cplusplus 25 | } 26 | #endif 27 | 28 | static void default_print(const char *buf) 29 | { 30 | fputs(buf,stdout); 31 | fflush(stdout); 32 | } 33 | 34 | void TRON::info(const char *fmt,...) 35 | { 36 | char buf[BUFSIZ]; 37 | va_list ap; 38 | va_start(ap,fmt); 39 | vsprintf(buf,fmt,ap); 40 | va_end(ap); 41 | (*tron_print_string)(buf); 42 | } 43 | 44 | TRON::TRON(const function *fun_obj, double eps, int max_iter) 45 | { 46 | this->fun_obj=const_cast(fun_obj); 47 | this->eps=eps; 48 | this->max_iter=max_iter; 49 | tron_print_string = default_print; 50 | } 51 | 52 | TRON::~TRON() 53 | { 54 | } 55 | 56 | void TRON::tron(double *w) 57 | { 58 | // Parameters for updating the iterates. 59 | double eta0 = 1e-4, eta1 = 0.25, eta2 = 0.75; 60 | 61 | // Parameters for updating the trust region size delta. 62 | double sigma1 = 0.25, sigma2 = 0.5, sigma3 = 4; 63 | 64 | int n = fun_obj->get_nr_variable(); 65 | int i, cg_iter; 66 | double delta, snorm, one=1.0; 67 | double alpha, f, fnew, prered, actred, gs; 68 | int search = 1, iter = 1, inc = 1; 69 | double *s = new double[n]; 70 | double *r = new double[n]; 71 | double *w_new = new double[n]; 72 | double *g = new double[n]; 73 | 74 | for (i=0; ifun(w); 78 | fun_obj->grad(w, g); 79 | delta = dnrm2_(&n, g, &inc); 80 | double gnorm1 = delta; 81 | double gnorm = gnorm1; 82 | 83 | if (gnorm <= eps*gnorm1) 84 | search = 0; 85 | 86 | iter = 1; 87 | 88 | while (iter <= max_iter && search) 89 | { 90 | cg_iter = trcg(delta, g, s, r); 91 | 92 | memcpy(w_new, w, sizeof(double)*n); 93 | daxpy_(&n, &one, s, &inc, w_new, &inc); 94 | 95 | gs = ddot_(&n, g, &inc, s, &inc); 96 | prered = -0.5*(gs-ddot_(&n, s, &inc, r, &inc)); 97 | fnew = fun_obj->fun(w_new); 98 | 99 | // Compute the actual reduction. 100 | actred = f - fnew; 101 | 102 | // On the first iteration, adjust the initial step bound. 103 | snorm = dnrm2_(&n, s, &inc); 104 | if (iter == 1) 105 | delta = min(delta, snorm); 106 | 107 | // Compute prediction alpha*snorm of the step. 108 | if (fnew - f - gs <= 0) 109 | alpha = sigma3; 110 | else 111 | alpha = max(sigma1, -0.5*(gs/(fnew - f - gs))); 112 | 113 | // Update the trust region bound according to the ratio of actual to predicted reduction. 114 | if (actred < eta0*prered) 115 | delta = min(max(alpha, sigma1)*snorm, sigma2*delta); 116 | else if (actred < eta1*prered) 117 | delta = max(sigma1*delta, min(alpha*snorm, sigma2*delta)); 118 | else if (actred < eta2*prered) 119 | delta = max(sigma1*delta, min(alpha*snorm, sigma3*delta)); 120 | else 121 | delta = max(delta, min(alpha*snorm, sigma3*delta)); 122 | 123 | info("iter %2d act %5.3e pre %5.3e delta %5.3e f %5.3e |g| %5.3e CG %3d\n", iter, actred, prered, delta, f, gnorm, cg_iter); 124 | 125 | if (actred > eta0*prered) 126 | { 127 | iter++; 128 | memcpy(w, w_new, sizeof(double)*n); 129 | f = fnew; 130 | fun_obj->grad(w, g); 131 | 132 | gnorm = dnrm2_(&n, g, &inc); 133 | if (gnorm <= eps*gnorm1) 134 | break; 135 | } 136 | if (f < -1.0e+32) 137 | { 138 | info("warning: f < -1.0e+32\n"); 139 | break; 140 | } 141 | if (fabs(actred) <= 0 && prered <= 0) 142 | { 143 | info("warning: actred and prered <= 0\n"); 144 | break; 145 | } 146 | if (fabs(actred) <= 1.0e-12*fabs(f) && 147 | fabs(prered) <= 1.0e-12*fabs(f)) 148 | { 149 | info("warning: actred and prered too small\n"); 150 | break; 151 | } 152 | } 153 | 154 | delete[] g; 155 | delete[] r; 156 | delete[] w_new; 157 | delete[] s; 158 | } 159 | 160 | int TRON::trcg(double delta, double *g, double *s, double *r) 161 | { 162 | int i, inc = 1; 163 | int n = fun_obj->get_nr_variable(); 164 | double one = 1; 165 | double *d = new double[n]; 166 | double *Hd = new double[n]; 167 | double rTr, rnewTrnew, alpha, beta, cgtol; 168 | 169 | for (i=0; iHv(d, Hd); 185 | 186 | alpha = rTr/ddot_(&n, d, &inc, Hd, &inc); 187 | daxpy_(&n, &alpha, d, &inc, s, &inc); 188 | if (dnrm2_(&n, s, &inc) > delta) 189 | { 190 | info("cg reaches trust region boundary\n"); 191 | alpha = -alpha; 192 | daxpy_(&n, &alpha, d, &inc, s, &inc); 193 | 194 | double std = ddot_(&n, s, &inc, d, &inc); 195 | double sts = ddot_(&n, s, &inc, s, &inc); 196 | double dtd = ddot_(&n, d, &inc, d, &inc); 197 | double dsq = delta*delta; 198 | double rad = sqrt(std*std + dtd*(dsq-sts)); 199 | if (std >= 0) 200 | alpha = (dsq - sts)/(std + rad); 201 | else 202 | alpha = (rad - std)/dtd; 203 | daxpy_(&n, &alpha, d, &inc, s, &inc); 204 | alpha = -alpha; 205 | daxpy_(&n, &alpha, Hd, &inc, r, &inc); 206 | break; 207 | } 208 | alpha = -alpha; 209 | daxpy_(&n, &alpha, Hd, &inc, r, &inc); 210 | rnewTrnew = ddot_(&n, r, &inc, r, &inc); 211 | beta = rnewTrnew/rTr; 212 | dscal_(&n, &beta, d, &inc); 213 | daxpy_(&n, &one, r, &inc, d, &inc); 214 | rTr = rnewTrnew; 215 | } 216 | 217 | delete[] d; 218 | delete[] Hd; 219 | 220 | return(cg_iter); 221 | } 222 | 223 | double TRON::norm_inf(int n, double *x) 224 | { 225 | double dmax = fabs(x[0]); 226 | for (int i=1; i= dmax) 228 | dmax = fabs(x[i]); 229 | return(dmax); 230 | } 231 | 232 | void TRON::set_print_string(void (*print_string) (const char *buf)) 233 | { 234 | tron_print_string = print_string; 235 | } 236 | -------------------------------------------------------------------------------- /liblinear-1.8/python/liblinearutil.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from liblinear 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 += [int(label)] 24 | prob_x += [xi] 25 | return (prob_y, prob_x) 26 | 27 | def load_model(model_file_name): 28 | """ 29 | load_model(model_file_name) -> model 30 | 31 | Load a LIBLINEAR model from model_file_name and return. 32 | """ 33 | model = liblinear.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 save_model(model_file_name, model): 41 | """ 42 | save_model(model_file_name, model) -> None 43 | 44 | Save a LIBLINEAR model to the file model_file_name. 45 | """ 46 | liblinear.save_model(model_file_name, model) 47 | 48 | def evaluations(ty, pv): 49 | """ 50 | evaluations(ty, pv) -> ACC 51 | 52 | Calculate accuracy using the true values (ty) and predicted values (pv). 53 | """ 54 | if len(ty) != len(pv): 55 | raise ValueError("len(ty) must equal to len(pv)") 56 | total_correct = total_error = 0 57 | for v, y in zip(pv, ty): 58 | if y == v: 59 | total_correct += 1 60 | l = len(ty) 61 | ACC = 100.0*total_correct/l 62 | return ACC 63 | 64 | def train(arg1, arg2=None, arg3=None): 65 | """ 66 | train(y, x [, 'options']) -> model | ACC 67 | train(prob, [, 'options']) -> model | ACC 68 | train(prob, param) -> model | ACC 69 | 70 | Train a model from data (y, x) or a problem prob using 71 | 'options' or a parameter param. 72 | If '-v' is specified in 'options' (i.e., cross validation) 73 | accuracy (ACC) is returned. 74 | 75 | 'options': 76 | -s type : set type of solver (default 1) 77 | 0 -- L2-regularized logistic regression (primal) 78 | 1 -- L2-regularized L2-loss support vector classification (dual) 79 | 2 -- L2-regularized L2-loss support vector classification (primal) 80 | 3 -- L2-regularized L1-loss support vector classification (dual) 81 | 4 -- multi-class support vector classification by Crammer and Singer 82 | 5 -- L1-regularized L2-loss support vector classification 83 | 6 -- L1-regularized logistic regression 84 | 7 -- L2-regularized logistic regression (dual) 85 | -c cost : set the parameter C (default 1) 86 | -e epsilon : set tolerance of termination criterion 87 | -s 0 and 2 88 | |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2, 89 | where f is the primal function, (default 0.01) 90 | -s 1, 3, 4, and 7 91 | Dual maximal violation <= eps; similar to liblinear (default 0.1) 92 | -s 5 and 6 93 | |f'(w)|_inf <= eps*min(pos,neg)/l*|f'(w0)|_inf, 94 | where f is the primal function (default 0.01) 95 | -B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1) 96 | -wi weight: weights adjust the parameter C of different classes (see README for details) 97 | -v n: n-fold cross validation mode 98 | -q : quiet mode (no outputs) 99 | """ 100 | prob, param = None, None 101 | if isinstance(arg1, (list, tuple)): 102 | assert isinstance(arg2, (list, tuple)) 103 | y, x, options = arg1, arg2, arg3 104 | prob = problem(y, x) 105 | param = parameter(options) 106 | elif isinstance(arg1, problem): 107 | prob = arg1 108 | if isinstance(arg2, parameter): 109 | param = arg2 110 | else : 111 | param = parameter(arg2) 112 | if prob == None or param == None : 113 | raise TypeError("Wrong types for the arguments") 114 | 115 | prob.set_bias(param.bias) 116 | liblinear.set_print_string_function(param.print_func) 117 | err_msg = liblinear.check_parameter(prob, param) 118 | if err_msg : 119 | raise ValueError('Error: %s' % err_msg) 120 | 121 | if param.cross_validation: 122 | l, nr_fold = prob.l, param.nr_fold 123 | target = (c_int * l)() 124 | liblinear.cross_validation(prob, param, nr_fold, target) 125 | ACC = evaluations(prob.y[:l], target[:l]) 126 | print("Cross Validation Accuracy = %g%%" % ACC) 127 | return ACC 128 | else : 129 | m = liblinear.train(prob, param) 130 | m = toPyModel(m) 131 | 132 | # If prob is destroyed, data including SVs pointed by m can remain. 133 | m.x_space = prob.x_space 134 | return m 135 | 136 | def predict(y, x, m, options=""): 137 | """ 138 | predict(y, x, m [, "options"]) -> (p_labels, p_acc, p_vals) 139 | 140 | Predict data (y, x) with the SVM model m. 141 | "options": 142 | -b probability_estimates: whether to predict probability estimates, 143 | 0 or 1 (default 0); 144 | 145 | The return tuple contains 146 | p_labels: a list of predicted labels 147 | p_acc: testing accuracy. 148 | p_vals: a list of decision values or probability estimates (if '-b 1' 149 | is specified). If k is the number of classes, for decision values, 150 | each element includes results of predicting k binary-class 151 | SVMs. if k = 2 and solver is not MCSVM_CS, only one decision value 152 | is returned. For probabilities, each element contains k values 153 | indicating the probability that the testing instance is in each class. 154 | Note that the order of classes here is the same as 'model.label' 155 | field in the model structure. 156 | """ 157 | predict_probability = 0 158 | argv = options.split() 159 | i = 0 160 | while i < len(argv): 161 | if argv[i] == '-b': 162 | i += 1 163 | predict_probability = int(argv[i]) 164 | else: 165 | raise ValueError("Wrong options") 166 | i+=1 167 | 168 | nr_class = m.get_nr_class() 169 | nr_feature = m.get_nr_feature() 170 | is_prob_model = m.is_probability_model() 171 | bias = m.bias 172 | if bias >= 0: 173 | biasterm = feature_node(nr_feature+1, bias) 174 | else: 175 | biasterm = feature_node(-1, bias) 176 | pred_labels = [] 177 | pred_values = [] 178 | 179 | if predict_probability: 180 | if not is_prob_model: 181 | raise TypeError('probability output is only supported for logistic regression') 182 | prob_estimates = (c_double * nr_class)() 183 | for xi in x: 184 | xi, idx = gen_feature_nodearray(xi, feature_max=nr_feature) 185 | xi[-2] = biasterm 186 | label = liblinear.predict_probability(m, xi, prob_estimates) 187 | values = prob_estimates[:nr_class] 188 | pred_labels += [label] 189 | pred_values += [values] 190 | else: 191 | if nr_class <= 2: 192 | nr_classifier = 1 193 | else: 194 | nr_classifier = nr_class 195 | dec_values = (c_double * nr_classifier)() 196 | for xi in x: 197 | xi, idx = gen_feature_nodearray(xi, feature_max=nr_feature) 198 | xi[-2] = biasterm 199 | label = liblinear.predict_values(m, xi, dec_values) 200 | values = dec_values[:nr_classifier] 201 | pred_labels += [label] 202 | pred_values += [values] 203 | if len(y) == 0: 204 | y = [0] * len(x) 205 | ACC = evaluations(y, pred_labels) 206 | l = len(y) 207 | print("Accuracy = %g%% (%d/%d)" % (ACC, int(l*ACC//100), l)) 208 | 209 | return pred_labels, ACC, pred_values 210 | 211 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/README: -------------------------------------------------------------------------------- 1 | -------------------------------------------- 2 | --- MATLAB/OCTAVE interface of LIBLINEAR --- 3 | -------------------------------------------- 4 | 5 | Table of Contents 6 | ================= 7 | 8 | - Introduction 9 | - Installation 10 | - Usage 11 | - Returned Model Structure 12 | - Other Utilities 13 | - Examples 14 | - Additional Information 15 | 16 | 17 | Introduction 18 | ============ 19 | 20 | This tool provides a simple interface to LIBLINEAR, a library for 21 | large-scale regularized linear classification 22 | (http://www.csie.ntu.edu.tw/~cjlin/liblinear). It is very easy to use 23 | as the usage and the way of specifying parameters are the same as that 24 | of LIBLINEAR. 25 | 26 | Installation 27 | ============ 28 | 29 | On Unix systems, we recommend using GNU g++ as your compiler and type 30 | 'make' to build 'train.mexglx' and 'predict.mexglx'. Note that we 31 | assume your MATLAB is installed in '/usr/local/matlab', if not, please 32 | change MATLABDIR in Makefile. 33 | 34 | Example: 35 | linux> make 36 | 37 | To use Octave, type 'make octave': 38 | 39 | Example: 40 | linux> make octave 41 | 42 | On Windows systems, pre-built 'train.mexw64' and 'predict.mexw64' are 43 | included in this package (in ..\windows), so no need to conduct 44 | installation unless you run 32 bit windows. If you have modified the 45 | sources and would like to re-build the package, type 'mex -setup' in 46 | MATLAB to choose a compiler for mex first. Then type 'make' to start 47 | the installation. 48 | 49 | Example: 50 | matlab> mex -setup 51 | (ps: MATLAB will show the following messages to setup default compiler.) 52 | Please choose your compiler for building external interface (MEX) files: 53 | Would you like mex to locate installed compilers [y]/n? y 54 | Select a compiler: 55 | [1] Microsoft Visual C/C++ 2005 in C:\Program Files\Microsoft Visual Studio 8 56 | [0] None 57 | Compiler: 1 58 | Please verify your choices: 59 | Compiler: Microsoft Visual C/C++ 2005 60 | Location: C:\Program Files\Microsoft Visual Studio 8 61 | Are these correct?([y]/n): y 62 | 63 | matlab> make 64 | 65 | For list of supported/compatible compilers for MATLAB, please check the 66 | following page: 67 | 68 | http://www.mathworks.com/support/compilers/current_release/ 69 | 70 | Usage 71 | ===== 72 | 73 | matlab> model = train(training_label_vector, training_instance_matrix [,'liblinear_options', 'col']); 74 | 75 | -training_label_vector: 76 | An m by 1 vector of training labels. (type must be double) 77 | -training_instance_matrix: 78 | An m by n matrix of m training instances with n features. 79 | It must be a sparse matrix. (type must be double) 80 | -liblinear_options: 81 | A string of training options in the same format as that of LIBLINEAR. 82 | -col: 83 | if 'col' is set, each column of training_instance_matrix is a data instance. Otherwise each row is a data instance. 84 | 85 | matlab> [predicted_label, accuracy, decision_values/prob_estimates] = predict(testing_label_vector, testing_instance_matrix, model [, 'liblinear_options', 'col']); 86 | 87 | -testing_label_vector: 88 | An m by 1 vector of prediction labels. If labels of test 89 | data are unknown, simply use any random values. (type must be double) 90 | -testing_instance_matrix: 91 | An m by n matrix of m testing instances with n features. 92 | It must be a sparse matrix. (type must be double) 93 | -model: 94 | The output of train. 95 | -liblinear_options: 96 | A string of testing options in the same format as that of LIBLINEAR. 97 | -col: 98 | if 'col' is set, each column of testing_instance_matrix is a data instance. Otherwise each row is a data instance. 99 | 100 | Returned Model Structure 101 | ======================== 102 | 103 | The 'train' function returns a model which can be used for future 104 | prediction. It is a structure and is organized as [Parameters, nr_class, 105 | nr_feature, bias, Label, w]: 106 | 107 | -Parameters: Parameters 108 | -nr_class: number of classes 109 | -nr_feature: number of features in training data (without including the bias term) 110 | -bias: If >= 0, we assume one additional feature is added to the end 111 | of each data instance. 112 | -Label: label of each class 113 | -w: a nr_w-by-n matrix for the weights, where n is nr_feature 114 | or nr_feature+1 depending on the existence of the bias term. 115 | nr_w is 1 if nr_class=2 and -s is not 4 (i.e., not 116 | multi-class svm by Crammer and Singer). It is 117 | nr_class otherwise. 118 | 119 | If the '-v' option is specified, cross validation is conducted and the 120 | returned model is just a scalar: cross-validation accuracy. 121 | 122 | Result of Prediction 123 | ==================== 124 | 125 | The function 'predict' has three outputs. The first one, 126 | predicted_label, is a vector of predicted labels. 127 | The second output is a scalar meaning accuracy. 128 | The third is a matrix containing decision values or probability 129 | estimates (if '-b 1' is specified). If k is the number of classes 130 | and k' is the number of classifiers (k'=1 if k=2, otherwise k'=k), for decision values, 131 | each row includes results of k' binary linear classifiers. For probabilities, 132 | each row contains k values indicating the probability that the testing instance is in 133 | each class. Note that the order of classes here is the same as 'Label' 134 | field in the model structure. 135 | 136 | Other Utilities 137 | =============== 138 | 139 | A matlab function libsvmread reads files in LIBSVM format: 140 | 141 | [label_vector, instance_matrix] = libsvmread('data.txt'); 142 | 143 | Two outputs are labels and instances, which can then be used as inputs 144 | of svmtrain or svmpredict. 145 | 146 | A matlab function libsvmwrite writes Matlab matrix to a file in LIBSVM format: 147 | 148 | libsvmwrite('data.txt', label_vector, instance_matrix] 149 | 150 | The instance_matrix must be a sparse matrix. (type must be double) 151 | For windows, `libsvmread.mexw64' and `libsvmwrite.mexw64' are ready in 152 | the directory `..\windows'. 153 | 154 | These codes are prepared by Rong-En Fan and Kai-Wei Chang from National 155 | Taiwan University. 156 | 157 | Examples 158 | ======== 159 | 160 | Train and test on the provided data heart_scale: 161 | 162 | matlab> [heart_scale_label, heart_scale_inst] = libsvmread('../heart_scale'); 163 | matlab> model = train(heart_scale_label, heart_scale_inst, '-c 1'); 164 | matlab> [predict_label, accuracy, dec_values] = predict(heart_scale_label, heart_scale_inst, model); % test the training data 165 | 166 | Note that for testing, you can put anything in the testing_label_vector. 167 | 168 | For probability estimates, you need '-b 1' for training and testing: 169 | 170 | matlab> [predict_label, accuracy, prob_estimates] = predict(heart_scale_label, heart_scale_inst, model, '-b 1'); 171 | 172 | Additional Information 173 | ====================== 174 | 175 | Please cite LIBLINEAR as follows 176 | 177 | R.-E. Fan, K.-W. Chang, C.-J. Hsieh, X.-R. Wang, and C.-J. Lin. 178 | LIBLINEAR: A Library for Large Linear Classification, Journal of 179 | Machine Learning Research 9(2008), 1871-1874.Software available at 180 | http://www.csie.ntu.edu.tw/~cjlin/liblinear 181 | 182 | For any question, please contact Chih-Jen Lin . 183 | 184 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/predict.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "linear.h" 5 | 6 | #include "mex.h" 7 | #include "linear_model_matlab.h" 8 | 9 | #if MX_API_VER < 0x07030000 10 | typedef int mwIndex; 11 | #endif 12 | 13 | #define CMD_LEN 2048 14 | 15 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 16 | 17 | int col_format_flag; 18 | 19 | void read_sparse_instance(const mxArray *prhs, int index, struct feature_node *x, int feature_number, double bias) 20 | { 21 | int i, j, low, high; 22 | mwIndex *ir, *jc; 23 | double *samples; 24 | 25 | ir = mxGetIr(prhs); 26 | jc = mxGetJc(prhs); 27 | samples = mxGetPr(prhs); 28 | 29 | // each column is one instance 30 | j = 0; 31 | low = (int) jc[index], high = (int) jc[index+1]; 32 | for(i=low; i=0) 39 | { 40 | x[j].index = feature_number+1; 41 | x[j].value = bias; 42 | j++; 43 | } 44 | x[j].index = -1; 45 | } 46 | 47 | static void fake_answer(mxArray *plhs[]) 48 | { 49 | plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); 50 | plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL); 51 | plhs[2] = mxCreateDoubleMatrix(0, 0, mxREAL); 52 | } 53 | 54 | void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag) 55 | { 56 | int label_vector_row_num, label_vector_col_num; 57 | int feature_number, testing_instance_number; 58 | int instance_index; 59 | double *ptr_instance, *ptr_label, *ptr_predict_label; 60 | double *ptr_prob_estimates, *ptr_dec_values, *ptr; 61 | struct feature_node *x; 62 | mxArray *pplhs[1]; // instance sparse matrix in row format 63 | 64 | int correct = 0; 65 | int total = 0; 66 | 67 | int nr_class=get_nr_class(model_); 68 | int nr_w; 69 | double *prob_estimates=NULL; 70 | 71 | if(nr_class==2 && model_->param.solver_type!=MCSVM_CS) 72 | nr_w=1; 73 | else 74 | nr_w=nr_class; 75 | 76 | // prhs[1] = testing instance matrix 77 | feature_number = get_nr_feature(model_); 78 | testing_instance_number = (int) mxGetM(prhs[1]); 79 | if(col_format_flag) 80 | { 81 | feature_number = (int) mxGetM(prhs[1]); 82 | testing_instance_number = (int) mxGetN(prhs[1]); 83 | } 84 | 85 | label_vector_row_num = (int) mxGetM(prhs[0]); 86 | label_vector_col_num = (int) mxGetN(prhs[0]); 87 | 88 | if(label_vector_row_num!=testing_instance_number) 89 | { 90 | mexPrintf("Length of label vector does not match # of instances.\n"); 91 | fake_answer(plhs); 92 | return; 93 | } 94 | if(label_vector_col_num!=1) 95 | { 96 | mexPrintf("label (1st argument) should be a vector (# of column is 1).\n"); 97 | fake_answer(plhs); 98 | return; 99 | } 100 | 101 | ptr_instance = mxGetPr(prhs[1]); 102 | ptr_label = mxGetPr(prhs[0]); 103 | 104 | // transpose instance matrix 105 | if(mxIsSparse(prhs[1])) 106 | { 107 | if(col_format_flag) 108 | { 109 | pplhs[0] = (mxArray *)prhs[1]; 110 | } 111 | else 112 | { 113 | mxArray *pprhs[1]; 114 | pprhs[0] = mxDuplicateArray(prhs[1]); 115 | if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) 116 | { 117 | mexPrintf("Error: cannot transpose testing instance matrix\n"); 118 | fake_answer(plhs); 119 | return; 120 | } 121 | } 122 | } 123 | else 124 | mexPrintf("Testing_instance_matrix must be sparse\n"); 125 | 126 | 127 | prob_estimates = Malloc(double, nr_class); 128 | 129 | plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); 130 | if(predict_probability_flag) 131 | plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); 132 | else 133 | plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_w, mxREAL); 134 | 135 | ptr_predict_label = mxGetPr(plhs[0]); 136 | ptr_prob_estimates = mxGetPr(plhs[2]); 137 | ptr_dec_values = mxGetPr(plhs[2]); 138 | x = Malloc(struct feature_node, feature_number+2); 139 | for(instance_index=0;instance_indexbias); 148 | 149 | if(predict_probability_flag) 150 | { 151 | v = predict_probability(model_, x, prob_estimates); 152 | ptr_predict_label[instance_index] = v; 153 | for(i=0;i 5 || nrhs < 3) 204 | { 205 | exit_with_help(); 206 | fake_answer(plhs); 207 | return; 208 | } 209 | if(nrhs == 5) 210 | { 211 | mxGetString(prhs[4], cmd, mxGetN(prhs[4])+1); 212 | if(strcmp(cmd, "col") == 0) 213 | { 214 | col_format_flag = 1; 215 | } 216 | } 217 | 218 | if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { 219 | mexPrintf("Error: label vector and instance matrix must be double\n"); 220 | fake_answer(plhs); 221 | return; 222 | } 223 | 224 | if(mxIsStruct(prhs[2])) 225 | { 226 | const char *error_msg; 227 | 228 | // parse options 229 | if(nrhs>=4) 230 | { 231 | int i, argc = 1; 232 | char *argv[CMD_LEN/2]; 233 | 234 | // put options in argv[] 235 | mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1); 236 | if((argv[argc] = strtok(cmd, " ")) != NULL) 237 | while((argv[++argc] = strtok(NULL, " ")) != NULL) 238 | ; 239 | 240 | for(i=1;i=argc) 244 | { 245 | exit_with_help(); 246 | fake_answer(plhs); 247 | return; 248 | } 249 | switch(argv[i-1][1]) 250 | { 251 | case 'b': 252 | prob_estimate_flag = atoi(argv[i]); 253 | break; 254 | default: 255 | mexPrintf("unknown option\n"); 256 | exit_with_help(); 257 | fake_answer(plhs); 258 | return; 259 | } 260 | } 261 | } 262 | 263 | model_ = Malloc(struct model, 1); 264 | error_msg = matlab_matrix_to_model(model_, prhs[2]); 265 | if(error_msg) 266 | { 267 | mexPrintf("Error: can't read model: %s\n", error_msg); 268 | free_and_destroy_model(&model_); 269 | fake_answer(plhs); 270 | return; 271 | } 272 | 273 | if(prob_estimate_flag) 274 | { 275 | if(!check_probability_model(model_)) 276 | { 277 | mexPrintf("probability output is only supported for logistic regression\n"); 278 | prob_estimate_flag=0; 279 | } 280 | } 281 | 282 | if(mxIsSparse(prhs[1])) 283 | do_predict(plhs, prhs, model_, prob_estimate_flag); 284 | else 285 | { 286 | mexPrintf("Testing_instance_matrix must be sparse\n"); 287 | fake_answer(plhs); 288 | } 289 | 290 | // destroy model_ 291 | free_and_destroy_model(&model_); 292 | } 293 | else 294 | { 295 | mexPrintf("model file should be a struct array\n"); 296 | fake_answer(plhs); 297 | } 298 | 299 | return; 300 | } 301 | -------------------------------------------------------------------------------- /liblinear-1.8/python/liblinear.py: -------------------------------------------------------------------------------- 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('linear'): 10 | liblinear = CDLL(find_library('linear')) 11 | elif find_library('liblinear'): 12 | liblinear = CDLL(find_library('liblinear')) 13 | else: 14 | if sys.platform == 'win32': 15 | liblinear = CDLL(os.path.join(os.path.dirname(__file__),\ 16 | '../windows/liblinear.dll')) 17 | else: 18 | liblinear = CDLL(os.path.join(os.path.dirname(__file__),\ 19 | '../liblinear.so.1')) 20 | 21 | # Construct constants 22 | SOLVER_TYPE = ['L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL',\ 23 | 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR', 'L2R_LR_DUAL'] 24 | for i, s in enumerate(SOLVER_TYPE): exec("%s = %d" % (s , i)) 25 | 26 | PRINT_STRING_FUN = CFUNCTYPE(None, c_char_p) 27 | def print_null(s): 28 | return 29 | 30 | def genFields(names, types): 31 | return list(zip(names, types)) 32 | 33 | def fillprototype(f, restype, argtypes): 34 | f.restype = restype 35 | f.argtypes = argtypes 36 | 37 | class feature_node(Structure): 38 | _names = ["index", "value"] 39 | _types = [c_int, c_double] 40 | _fields_ = genFields(_names, _types) 41 | 42 | def gen_feature_nodearray(xi, feature_max=None, issparse=True): 43 | if isinstance(xi, dict): 44 | index_range = xi.keys() 45 | elif isinstance(xi, (list, tuple)): 46 | xi = [0] + xi # idx should start from 1 47 | index_range = range(1, len(xi)) 48 | else: 49 | raise TypeError('xi should be a dictionary, list or tuple') 50 | 51 | if feature_max: 52 | assert(isinstance(feature_max, int)) 53 | index_range = filter(lambda j: j <= feature_max, index_range) 54 | if issparse: 55 | index_range = filter(lambda j:xi[j] != 0, index_range) 56 | 57 | index_range = sorted(index_range) 58 | ret = (feature_node * (len(index_range)+2))() 59 | ret[-1].index = -1 # for bias term 60 | ret[-2].index = -1 61 | for idx, j in enumerate(index_range): 62 | ret[idx].index = j 63 | ret[idx].value = xi[j] 64 | max_idx = 0 65 | if index_range : 66 | max_idx = index_range[-1] 67 | return ret, max_idx 68 | 69 | class problem(Structure): 70 | _names = ["l", "n", "y", "x", "bias"] 71 | _types = [c_int, c_int, POINTER(c_int), POINTER(POINTER(feature_node)), c_double] 72 | _fields_ = genFields(_names, _types) 73 | 74 | def __init__(self, y, x, bias = -1): 75 | if len(y) != len(x) : 76 | raise ValueError("len(y) != len(x)") 77 | self.l = l = len(y) 78 | self.bias = -1 79 | 80 | max_idx = 0 81 | x_space = self.x_space = [] 82 | for i, xi in enumerate(x): 83 | tmp_xi, tmp_idx = gen_feature_nodearray(xi) 84 | x_space += [tmp_xi] 85 | max_idx = max(max_idx, tmp_idx) 86 | self.n = max_idx 87 | 88 | self.y = (c_int * l)() 89 | for i, yi in enumerate(y): self.y[i] = y[i] 90 | 91 | self.x = (POINTER(feature_node) * l)() 92 | for i, xi in enumerate(self.x_space): self.x[i] = xi 93 | 94 | self.set_bias(bias) 95 | 96 | def set_bias(self, bias): 97 | if self.bias == bias: 98 | return 99 | if bias >= 0 and self.bias < 0: 100 | self.n += 1 101 | node = feature_node(self.n, bias) 102 | if bias < 0 and self.bias >= 0: 103 | self.n -= 1 104 | node = feature_node(-1, bias) 105 | 106 | for xi in self.x_space: 107 | xi[-2] = node 108 | self.bias = bias 109 | 110 | 111 | class parameter(Structure): 112 | _names = ["solver_type", "eps", "C", "nr_weight", "weight_label", "weight"] 113 | _types = [c_int, c_double, c_double, c_int, POINTER(c_int), POINTER(c_double)] 114 | _fields_ = genFields(_names, _types) 115 | 116 | def __init__(self, options = None): 117 | if options == None: 118 | options = '' 119 | self.parse_options(options) 120 | 121 | def show(self): 122 | attrs = parameter._names + self.__dict__.keys() 123 | values = map(lambda attr: getattr(self, attr), attrs) 124 | for attr, val in zip(attrs, values): 125 | print(' %s: %s' % (attr, val)) 126 | 127 | def set_to_default_values(self): 128 | self.solver_type = L2R_L2LOSS_SVC_DUAL 129 | self.eps = float('inf') 130 | self.C = 1 131 | self.nr_weight = 0 132 | self.weight_label = (c_int * 0)() 133 | self.weight = (c_double * 0)() 134 | self.bias = -1 135 | self.cross_validation = False 136 | self.nr_fold = 0 137 | self.print_func = None 138 | 139 | def parse_options(self, options): 140 | argv = options.split() 141 | self.set_to_default_values() 142 | self.print_func = cast(None, PRINT_STRING_FUN) 143 | weight_label = [] 144 | weight = [] 145 | 146 | i = 0 147 | while i < len(argv) : 148 | if argv[i] == "-s": 149 | i = i + 1 150 | self.solver_type = int(argv[i]) 151 | elif argv[i] == "-c": 152 | i = i + 1 153 | self.C = float(argv[i]) 154 | elif argv[i] == "-e": 155 | i = i + 1 156 | self.eps = float(argv[i]) 157 | elif argv[i] == "-B": 158 | i = i + 1 159 | self.bias = float(argv[i]) 160 | elif argv[i] == "-v": 161 | i = i + 1 162 | self.cross_validation = 1 163 | self.nr_fold = int(argv[i]) 164 | if self.nr_fold < 2 : 165 | raise ValueError("n-fold cross validation: n must >= 2") 166 | elif argv[i].startswith("-w"): 167 | i = i + 1 168 | self.nr_weight += 1 169 | nr_weight = self.nr_weight 170 | weight_label += [int(argv[i-1][2:])] 171 | weight += [float(argv[i])] 172 | elif argv[i] == "-q": 173 | self.print_func = PRINT_STRING_FUN(print_null) 174 | else : 175 | raise ValueError("Wrong options") 176 | i += 1 177 | 178 | liblinear.set_print_string_function(self.print_func) 179 | self.weight_label = (c_int*self.nr_weight)() 180 | self.weight = (c_double*self.nr_weight)() 181 | for i in range(self.nr_weight): 182 | self.weight[i] = weight[i] 183 | self.weight_label[i] = weight_label[i] 184 | 185 | if self.eps == float('inf'): 186 | if self.solver_type in [L2R_LR, L2R_L2LOSS_SVC]: 187 | self.eps = 0.01 188 | elif self.solver_type in [L2R_L2LOSS_SVC_DUAL, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L2R_LR_DUAL]: 189 | self.eps = 0.1 190 | elif self.solver_type in [L1R_L2LOSS_SVC, L1R_LR]: 191 | self.eps = 0.01 192 | 193 | 194 | class model(Structure): 195 | _names = ["param", "nr_class", "nr_feature", "w", "label", "bias"] 196 | _types = [parameter, c_int, c_int, POINTER(c_double), POINTER(c_int), c_double] 197 | _fields_ = genFields(_names, _types) 198 | 199 | def __init__(self): 200 | self.__createfrom__ = 'python' 201 | 202 | def __del__(self): 203 | # free memory created by C to avoid memory leak 204 | if hasattr(self, '__createfrom__') and self.__createfrom__ == 'C': 205 | liblinear.free_and_destroy_model(pointer(self)) 206 | 207 | def get_nr_feature(self): 208 | return liblinear.get_nr_feature(self) 209 | 210 | def get_nr_class(self): 211 | return liblinear.get_nr_class(self) 212 | 213 | def get_labels(self): 214 | nr_class = self.get_nr_class() 215 | labels = (c_int * nr_class)() 216 | liblinear.get_labels(self, labels) 217 | return labels[:nr_class] 218 | 219 | def is_probability_model(self): 220 | return (liblinear.check_probability_model(self) == 1) 221 | 222 | def toPyModel(model_ptr): 223 | """ 224 | toPyModel(model_ptr) -> model 225 | 226 | Convert a ctypes POINTER(model) to a Python model 227 | """ 228 | if bool(model_ptr) == False: 229 | raise ValueError("Null pointer") 230 | m = model_ptr.contents 231 | m.__createfrom__ = 'C' 232 | return m 233 | 234 | fillprototype(liblinear.train, POINTER(model), [POINTER(problem), POINTER(parameter)]) 235 | fillprototype(liblinear.cross_validation, None, [POINTER(problem), POINTER(parameter), c_int, POINTER(c_int)]) 236 | 237 | fillprototype(liblinear.predict_values, c_int, [POINTER(model), POINTER(feature_node), POINTER(c_double)]) 238 | fillprototype(liblinear.predict, c_int, [POINTER(model), POINTER(feature_node)]) 239 | fillprototype(liblinear.predict_probability, c_int, [POINTER(model), POINTER(feature_node), POINTER(c_double)]) 240 | 241 | fillprototype(liblinear.save_model, c_int, [c_char_p, POINTER(model)]) 242 | fillprototype(liblinear.load_model, POINTER(model), [c_char_p]) 243 | 244 | fillprototype(liblinear.get_nr_feature, c_int, [POINTER(model)]) 245 | fillprototype(liblinear.get_nr_class, c_int, [POINTER(model)]) 246 | fillprototype(liblinear.get_labels, None, [POINTER(model), POINTER(c_int)]) 247 | 248 | fillprototype(liblinear.free_model_content, None, [POINTER(model)]) 249 | fillprototype(liblinear.free_and_destroy_model, None, [POINTER(POINTER(model))]) 250 | fillprototype(liblinear.destroy_param, None, [POINTER(parameter)]) 251 | fillprototype(liblinear.check_parameter, c_char_p, [POINTER(problem), POINTER(parameter)]) 252 | fillprototype(liblinear.check_probability_model, c_int, [POINTER(model)]) 253 | fillprototype(liblinear.set_print_string_function, None, [CFUNCTYPE(None, c_char_p)]) 254 | 255 | 256 | -------------------------------------------------------------------------------- /liblinear-1.8/train.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "linear.h" 8 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 9 | #define INF HUGE_VAL 10 | 11 | void print_null(const char *s) {} 12 | 13 | void exit_with_help() 14 | { 15 | printf( 16 | "Usage: train [options] training_set_file [model_file]\n" 17 | "options:\n" 18 | "-s type : set type of solver (default 1)\n" 19 | " 0 -- L2-regularized logistic regression (primal)\n" 20 | " 1 -- L2-regularized L2-loss support vector classification (dual)\n" 21 | " 2 -- L2-regularized L2-loss support vector classification (primal)\n" 22 | " 3 -- L2-regularized L1-loss support vector classification (dual)\n" 23 | " 4 -- multi-class support vector classification by Crammer and Singer\n" 24 | " 5 -- L1-regularized L2-loss support vector classification\n" 25 | " 6 -- L1-regularized logistic regression\n" 26 | " 7 -- L2-regularized logistic regression (dual)\n" 27 | "-c cost : set the parameter C (default 1)\n" 28 | "-e epsilon : set tolerance of termination criterion\n" 29 | " -s 0 and 2\n" 30 | " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n" 31 | " where f is the primal function and pos/neg are # of\n" 32 | " positive/negative data (default 0.01)\n" 33 | " -s 1, 3, 4 and 7\n" 34 | " Dual maximal violation <= eps; similar to libsvm (default 0.1)\n" 35 | " -s 5 and 6\n" 36 | " |f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,\n" 37 | " where f is the primal function (default 0.01)\n" 38 | "-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1)\n" 39 | "-wi weight: weights adjust the parameter C of different classes (see README for details)\n" 40 | "-v n: n-fold cross validation mode\n" 41 | "-q : quiet mode (no outputs)\n" 42 | ); 43 | exit(1); 44 | } 45 | 46 | void exit_input_error(int line_num) 47 | { 48 | fprintf(stderr,"Wrong input format at line %d\n", line_num); 49 | exit(1); 50 | } 51 | 52 | static char *line = NULL; 53 | static int max_line_len; 54 | 55 | static char* readline(FILE *input) 56 | { 57 | int len; 58 | 59 | if(fgets(line,max_line_len,input) == NULL) 60 | return NULL; 61 | 62 | while(strrchr(line,'\n') == NULL) 63 | { 64 | max_line_len *= 2; 65 | line = (char *) realloc(line,max_line_len); 66 | len = (int) strlen(line); 67 | if(fgets(line+len,max_line_len-len,input) == NULL) 68 | break; 69 | } 70 | return line; 71 | } 72 | 73 | void parse_command_line(int argc, char **argv, char *input_file_name, char *model_file_name); 74 | void read_problem(const char *filename); 75 | void do_cross_validation(); 76 | 77 | struct feature_node *x_space; 78 | struct parameter param; 79 | struct problem prob; 80 | struct model* model_; 81 | int flag_cross_validation; 82 | int nr_fold; 83 | double bias; 84 | 85 | int main(int argc, char **argv) 86 | { 87 | char input_file_name[1024]; 88 | char model_file_name[1024]; 89 | const char *error_msg; 90 | 91 | parse_command_line(argc, argv, input_file_name, model_file_name); 92 | read_problem(input_file_name); 93 | error_msg = check_parameter(&prob,¶m); 94 | 95 | if(error_msg) 96 | { 97 | fprintf(stderr,"Error: %s\n",error_msg); 98 | exit(1); 99 | } 100 | 101 | if(flag_cross_validation) 102 | { 103 | do_cross_validation(); 104 | } 105 | else 106 | { 107 | model_=train(&prob, ¶m); 108 | if(save_model(model_file_name, model_)) 109 | { 110 | fprintf(stderr,"can't save model to file %s\n",model_file_name); 111 | exit(1); 112 | } 113 | free_and_destroy_model(&model_); 114 | } 115 | destroy_param(¶m); 116 | free(prob.y); 117 | free(prob.x); 118 | free(x_space); 119 | free(line); 120 | 121 | return 0; 122 | } 123 | 124 | void do_cross_validation() 125 | { 126 | int i; 127 | int total_correct = 0; 128 | int *target = Malloc(int, prob.l); 129 | 130 | cross_validation(&prob,¶m,nr_fold,target); 131 | 132 | for(i=0;i=argc) 160 | exit_with_help(); 161 | switch(argv[i-1][1]) 162 | { 163 | case 's': 164 | param.solver_type = atoi(argv[i]); 165 | break; 166 | 167 | case 'c': 168 | param.C = atof(argv[i]); 169 | break; 170 | 171 | case 'e': 172 | param.eps = atof(argv[i]); 173 | break; 174 | 175 | case 'B': 176 | bias = atof(argv[i]); 177 | break; 178 | 179 | case 'w': 180 | ++param.nr_weight; 181 | param.weight_label = (int *) realloc(param.weight_label,sizeof(int)*param.nr_weight); 182 | param.weight = (double *) realloc(param.weight,sizeof(double)*param.nr_weight); 183 | param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]); 184 | param.weight[param.nr_weight-1] = atof(argv[i]); 185 | break; 186 | 187 | case 'v': 188 | flag_cross_validation = 1; 189 | nr_fold = atoi(argv[i]); 190 | if(nr_fold < 2) 191 | { 192 | fprintf(stderr,"n-fold cross validation: n must >= 2\n"); 193 | exit_with_help(); 194 | } 195 | break; 196 | 197 | case 'q': 198 | print_func = &print_null; 199 | i--; 200 | break; 201 | 202 | default: 203 | fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]); 204 | exit_with_help(); 205 | break; 206 | } 207 | } 208 | 209 | set_print_string_function(print_func); 210 | 211 | // determine filenames 212 | if(i>=argc) 213 | exit_with_help(); 214 | 215 | strcpy(input_file_name, argv[i]); 216 | 217 | if(i max_index) 321 | max_index = inst_max_index; 322 | 323 | if(prob.bias >= 0) 324 | x_space[j++].value = prob.bias; 325 | 326 | x_space[j++].index = -1; 327 | } 328 | 329 | if(prob.bias >= 0) 330 | { 331 | prob.n=max_index+1; 332 | for(i=1;iindex = prob.n; 334 | x_space[j-2].index = prob.n; 335 | } 336 | else 337 | prob.n=max_index; 338 | 339 | fclose(fp); 340 | } 341 | -------------------------------------------------------------------------------- /liblinear-1.8/ruby/linear.rb: -------------------------------------------------------------------------------- 1 | require 'liblinear' 2 | include Liblinear 3 | 4 | def _int_array(seq) 5 | size = seq.size 6 | array = new_int(size) 7 | i = 0 8 | for item in seq 9 | int_setitem(array,i,item) 10 | i = i + 1 11 | end 12 | return array 13 | end 14 | 15 | def _double_array(seq) 16 | size = seq.size 17 | array = new_double(size) 18 | i = 0 19 | for item in seq 20 | double_setitem(array,i,item) 21 | i = i + 1 22 | end 23 | return array 24 | end 25 | 26 | def _free_int_array(x) 27 | if !x.nil? # and !x.empty? 28 | delete_int(x) 29 | end 30 | end 31 | 32 | def _free_double_array(x) 33 | if !x.nil? # and !x.empty? 34 | delete_double(x) 35 | end 36 | end 37 | 38 | def _int_array_to_list(x,n) 39 | list = [] 40 | (0..n-1).each {|i| list << int_getitem(x,i) } 41 | return list 42 | end 43 | 44 | def _double_array_to_list(x,n) 45 | list = [] 46 | (0..n-1).each {|i| list << double_getitem(x,i) } 47 | return list 48 | end 49 | 50 | class LParameter 51 | attr_accessor :param 52 | 53 | def initialize(*args) 54 | @param = Parameter.new 55 | @param.solver_type = L2R_LR 56 | @param.C = 1 57 | @param.eps = 0.01 58 | @param.nr_weight = 0 59 | @param.weight_label = _int_array([]) 60 | @param.weight = _double_array([]) 61 | 62 | args[0].each {|k,v| 63 | self.send("#{k}=",v) 64 | } if !args[0].nil? 65 | end 66 | 67 | def method_missing(m, *args) 68 | #print m.to_s 69 | #puts args.inspect 70 | if m.to_s == 'weight_label=' 71 | @weight_label_len = args[0].size 72 | pargs = _int_array(args[0]) 73 | _free_int_array(@param.weight_label) 74 | elsif m.to_s == 'weight=' 75 | @weight_len = args[0].size 76 | pargs = _double_array(args[0]) 77 | _free_double_array(@param.weight) 78 | else 79 | pargs = args[0] 80 | end 81 | 82 | if m.to_s.index('=') 83 | @param.send("#{m}",pargs) 84 | else 85 | @param.send("#{m}") 86 | end 87 | 88 | end 89 | 90 | def inspect 91 | "LParameter: solver_type=#{@param.solver_type} C=#{@param.C} eps=#{@param.eps}" 92 | end 93 | 94 | def destroy 95 | _free_int_array(@param.weight_label) 96 | _free_double_array(@param.weight) 97 | delete_parameter(@param) 98 | @param = nil 99 | end 100 | end 101 | 102 | def _convert_to_feature_node_array(x, maxlen, bias=-1) 103 | # convert a sequence or mapping to an feature_node array 104 | 105 | # Find non zero elements 106 | iter_range = [] 107 | if x.class == Hash 108 | x.each {|k, v| 109 | # all zeros kept due to the precomputed kernel; no good solution yet 110 | iter_range << k #if v != 0 111 | } 112 | elsif x.class == Array 113 | x.each_index {|j| 114 | iter_range << j #if x[j] != 0 115 | } 116 | else 117 | raise TypeError,"data must be a hash or an array" 118 | end 119 | 120 | iter_range.sort! 121 | if bias >=0 122 | data = feature_node_array(iter_range.size+2) 123 | #puts "bias element (#{iter_range.size},#{bias})" 124 | feature_node_array_set(data,iter_range.size,maxlen+1,bias) 125 | feature_node_array_set(data,iter_range.size+1,-1,0) 126 | else 127 | data = feature_node_array(iter_range.size+1) 128 | feature_node_array_set(data,iter_range.size,-1,0) 129 | end 130 | 131 | j = 0 132 | for k in iter_range 133 | #puts "element #{j}= (#{k},#{x[k]})" 134 | feature_node_array_set(data,j,k,x[k]) 135 | j = j + 1 136 | end 137 | return data 138 | end 139 | 140 | 141 | class LProblem 142 | attr_accessor :prob, :maxlen, :size 143 | 144 | def initialize(y,x,bias) 145 | # assert_equal(y.size, x.size) 146 | @prob = prob = Problem.new 147 | @size = size = y.size 148 | 149 | @y_array = y_array = new_int(size) 150 | for i in (0..size-1) 151 | int_setitem(@y_array,i,y[i]) 152 | end 153 | 154 | @x_matrix = x_matrix = feature_node_matrix(size) 155 | @data = [] 156 | @maxlen = 0 #max number of features 157 | len_array=[] 158 | 159 | for i in (0..size-1) 160 | data = _convert_to_feature_node_array(x[i], @maxlen, bias) 161 | @data << data 162 | feature_node_matrix_set(x_matrix,i,data) 163 | 164 | if x[i].class == Hash 165 | if x[i].size > 0 166 | @maxlen = [@maxlen,x[i].keys.max].max 167 | end 168 | else 169 | @maxlen = [@maxlen,x[i].size].max 170 | end 171 | len_array << x[i].size 172 | end 173 | 174 | if bias >= 0 175 | set_bias_index(x_matrix, size, @maxlen, _int_array(len_array)) 176 | end 177 | 178 | prob.y = y_array 179 | prob.x = x_matrix 180 | prob.bias = bias 181 | prob.l = size 182 | prob.n = @maxlen 183 | if bias >= 0 184 | prob.n += 1 185 | end 186 | end 187 | 188 | def inspect 189 | "LProblem: size = #{size} n=#{prob.n} bias=#{prob.bias} maxlen=#{@maxlen}" 190 | end 191 | 192 | def destroy 193 | delete_problem(@prob) 194 | delete_int(@y_array) 195 | for i in (0..size-1) 196 | feature_node_array_destroy(@data[i]) 197 | end 198 | feature_node_matrix_destroy(@x_matrix) 199 | end 200 | end 201 | 202 | class LModel 203 | attr_accessor :model, :probability 204 | 205 | def initialize(arg1,arg2=nil) 206 | if arg2 == nil 207 | # create model from file 208 | filename = arg1 209 | @model = load_model(filename) 210 | else 211 | # create model from problem and parameter 212 | prob,param = arg1,arg2 213 | @prob = prob 214 | msg = check_parameter(prob.prob,param.param) 215 | raise "ValueError", msg if msg 216 | @model = Liblinear::train(prob.prob,param.param) 217 | end 218 | #setup some classwide variables 219 | @nr_class = Liblinear::get_nr_class(@model) 220 | #create labels(classes) 221 | intarr = new_int(@nr_class) 222 | Liblinear::get_labels(@model,intarr) 223 | @labels = _int_array_to_list(intarr, @nr_class) 224 | delete_int(intarr) 225 | end 226 | 227 | def predict(x) 228 | data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) 229 | ret = Liblinear::predict(@model,data) 230 | feature_node_array_destroy(data) 231 | return ret 232 | end 233 | 234 | 235 | def get_nr_class 236 | return @nr_class 237 | end 238 | 239 | def get_labels 240 | return @labels 241 | end 242 | 243 | def predict_values_raw(x) 244 | #convert x into feature_node, allocate a double array for return 245 | n = (@nr_class*(@nr_class-1)/2).floor 246 | data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) 247 | dblarr = new_double(n) 248 | Liblinear::predict_values(@model, data, dblarr) 249 | ret = _double_array_to_list(dblarr, n) 250 | delete_double(dblarr) 251 | feature_node_array_destroy(data) 252 | return ret 253 | end 254 | 255 | def predict_values(x) 256 | v=predict_values_raw(x) 257 | #puts v.inspect 258 | if false 259 | #if @svm_type == NU_SVR or @svm_type == EPSILON_SVR or @svm_type == ONE_CLASS 260 | return v[0] 261 | else #self.svm_type == C_SVC or self.svm_type == NU_SVC 262 | count = 0 263 | 264 | # create a width x height array 265 | width = @labels.size 266 | height = @labels.size 267 | d = Array.new(width) 268 | d.map! { Array.new(height) } 269 | 270 | for i in (0..@labels.size-1) 271 | for j in (i+1..@labels.size-1) 272 | d[@labels[i]][@labels[j]] = v[count] 273 | d[@labels[j]][@labels[i]] = -v[count] 274 | count += 1 275 | end 276 | end 277 | return d 278 | end 279 | end 280 | 281 | def predict_probability(x) 282 | # if not @probability 283 | # raise TypeError, "model does not support probabiliy estimates" 284 | # end 285 | 286 | #convert x into feature_node, alloc a double array to receive probabilities 287 | data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) 288 | dblarr = new_double(@nr_class) 289 | pred = Liblinear::predict_probability(@model, data, dblarr) 290 | pv = _double_array_to_list(dblarr, @nr_class) 291 | delete_double(dblarr) 292 | feature_node_array_destroy(data) 293 | p = {} 294 | for i in (0..@labels.size-1) 295 | p[@labels[i]] = pv[i] 296 | end 297 | return pred, p 298 | end 299 | 300 | # def get_svr_probability 301 | # #leave the Error checking to svm.cpp code 302 | # ret = Liblinear::get_svr_probability(@model) 303 | # if ret == 0 304 | # raise TypeError, "not a regression model or probability information not available" 305 | # end 306 | # return ret 307 | # end 308 | 309 | # def get_svr_pdf 310 | # #get_svr_probability will handle error checking 311 | # sigma = get_svr_probability() 312 | # return Proc.new{|z| exp(-z.abs/sigma)/(2*sigma)} # TODO: verify this works 313 | # end 314 | 315 | def save(filename) 316 | save_model(filename,@model) 317 | end 318 | 319 | def destroy 320 | destroy_model(@model) 321 | end 322 | end 323 | 324 | def cross_validation(prob, param, fold) 325 | target = new_int(prob.size) 326 | Liblinear::cross_validation(prob.prob, param.param, fold, target) 327 | ret = _int_array_to_list(target, prob.size) 328 | delete_int(target) 329 | return ret 330 | end 331 | 332 | def read_file filename 333 | labels = [] 334 | samples = [] 335 | max_index = 0 336 | 337 | f = File.open(filename) 338 | f.each do |line| 339 | elems = line.split 340 | sample = {} 341 | for e in elems[1..-1] 342 | points = e.split(":") 343 | sample[points[0].to_i] = points[1].to_f 344 | if points[0].to_i < max_index 345 | max_index = points[0].to_i 346 | end 347 | end 348 | labels << elems[0].to_i 349 | samples << sample 350 | #print elems[0].to_i 351 | #print " - " 352 | #puts sample.inspect 353 | end 354 | puts "#{filename}: #{samples.size} samples loaded." 355 | return labels,samples 356 | end 357 | 358 | -------------------------------------------------------------------------------- /lib/linear.rb: -------------------------------------------------------------------------------- 1 | require 'liblinear-ruby-swig/liblinear' 2 | include Liblinear 3 | 4 | def _int_array(seq) 5 | size = seq.size 6 | array = new_int(size) 7 | i = 0 8 | for item in seq 9 | int_setitem(array,i,item) 10 | i = i + 1 11 | end 12 | return array 13 | end 14 | 15 | def _double_array(seq) 16 | size = seq.size 17 | array = new_double(size) 18 | i = 0 19 | for item in seq 20 | double_setitem(array,i,item) 21 | i = i + 1 22 | end 23 | return array 24 | end 25 | 26 | def _free_int_array(x) 27 | if !x.nil? # and !x.empty? 28 | delete_int(x) 29 | end 30 | end 31 | 32 | def _free_double_array(x) 33 | if !x.nil? # and !x.empty? 34 | delete_double(x) 35 | end 36 | end 37 | 38 | def _int_array_to_list(x,n) 39 | list = [] 40 | (0..n-1).each {|i| list << int_getitem(x,i) } 41 | return list 42 | end 43 | 44 | def _double_array_to_list(x,n) 45 | list = [] 46 | (0..n-1).each {|i| list << double_getitem(x,i) } 47 | return list 48 | end 49 | 50 | class LParameter 51 | attr_accessor :param 52 | 53 | def initialize(*args) 54 | @param = Liblinear::Parameter.new 55 | @param.solver_type = L2R_LR 56 | @param.C = 1 57 | @param.eps = 0.01 58 | @param.nr_weight = 0 59 | @param.weight_label = _int_array([]) 60 | @param.weight = _double_array([]) 61 | 62 | args[0].each {|k,v| 63 | self.send("#{k}=",v) 64 | } if !args[0].nil? 65 | end 66 | 67 | def method_missing(m, *args) 68 | #print m.to_s 69 | #puts args.inspect 70 | if m.to_s == 'weight_label=' 71 | @weight_label_len = args[0].size 72 | pargs = _int_array(args[0]) 73 | _free_int_array(@param.weight_label) 74 | elsif m.to_s == 'weight=' 75 | @weight_len = args[0].size 76 | pargs = _double_array(args[0]) 77 | _free_double_array(@param.weight) 78 | else 79 | pargs = args[0] 80 | end 81 | 82 | if m.to_s.index('=') 83 | @param.send("#{m}",pargs) 84 | else 85 | @param.send("#{m}") 86 | end 87 | 88 | end 89 | 90 | def inspect 91 | "LParameter: solver_type=#{@param.solver_type} C=#{@param.C} eps=#{@param.eps}" 92 | end 93 | 94 | def destroy 95 | _free_int_array(@param.weight_label) 96 | _free_double_array(@param.weight) 97 | delete_parameter(@param) 98 | @param = nil 99 | end 100 | end 101 | 102 | def _convert_to_feature_node_array(x, maxlen, bias=-1) 103 | # convert a sequence or mapping to an feature_node array 104 | 105 | # Find non zero elements 106 | iter_range = [] 107 | if x.class == Hash 108 | x.each {|k, v| 109 | # all zeros kept due to the precomputed kernel; no good solution yet 110 | iter_range << k #if v != 0 111 | } 112 | elsif x.class == Array 113 | x.each_index {|j| 114 | iter_range << j #if x[j] != 0 115 | } 116 | else 117 | raise TypeError,"data must be a hash or an array" 118 | end 119 | 120 | iter_range.sort! 121 | if bias >=0 122 | data = feature_node_array(iter_range.size+2) 123 | #puts "bias element (#{iter_range.size},#{bias})" 124 | feature_node_array_set(data,iter_range.size,maxlen+1,bias) 125 | feature_node_array_set(data,iter_range.size+1,-1,0) 126 | else 127 | data = feature_node_array(iter_range.size+1) 128 | feature_node_array_set(data,iter_range.size,-1,0) 129 | end 130 | 131 | j = 0 132 | for k in iter_range 133 | #puts "element #{j}= (#{k},#{x[k]})" 134 | feature_node_array_set(data,j,k,x[k]) 135 | j = j + 1 136 | end 137 | return data 138 | end 139 | 140 | 141 | class LProblem 142 | attr_accessor :prob, :maxlen, :size 143 | 144 | def initialize(y,x,bias) 145 | # assert_equal(y.size, x.size) 146 | @prob = prob = Liblinear::Problem.new 147 | @size = size = y.size 148 | 149 | @y_array = y_array = new_int(size) 150 | for i in (0..size-1) 151 | int_setitem(@y_array,i,y[i]) 152 | end 153 | 154 | @x_matrix = x_matrix = feature_node_matrix(size) 155 | @data = [] 156 | @maxlen = 0 #max number of features 157 | len_array=[] 158 | 159 | for i in (0..size-1) 160 | data = _convert_to_feature_node_array(x[i], @maxlen, bias) 161 | @data << data 162 | feature_node_matrix_set(x_matrix,i,data) 163 | 164 | if x[i].class == Hash 165 | if x[i].size > 0 166 | @maxlen = [@maxlen,x[i].keys.max].max 167 | end 168 | else 169 | @maxlen = [@maxlen,x[i].size].max 170 | end 171 | len_array << x[i].size 172 | end 173 | 174 | if bias >= 0 175 | set_bias_index(x_matrix, size, @maxlen, _int_array(len_array)) 176 | end 177 | 178 | prob.y = y_array 179 | prob.x = x_matrix 180 | prob.bias = bias 181 | prob.l = size 182 | prob.n = @maxlen 183 | if bias >= 0 184 | prob.n += 1 185 | end 186 | end 187 | 188 | def inspect 189 | "LProblem: size = #{size} n=#{prob.n} bias=#{prob.bias} maxlen=#{@maxlen}" 190 | end 191 | 192 | def destroy 193 | delete_problem(@prob) 194 | delete_int(@y_array) 195 | for i in (0..size-1) 196 | feature_node_array_destroy(@data[i]) 197 | end 198 | feature_node_matrix_destroy(@x_matrix) 199 | end 200 | end 201 | 202 | class LModel 203 | attr_accessor :model, :probability 204 | 205 | def initialize(arg1,arg2=nil) 206 | if arg2 == nil 207 | # create model from file 208 | filename = arg1 209 | @model = load_model(filename) 210 | else 211 | # create model from problem and parameter 212 | prob,param = arg1,arg2 213 | @prob = prob 214 | msg = check_parameter(prob.prob,param.param) 215 | raise "ValueError", msg if msg 216 | @model = Liblinear::train(prob.prob,param.param) 217 | end 218 | #setup some classwide variables 219 | @nr_class = Liblinear::get_nr_class(@model) 220 | #create labels(classes) 221 | intarr = new_int(@nr_class) 222 | Liblinear::get_labels(@model,intarr) 223 | @labels = _int_array_to_list(intarr, @nr_class) 224 | delete_int(intarr) 225 | end 226 | 227 | def predict(x) 228 | data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) 229 | ret = Liblinear::predict(@model,data) 230 | feature_node_array_destroy(data) 231 | return ret 232 | end 233 | 234 | 235 | def get_nr_class 236 | return @nr_class 237 | end 238 | 239 | def get_labels 240 | return @labels 241 | end 242 | 243 | def predict_values_raw(x) 244 | #convert x into feature_node, allocate a double array for return 245 | n = (@nr_class*(@nr_class-1)/2).floor 246 | data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) 247 | dblarr = new_double(n) 248 | Liblinear::predict_values(@model, data, dblarr) 249 | ret = _double_array_to_list(dblarr, n) 250 | delete_double(dblarr) 251 | feature_node_array_destroy(data) 252 | return ret 253 | end 254 | 255 | def predict_values(x) 256 | v=predict_values_raw(x) 257 | #puts v.inspect 258 | if false 259 | #if @svm_type == NU_SVR or @svm_type == EPSILON_SVR or @svm_type == ONE_CLASS 260 | return v[0] 261 | else #self.svm_type == C_SVC or self.svm_type == NU_SVC 262 | count = 0 263 | 264 | # create a width x height array 265 | width = @labels.size 266 | height = @labels.size 267 | d = Array.new(width) 268 | d.map! { Array.new(height) } 269 | 270 | for i in (0..@labels.size-1) 271 | for j in (i+1..@labels.size-1) 272 | d[@labels[i]][@labels[j]] = v[count] 273 | d[@labels[j]][@labels[i]] = -v[count] 274 | count += 1 275 | end 276 | end 277 | return d 278 | end 279 | end 280 | 281 | def predict_probability(x) 282 | # if not @probability 283 | # raise TypeError, "model does not support probabiliy estimates" 284 | # end 285 | 286 | #convert x into feature_node, alloc a double array to receive probabilities 287 | data = _convert_to_feature_node_array(x, @model.nr_feature, @model.bias) 288 | dblarr = new_double(@nr_class) 289 | pred = Liblinear::predict_probability(@model, data, dblarr) 290 | pv = _double_array_to_list(dblarr, @nr_class) 291 | delete_double(dblarr) 292 | feature_node_array_destroy(data) 293 | p = {} 294 | for i in (0..@labels.size-1) 295 | p[@labels[i]] = pv[i] 296 | end 297 | return pred, p 298 | end 299 | 300 | # def get_svr_probability 301 | # #leave the Error checking to svm.cpp code 302 | # ret = Liblinear::get_svr_probability(@model) 303 | # if ret == 0 304 | # raise TypeError, "not a regression model or probability information not available" 305 | # end 306 | # return ret 307 | # end 308 | 309 | # def get_svr_pdf 310 | # #get_svr_probability will handle error checking 311 | # sigma = get_svr_probability() 312 | # return Proc.new{|z| exp(-z.abs/sigma)/(2*sigma)} # TODO: verify this works 313 | # end 314 | 315 | def save(filename) 316 | save_model(filename,@model) 317 | end 318 | 319 | def destroy 320 | destroy_model(@model) 321 | end 322 | end 323 | 324 | def cross_validation(prob, param, fold) 325 | target = new_int(prob.size) 326 | Liblinear::cross_validation(prob.prob, param.param, fold, target) 327 | ret = _int_array_to_list(target, prob.size) 328 | delete_int(target) 329 | return ret 330 | end 331 | 332 | def read_file filename 333 | labels = [] 334 | samples = [] 335 | max_index = 0 336 | 337 | f = File.open(filename) 338 | f.each do |line| 339 | elems = line.split 340 | sample = {} 341 | for e in elems[1..-1] 342 | points = e.split(":") 343 | sample[points[0].to_i] = points[1].to_f 344 | if points[0].to_i < max_index 345 | max_index = points[0].to_i 346 | end 347 | end 348 | labels << elems[0].to_i 349 | samples << sample 350 | #print elems[0].to_i 351 | #print " - " 352 | #puts sample.inspect 353 | end 354 | puts "#{filename}: #{samples.size} samples loaded." 355 | return labels,samples 356 | end 357 | 358 | -------------------------------------------------------------------------------- /liblinear-1.8/matlab/train.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "linear.h" 7 | 8 | #include "mex.h" 9 | #include "linear_model_matlab.h" 10 | 11 | #if MX_API_VER < 0x07030000 12 | typedef int mwIndex; 13 | #endif 14 | 15 | #define CMD_LEN 2048 16 | #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 17 | #define INF HUGE_VAL 18 | 19 | void print_null(const char *s) {} 20 | void print_string_matlab(const char *s) {mexPrintf(s);} 21 | 22 | void exit_with_help() 23 | { 24 | mexPrintf( 25 | "Usage: model = train(training_label_vector, training_instance_matrix, 'liblinear_options', 'col');\n" 26 | "liblinear_options:\n" 27 | "-s type : set type of solver (default 1)\n" 28 | " 0 -- L2-regularized logistic regression (primal)\n" 29 | " 1 -- L2-regularized L2-loss support vector classification (dual)\n" 30 | " 2 -- L2-regularized L2-loss support vector classification (primal)\n" 31 | " 3 -- L2-regularized L1-loss support vector classification (dual)\n" 32 | " 4 -- multi-class support vector classification by Crammer and Singer\n" 33 | " 5 -- L1-regularized L2-loss support vector classification\n" 34 | " 6 -- L1-regularized logistic regression\n" 35 | " 7 -- L2-regularized logistic regression (dual)\n" 36 | "-c cost : set the parameter C (default 1)\n" 37 | "-e epsilon : set tolerance of termination criterion\n" 38 | " -s 0 and 2\n" 39 | " |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2,\n" 40 | " where f is the primal function and pos/neg are # of\n" 41 | " positive/negative data (default 0.01)\n" 42 | " -s 1, 3, 4 and 7\n" 43 | " Dual maximal violation <= eps; similar to libsvm (default 0.1)\n" 44 | " -s 5 and 6\n" 45 | " |f'(w)|_1 <= eps*min(pos,neg)/l*|f'(w0)|_1,\n" 46 | " where f is the primal function (default 0.01)\n" 47 | "-B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1)\n" 48 | "-wi weight: weights adjust the parameter C of different classes (see README for details)\n" 49 | "-v n: n-fold cross validation mode\n" 50 | "-q : quiet mode (no outputs)\n" 51 | "col:\n" 52 | " if 'col' is setted, training_instance_matrix is parsed in column format, otherwise is in row format\n" 53 | ); 54 | } 55 | 56 | // liblinear arguments 57 | struct parameter param; // set by parse_command_line 58 | struct problem prob; // set by read_problem 59 | struct model *model_; 60 | struct feature_node *x_space; 61 | int cross_validation_flag; 62 | int col_format_flag; 63 | int nr_fold; 64 | double bias; 65 | 66 | double do_cross_validation() 67 | { 68 | int i; 69 | int total_correct = 0; 70 | int *target = Malloc(int,prob.l); 71 | double retval = 0.0; 72 | 73 | cross_validation(&prob,¶m,nr_fold,target); 74 | 75 | for(i=0;i 2) 117 | { 118 | mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1); 119 | if((argv[argc] = strtok(cmd, " ")) != NULL) 120 | while((argv[++argc] = strtok(NULL, " ")) != NULL) 121 | ; 122 | } 123 | 124 | // parse options 125 | for(i=1;i=argc && argv[i-1][1] != 'q') // since option -q has no parameter 130 | return 1; 131 | switch(argv[i-1][1]) 132 | { 133 | case 's': 134 | param.solver_type = atoi(argv[i]); 135 | break; 136 | case 'c': 137 | param.C = atof(argv[i]); 138 | break; 139 | case 'e': 140 | param.eps = atof(argv[i]); 141 | break; 142 | case 'B': 143 | bias = atof(argv[i]); 144 | break; 145 | case 'v': 146 | cross_validation_flag = 1; 147 | nr_fold = atoi(argv[i]); 148 | if(nr_fold < 2) 149 | { 150 | mexPrintf("n-fold cross validation: n must >= 2\n"); 151 | return 1; 152 | } 153 | break; 154 | case 'w': 155 | ++param.nr_weight; 156 | param.weight_label = (int *) realloc(param.weight_label,sizeof(int)*param.nr_weight); 157 | param.weight = (double *) realloc(param.weight,sizeof(double)*param.nr_weight); 158 | param.weight_label[param.nr_weight-1] = atoi(&argv[i-1][2]); 159 | param.weight[param.nr_weight-1] = atof(argv[i]); 160 | break; 161 | case 'q': 162 | print_func = &print_null; 163 | i--; 164 | break; 165 | default: 166 | mexPrintf("unknown option\n"); 167 | return 1; 168 | } 169 | } 170 | 171 | set_print_string_function(print_func); 172 | 173 | if(param.eps == INF) 174 | { 175 | if(param.solver_type == L2R_LR || param.solver_type == L2R_L2LOSS_SVC) 176 | param.eps = 0.01; 177 | else if(param.solver_type == L2R_L2LOSS_SVC_DUAL || param.solver_type == L2R_L1LOSS_SVC_DUAL || param.solver_type == MCSVM_CS || param.solver_type == L2R_LR_DUAL) 178 | param.eps = 0.1; 179 | else if(param.solver_type == L1R_L2LOSS_SVC || param.solver_type == L1R_LR) 180 | param.eps = 0.01; 181 | } 182 | return 0; 183 | } 184 | 185 | static void fake_answer(mxArray *plhs[]) 186 | { 187 | plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); 188 | } 189 | 190 | int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat) 191 | { 192 | int i, j, k, low, high; 193 | mwIndex *ir, *jc; 194 | int elements, max_index, num_samples, label_vector_row_num; 195 | double *samples, *labels; 196 | mxArray *instance_mat_col; // instance sparse matrix in column format 197 | 198 | prob.x = NULL; 199 | prob.y = NULL; 200 | x_space = NULL; 201 | 202 | if(col_format_flag) 203 | instance_mat_col = (mxArray *)instance_mat; 204 | else 205 | { 206 | // transpose instance matrix 207 | mxArray *prhs[1], *plhs[1]; 208 | prhs[0] = mxDuplicateArray(instance_mat); 209 | if(mexCallMATLAB(1, plhs, 1, prhs, "transpose")) 210 | { 211 | mexPrintf("Error: cannot transpose training instance matrix\n"); 212 | return -1; 213 | } 214 | instance_mat_col = plhs[0]; 215 | mxDestroyArray(prhs[0]); 216 | } 217 | 218 | // the number of instance 219 | prob.l = (int) mxGetN(instance_mat_col); 220 | label_vector_row_num = (int) mxGetM(label_vec); 221 | 222 | if(label_vector_row_num!=prob.l) 223 | { 224 | mexPrintf("Length of label vector does not match # of instances.\n"); 225 | return -1; 226 | } 227 | 228 | // each column is one instance 229 | labels = mxGetPr(label_vec); 230 | samples = mxGetPr(instance_mat_col); 231 | ir = mxGetIr(instance_mat_col); 232 | jc = mxGetJc(instance_mat_col); 233 | 234 | num_samples = (int) mxGetNzmax(instance_mat_col); 235 | 236 | elements = num_samples + prob.l*2; 237 | max_index = (int) mxGetM(instance_mat_col); 238 | 239 | prob.y = Malloc(int, prob.l); 240 | prob.x = Malloc(struct feature_node*, prob.l); 241 | x_space = Malloc(struct feature_node, elements); 242 | 243 | prob.bias=bias; 244 | 245 | j = 0; 246 | for(i=0;i=0) 258 | { 259 | x_space[j].index = max_index+1; 260 | x_space[j].value = prob.bias; 261 | j++; 262 | } 263 | x_space[j++].index = -1; 264 | } 265 | 266 | if(prob.bias>=0) 267 | prob.n = max_index+1; 268 | else 269 | prob.n = max_index; 270 | 271 | return 0; 272 | } 273 | 274 | // Interface function of matlab 275 | // now assume prhs[0]: label prhs[1]: features 276 | void mexFunction( int nlhs, mxArray *plhs[], 277 | int nrhs, const mxArray *prhs[] ) 278 | { 279 | const char *error_msg; 280 | // fix random seed to have same results for each run 281 | // (for cross validation) 282 | srand(1); 283 | 284 | // Transform the input Matrix to libsvm format 285 | if(nrhs > 1 && nrhs < 5) 286 | { 287 | int err=0; 288 | 289 | if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) { 290 | mexPrintf("Error: label vector and instance matrix must be double\n"); 291 | fake_answer(plhs); 292 | return; 293 | } 294 | 295 | if(parse_command_line(nrhs, prhs, NULL)) 296 | { 297 | exit_with_help(); 298 | destroy_param(¶m); 299 | fake_answer(plhs); 300 | return; 301 | } 302 | 303 | if(mxIsSparse(prhs[1])) 304 | err = read_problem_sparse(prhs[0], prhs[1]); 305 | else 306 | { 307 | mexPrintf("Training_instance_matrix must be sparse\n"); 308 | destroy_param(¶m); 309 | fake_answer(plhs); 310 | return; 311 | } 312 | 313 | // train's original code 314 | error_msg = check_parameter(&prob, ¶m); 315 | 316 | if(err || error_msg) 317 | { 318 | if (error_msg != NULL) 319 | mexPrintf("Error: %s\n", error_msg); 320 | destroy_param(¶m); 321 | free(prob.y); 322 | free(prob.x); 323 | free(x_space); 324 | fake_answer(plhs); 325 | return; 326 | } 327 | 328 | if(cross_validation_flag) 329 | { 330 | double *ptr; 331 | plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); 332 | ptr = mxGetPr(plhs[0]); 333 | ptr[0] = do_cross_validation(); 334 | } 335 | else 336 | { 337 | const char *error_msg; 338 | 339 | model_ = train(&prob, ¶m); 340 | error_msg = model_to_matlab_structure(plhs, model_); 341 | if(error_msg) 342 | mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg); 343 | free_and_destroy_model(&model_); 344 | } 345 | destroy_param(¶m); 346 | free(prob.y); 347 | free(prob.x); 348 | free(x_space); 349 | } 350 | else 351 | { 352 | exit_with_help(); 353 | fake_answer(plhs); 354 | return; 355 | } 356 | } 357 | -------------------------------------------------------------------------------- /liblinear-1.8/python/README: -------------------------------------------------------------------------------- 1 | ------------------------------------- 2 | --- Python interface of LIBLINEAR --- 3 | ------------------------------------- 4 | 5 | Table of Contents 6 | ================= 7 | 8 | - Introduction 9 | - Installation 10 | - Quick Start 11 | - Design Description 12 | - Data Structures 13 | - Utility Functions 14 | - Additional Information 15 | 16 | Introduction 17 | ============ 18 | 19 | Python (http://www.python.org/) is a programming language suitable for rapid 20 | development. This tool provides a simple Python interface to LIBLINEAR, a library 21 | for support vector machines (http://www.csie.ntu.edu.tw/~cjlin/liblinear). The 22 | interface is very easy to use as the usage is the same as that of LIBLINEAR. The 23 | interface is developed with the built-in Python library "ctypes." 24 | 25 | Installation 26 | ============ 27 | 28 | On Unix systems, type 29 | 30 | > make 31 | 32 | The interface needs only LIBLINEAR shared library, which is generated by 33 | the above command. We assume that the shared library is on the LIBLINEAR 34 | main directory or in the system path. 35 | 36 | For windows, the shared library liblinear.dll is ready in the directory 37 | `..\windows'. You can also copy it to the system directory (e.g., 38 | `C:\WINDOWS\system32\' for Windows XP). To regenerate the shared library, 39 | please follow the instruction of building windows binaries in LIBLINEAR README. 40 | 41 | Quick Start 42 | =========== 43 | 44 | There are two levels of usage. The high-level one uses utility functions 45 | in liblinearutil.py and the usage is the same as the LIBLINEAR MATLAB interface. 46 | 47 | >>> from liblinearutil import * 48 | # Read data in LIBSVM format 49 | >>> y, x = svm_read_problem('../heart_scale') 50 | >>> m = train(y[:200], x[:200], '-c 4') 51 | >>> p_label, p_acc, p_val = predict(y[200:], x[200:], m) 52 | 53 | # Construct problem in python format 54 | # Dense data 55 | >>> y, x = [1,-1], [[1,0,1], [-1,0,-1]] 56 | # Sparse data 57 | >>> y, x = [1,-1], [{1:1, 3:1}, {1:-1,3:-1}] 58 | >>> prob = problem(y, x) 59 | >>> param = parameter('-c 4 -B 1') 60 | >>> m = train(prob, param) 61 | 62 | # Other utility functions 63 | >>> save_model('heart_scale.model', m) 64 | >>> m = load_model('heart_scale.model') 65 | >>> p_label, p_acc, p_val = predict(y, x, m, '-b 1') 66 | >>> ACC = evaluations(y, p_val) 67 | 68 | # Getting online help 69 | >>> help(train) 70 | 71 | The low-level use directly calls C interfaces imported by liblinear.py. Note that 72 | all arguments and return values are in ctypes format. You need to handle them 73 | carefully. 74 | 75 | >>> from liblinear import * 76 | >>> prob = problem([1,-1], [{1:1, 3:1}, {1:-1,3:-1}]) 77 | >>> param = parameter('-c 4') 78 | >>> m = liblinear.train(prob, param) # m is a ctype pointer to a model 79 | # Convet a Python-fromat instance to feature_nodearray, a ctypes structure 80 | >>> x0, max_idx = gen_feature_nodearray({1:1, 3:1}) 81 | >>> label = liblinear.predict(m, x0) 82 | 83 | Design Description 84 | ================== 85 | 86 | There are two files liblinear.py and liblinearutil.py, which respectively correspond to 87 | low-level and high-level use of the interface. 88 | 89 | In liblinear.py, we adopt the Python built-in library "ctypes," so that 90 | Python can directly access C structures and interface functions defined 91 | in linear.h. 92 | 93 | While advanced users can use structures/functions in liblinear.py, to 94 | avoid handling ctypes structures, in liblinearutil.py we provide some easy-to-use 95 | functions. The usage is similar to LIBLINEAR MATLAB interface. 96 | 97 | Data Structures 98 | =============== 99 | 100 | Three data structures derived from linear.h are node, problem, and 101 | parameter. They all contain fields with the same names in 102 | linear.h. Access these fields carefully because you directly use a C structure 103 | instead of a Python object. The following description introduces additional 104 | fields and methods. 105 | 106 | Before using the data structures, execute the following command to load the 107 | LIBLINEAR shared library: 108 | 109 | >>> from liblinear import * 110 | 111 | - class feature_node: 112 | 113 | Construct an feature_node. 114 | 115 | >>> node = feature_node(idx, val) 116 | 117 | idx: an integer indicates the feature index. 118 | 119 | val: a float indicates the feature value. 120 | 121 | - Function: gen_feature_nodearray(xi [,feature_max=None [,issparse=True]]) 122 | 123 | Generate a feature vector from a Python list/tuple or a dictionary: 124 | 125 | >>> xi, max_idx = gen_feature_nodearray({1:1, 3:1, 5:-2}) 126 | 127 | xi: the returned feature_nodearray (a ctypes structure) 128 | 129 | max_idx: the maximal feature index of xi 130 | 131 | issparse: if issparse == True, zero feature values are removed. The default 132 | value is True for the sparsity. 133 | 134 | feature_max: if feature_max is assigned, features with indices larger than 135 | feature_max are removed. 136 | 137 | - class problem: 138 | 139 | Construct an problem instance 140 | 141 | >>> prob = problem(y, x, [bias=-1]) 142 | 143 | y: a Python list/tuple of l labels (type must be int/double). 144 | 145 | x: a Python list/tuple of l data instances. Each element of x must be 146 | an instance of list/tuple/dictionary type. 147 | 148 | bias: if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term 149 | added (default -1) 150 | 151 | You can alos modify the bias value by 152 | 153 | >>> prob.set_bias(1) 154 | 155 | Note that if your x contains sparse data (i.e., dictionary), the internal 156 | ctypes data format is still sparse. 157 | 158 | - class parameter: 159 | 160 | Construct an parameter instance 161 | 162 | >>> param = parameter('training_options') 163 | 164 | If 'training_options' is empty, LIBLINEAR default values are applied. 165 | 166 | Set param to LIBLINEAR default values. 167 | 168 | >>> param.set_to_default_values() 169 | 170 | Parse a string of options. 171 | 172 | >>> param.parse_options('training_options') 173 | 174 | Show values of parameters. 175 | 176 | >>> param.show() 177 | 178 | - class model: 179 | 180 | There are two ways to obtain an instance of model: 181 | 182 | >>> model_ = train(y, x) 183 | >>> model_ = load_model('model_file_name') 184 | 185 | Note that the returned structure of interface functions 186 | liblinear.train and liblinear.load_model is a ctypes pointer of 187 | model, which is different from the model object returned 188 | by train and load_model in liblinearutil.py. We provide a 189 | function toPyModel for the conversion: 190 | 191 | >>> model_ptr = liblinear.train(prob, param) 192 | >>> model_ = toPyModel(model_ptr) 193 | 194 | If you obtain a model in a way other than the above approaches, 195 | handle it carefully to avoid memory leak or segmentation fault. 196 | 197 | Some interface functions to access LIBLINEAR models are wrapped as 198 | members of the class model: 199 | 200 | >>> type = model_.get_type() 201 | >>> nr_feature = model_.get_nr_feature() 202 | >>> nr_class = model_.get_nr_class() 203 | >>> class_labels = model_.get_labels() 204 | >>> is_prob_model = model_.is_probability_model() 205 | 206 | Utility Functions 207 | ================= 208 | 209 | To use utility functions, type 210 | 211 | >>> from liblinearutil import * 212 | 213 | The above command loads 214 | train() : train an linear model 215 | predict() : predict testing data 216 | svm_read_problem() : read the data from a LIBSVM-format file. 217 | load_model() : load a LIBLINEAR model. 218 | save_model() : save model to a file. 219 | evaluations() : evaluate prediction results. 220 | 221 | - Function: train 222 | 223 | There are three ways to call train() 224 | 225 | >>> model = train(y, x [, 'training_options']) 226 | >>> model = train(prob [, 'training_options']) 227 | >>> model = train(prob, param) 228 | 229 | y: a list/tuple of l training labels (type must be int/double). 230 | 231 | x: a list/tuple of l training instances. The feature vector of 232 | each training instance is an instance of list/tuple or dictionary. 233 | 234 | training_options: a string in the same form as that for LIBLINEAR command 235 | mode. 236 | 237 | prob: an problem instance generated by calling 238 | problem(y, x). 239 | 240 | param: an parameter instance generated by calling 241 | parameter('training_options') 242 | 243 | model: the returned model instance. See linear.h for details of this 244 | structure. If '-v' is specified, cross validation is 245 | conducted and the returned model is just a scalar: cross-validation 246 | accuracy for classification and mean-squared error for regression. 247 | 248 | To train the same data many times with different 249 | parameters, the second and the third ways should be faster.. 250 | 251 | Examples: 252 | 253 | >>> y, x = svm_read_problem('../heart_scale') 254 | >>> prob = problem(y, x) 255 | >>> param = parameter('-s 3 -c 5 -q') 256 | >>> m = train(y, x, '-c 5') 257 | >>> m = train(prob, '-w1 5 -c 5') 258 | >>> m = train(prob, param) 259 | >>> CV_ACC = train(y, x, '-v 3') 260 | 261 | - Function: predict 262 | 263 | To predict testing data with a model, use 264 | 265 | >>> p_labs, p_acc, p_vals = predict(y, x, model [,'predicting_options']) 266 | 267 | y: a list/tuple of l true labels (type must be int/double). It is used 268 | for calculating the accuracy. Use [] if true labels are 269 | unavailable. 270 | 271 | x: a list/tuple of l predicting instances. The feature vector of 272 | each predicting instance is an instance of list/tuple or dictionary. 273 | 274 | predicting_options: a string of predicting options in the same format as 275 | that of LIBLINEAR. 276 | 277 | model: an model instance. 278 | 279 | p_labels: a list of predicted labels 280 | 281 | p_acc: testing accuracy 282 | 283 | p_vals: a list of decision values or probability estimates (if '-b 1' 284 | is specified). If k is the number of classes, for decision values, 285 | each element includes results of predicting k binary-class 286 | SVMs. if k = 2 and solver is not MCSVM_CS, only one decision value 287 | is returned. For probabilities, each element contains k values 288 | indicating the probability that the testing instance is in each class. 289 | Note that the order of classes here is the same as 'model.label' 290 | field in the model structure. 291 | 292 | Example: 293 | 294 | >>> m = train(y, x, '-c 5') 295 | >>> p_labels, p_acc, p_vals = predict(y, x, m) 296 | 297 | - Functions: svm_read_problem/load_model/save_model 298 | 299 | See the usage by examples: 300 | 301 | >>> y, x = svm_read_problem('data.txt') 302 | >>> m = load_model('model_file') 303 | >>> save_model('model_file', m) 304 | 305 | - Function: evaluations 306 | 307 | Calculate some evaluations using the true values (ty) and predicted 308 | values (pv): 309 | 310 | >>> ACC = evaluations(ty, pv) 311 | 312 | ty: a list of true values. 313 | 314 | pv: a list of predict values. 315 | 316 | ACC: accuracy. 317 | 318 | 319 | Additional Information 320 | ====================== 321 | 322 | This interface was written by Hsiang-Fu Yu from Department of Computer 323 | Science, National Taiwan University. If you find this tool useful, please 324 | cite LIBLINEAR as follows 325 | 326 | R.-E. Fan, K.-W. Chang, C.-J. Hsieh, X.-R. Wang, and C.-J. Lin. 327 | LIBLINEAR: A Library for Large Linear Classification, Journal of 328 | Machine Learning Research 9(2008), 1871-1874. Software available at 329 | http://www.csie.ntu.edu.tw/~cjlin/liblinear 330 | 331 | For any question, please contact Chih-Jen Lin , 332 | or check the FAQ page: 333 | 334 | http://www.csie.ntu.edu.tw/~cjlin/liblinear/faq.html 335 | -------------------------------------------------------------------------------- /ext/blasp.h: -------------------------------------------------------------------------------- 1 | /* blasp.h -- C prototypes for BLAS Ver 1.0 */ 2 | /* Jesse Bennett March 23, 2000 */ 3 | 4 | /* Functions listed in alphabetical order */ 5 | 6 | #ifdef F2C_COMPAT 7 | 8 | void cdotc_(fcomplex *dotval, int *n, fcomplex *cx, int *incx, 9 | fcomplex *cy, int *incy); 10 | 11 | void cdotu_(fcomplex *dotval, int *n, fcomplex *cx, int *incx, 12 | fcomplex *cy, int *incy); 13 | 14 | double sasum_(int *n, float *sx, int *incx); 15 | 16 | double scasum_(int *n, fcomplex *cx, int *incx); 17 | 18 | double scnrm2_(int *n, fcomplex *x, int *incx); 19 | 20 | double sdot_(int *n, float *sx, int *incx, float *sy, int *incy); 21 | 22 | double snrm2_(int *n, float *x, int *incx); 23 | 24 | void zdotc_(dcomplex *dotval, int *n, dcomplex *cx, int *incx, 25 | dcomplex *cy, int *incy); 26 | 27 | void zdotu_(dcomplex *dotval, int *n, dcomplex *cx, int *incx, 28 | dcomplex *cy, int *incy); 29 | 30 | #else 31 | 32 | fcomplex cdotc_(int *n, fcomplex *cx, int *incx, fcomplex *cy, int *incy); 33 | 34 | fcomplex cdotu_(int *n, fcomplex *cx, int *incx, fcomplex *cy, int *incy); 35 | 36 | float sasum_(int *n, float *sx, int *incx); 37 | 38 | float scasum_(int *n, fcomplex *cx, int *incx); 39 | 40 | float scnrm2_(int *n, fcomplex *x, int *incx); 41 | 42 | float sdot_(int *n, float *sx, int *incx, float *sy, int *incy); 43 | 44 | float snrm2_(int *n, float *x, int *incx); 45 | 46 | dcomplex zdotc_(int *n, dcomplex *cx, int *incx, dcomplex *cy, int *incy); 47 | 48 | dcomplex zdotu_(int *n, dcomplex *cx, int *incx, dcomplex *cy, int *incy); 49 | 50 | #endif 51 | 52 | /* Remaining functions listed in alphabetical order */ 53 | 54 | int caxpy_(int *n, fcomplex *ca, fcomplex *cx, int *incx, fcomplex *cy, 55 | int *incy); 56 | 57 | int ccopy_(int *n, fcomplex *cx, int *incx, fcomplex *cy, int *incy); 58 | 59 | int cgbmv_(char *trans, int *m, int *n, int *kl, int *ku, 60 | fcomplex *alpha, fcomplex *a, int *lda, fcomplex *x, int *incx, 61 | fcomplex *beta, fcomplex *y, int *incy); 62 | 63 | int cgemm_(char *transa, char *transb, int *m, int *n, int *k, 64 | fcomplex *alpha, fcomplex *a, int *lda, fcomplex *b, int *ldb, 65 | fcomplex *beta, fcomplex *c, int *ldc); 66 | 67 | int cgemv_(char *trans, int *m, int *n, fcomplex *alpha, fcomplex *a, 68 | int *lda, fcomplex *x, int *incx, fcomplex *beta, fcomplex *y, 69 | int *incy); 70 | 71 | int cgerc_(int *m, int *n, fcomplex *alpha, fcomplex *x, int *incx, 72 | fcomplex *y, int *incy, fcomplex *a, int *lda); 73 | 74 | int cgeru_(int *m, int *n, fcomplex *alpha, fcomplex *x, int *incx, 75 | fcomplex *y, int *incy, fcomplex *a, int *lda); 76 | 77 | int chbmv_(char *uplo, int *n, int *k, fcomplex *alpha, fcomplex *a, 78 | int *lda, fcomplex *x, int *incx, fcomplex *beta, fcomplex *y, 79 | int *incy); 80 | 81 | int chemm_(char *side, char *uplo, int *m, int *n, fcomplex *alpha, 82 | fcomplex *a, int *lda, fcomplex *b, int *ldb, fcomplex *beta, 83 | fcomplex *c, int *ldc); 84 | 85 | int chemv_(char *uplo, int *n, fcomplex *alpha, fcomplex *a, int *lda, 86 | fcomplex *x, int *incx, fcomplex *beta, fcomplex *y, int *incy); 87 | 88 | int cher_(char *uplo, int *n, float *alpha, fcomplex *x, int *incx, 89 | fcomplex *a, int *lda); 90 | 91 | int cher2_(char *uplo, int *n, fcomplex *alpha, fcomplex *x, int *incx, 92 | fcomplex *y, int *incy, fcomplex *a, int *lda); 93 | 94 | int cher2k_(char *uplo, char *trans, int *n, int *k, fcomplex *alpha, 95 | fcomplex *a, int *lda, fcomplex *b, int *ldb, float *beta, 96 | fcomplex *c, int *ldc); 97 | 98 | int cherk_(char *uplo, char *trans, int *n, int *k, float *alpha, 99 | fcomplex *a, int *lda, float *beta, fcomplex *c, int *ldc); 100 | 101 | int chpmv_(char *uplo, int *n, fcomplex *alpha, fcomplex *ap, fcomplex *x, 102 | int *incx, fcomplex *beta, fcomplex *y, int *incy); 103 | 104 | int chpr_(char *uplo, int *n, float *alpha, fcomplex *x, int *incx, 105 | fcomplex *ap); 106 | 107 | int chpr2_(char *uplo, int *n, fcomplex *alpha, fcomplex *x, int *incx, 108 | fcomplex *y, int *incy, fcomplex *ap); 109 | 110 | int crotg_(fcomplex *ca, fcomplex *cb, float *c, fcomplex *s); 111 | 112 | int cscal_(int *n, fcomplex *ca, fcomplex *cx, int *incx); 113 | 114 | int csscal_(int *n, float *sa, fcomplex *cx, int *incx); 115 | 116 | int cswap_(int *n, fcomplex *cx, int *incx, fcomplex *cy, int *incy); 117 | 118 | int csymm_(char *side, char *uplo, int *m, int *n, fcomplex *alpha, 119 | fcomplex *a, int *lda, fcomplex *b, int *ldb, fcomplex *beta, 120 | fcomplex *c, int *ldc); 121 | 122 | int csyr2k_(char *uplo, char *trans, int *n, int *k, fcomplex *alpha, 123 | fcomplex *a, int *lda, fcomplex *b, int *ldb, fcomplex *beta, 124 | fcomplex *c, int *ldc); 125 | 126 | int csyrk_(char *uplo, char *trans, int *n, int *k, fcomplex *alpha, 127 | fcomplex *a, int *lda, fcomplex *beta, fcomplex *c, int *ldc); 128 | 129 | int ctbmv_(char *uplo, char *trans, char *diag, int *n, int *k, 130 | fcomplex *a, int *lda, fcomplex *x, int *incx); 131 | 132 | int ctbsv_(char *uplo, char *trans, char *diag, int *n, int *k, 133 | fcomplex *a, int *lda, fcomplex *x, int *incx); 134 | 135 | int ctpmv_(char *uplo, char *trans, char *diag, int *n, fcomplex *ap, 136 | fcomplex *x, int *incx); 137 | 138 | int ctpsv_(char *uplo, char *trans, char *diag, int *n, fcomplex *ap, 139 | fcomplex *x, int *incx); 140 | 141 | int ctrmm_(char *side, char *uplo, char *transa, char *diag, int *m, 142 | int *n, fcomplex *alpha, fcomplex *a, int *lda, fcomplex *b, 143 | int *ldb); 144 | 145 | int ctrmv_(char *uplo, char *trans, char *diag, int *n, fcomplex *a, 146 | int *lda, fcomplex *x, int *incx); 147 | 148 | int ctrsm_(char *side, char *uplo, char *transa, char *diag, int *m, 149 | int *n, fcomplex *alpha, fcomplex *a, int *lda, fcomplex *b, 150 | int *ldb); 151 | 152 | int ctrsv_(char *uplo, char *trans, char *diag, int *n, fcomplex *a, 153 | int *lda, fcomplex *x, int *incx); 154 | 155 | int daxpy_(int *n, double *sa, double *sx, int *incx, double *sy, 156 | int *incy); 157 | 158 | int dcopy_(int *n, double *sx, int *incx, double *sy, int *incy); 159 | 160 | int dgbmv_(char *trans, int *m, int *n, int *kl, int *ku, 161 | double *alpha, double *a, int *lda, double *x, int *incx, 162 | double *beta, double *y, int *incy); 163 | 164 | int dgemm_(char *transa, char *transb, int *m, int *n, int *k, 165 | double *alpha, double *a, int *lda, double *b, int *ldb, 166 | double *beta, double *c, int *ldc); 167 | 168 | int dgemv_(char *trans, int *m, int *n, double *alpha, double *a, 169 | int *lda, double *x, int *incx, double *beta, double *y, 170 | int *incy); 171 | 172 | int dger_(int *m, int *n, double *alpha, double *x, int *incx, 173 | double *y, int *incy, double *a, int *lda); 174 | 175 | int drot_(int *n, double *sx, int *incx, double *sy, int *incy, 176 | double *c, double *s); 177 | 178 | int drotg_(double *sa, double *sb, double *c, double *s); 179 | 180 | int dsbmv_(char *uplo, int *n, int *k, double *alpha, double *a, 181 | int *lda, double *x, int *incx, double *beta, double *y, 182 | int *incy); 183 | 184 | int dscal_(int *n, double *sa, double *sx, int *incx); 185 | 186 | int dspmv_(char *uplo, int *n, double *alpha, double *ap, double *x, 187 | int *incx, double *beta, double *y, int *incy); 188 | 189 | int dspr_(char *uplo, int *n, double *alpha, double *x, int *incx, 190 | double *ap); 191 | 192 | int dspr2_(char *uplo, int *n, double *alpha, double *x, int *incx, 193 | double *y, int *incy, double *ap); 194 | 195 | int dswap_(int *n, double *sx, int *incx, double *sy, int *incy); 196 | 197 | int dsymm_(char *side, char *uplo, int *m, int *n, double *alpha, 198 | double *a, int *lda, double *b, int *ldb, double *beta, 199 | double *c, int *ldc); 200 | 201 | int dsymv_(char *uplo, int *n, double *alpha, double *a, int *lda, 202 | double *x, int *incx, double *beta, double *y, int *incy); 203 | 204 | int dsyr_(char *uplo, int *n, double *alpha, double *x, int *incx, 205 | double *a, int *lda); 206 | 207 | int dsyr2_(char *uplo, int *n, double *alpha, double *x, int *incx, 208 | double *y, int *incy, double *a, int *lda); 209 | 210 | int dsyr2k_(char *uplo, char *trans, int *n, int *k, double *alpha, 211 | double *a, int *lda, double *b, int *ldb, double *beta, 212 | double *c, int *ldc); 213 | 214 | int dsyrk_(char *uplo, char *trans, int *n, int *k, double *alpha, 215 | double *a, int *lda, double *beta, double *c, int *ldc); 216 | 217 | int dtbmv_(char *uplo, char *trans, char *diag, int *n, int *k, 218 | double *a, int *lda, double *x, int *incx); 219 | 220 | int dtbsv_(char *uplo, char *trans, char *diag, int *n, int *k, 221 | double *a, int *lda, double *x, int *incx); 222 | 223 | int dtpmv_(char *uplo, char *trans, char *diag, int *n, double *ap, 224 | double *x, int *incx); 225 | 226 | int dtpsv_(char *uplo, char *trans, char *diag, int *n, double *ap, 227 | double *x, int *incx); 228 | 229 | int dtrmm_(char *side, char *uplo, char *transa, char *diag, int *m, 230 | int *n, double *alpha, double *a, int *lda, double *b, 231 | int *ldb); 232 | 233 | int dtrmv_(char *uplo, char *trans, char *diag, int *n, double *a, 234 | int *lda, double *x, int *incx); 235 | 236 | int dtrsm_(char *side, char *uplo, char *transa, char *diag, int *m, 237 | int *n, double *alpha, double *a, int *lda, double *b, 238 | int *ldb); 239 | 240 | int dtrsv_(char *uplo, char *trans, char *diag, int *n, double *a, 241 | int *lda, double *x, int *incx); 242 | 243 | 244 | int saxpy_(int *n, float *sa, float *sx, int *incx, float *sy, int *incy); 245 | 246 | int scopy_(int *n, float *sx, int *incx, float *sy, int *incy); 247 | 248 | int sgbmv_(char *trans, int *m, int *n, int *kl, int *ku, 249 | float *alpha, float *a, int *lda, float *x, int *incx, 250 | float *beta, float *y, int *incy); 251 | 252 | int sgemm_(char *transa, char *transb, int *m, int *n, int *k, 253 | float *alpha, float *a, int *lda, float *b, int *ldb, 254 | float *beta, float *c, int *ldc); 255 | 256 | int sgemv_(char *trans, int *m, int *n, float *alpha, float *a, 257 | int *lda, float *x, int *incx, float *beta, float *y, 258 | int *incy); 259 | 260 | int sger_(int *m, int *n, float *alpha, float *x, int *incx, 261 | float *y, int *incy, float *a, int *lda); 262 | 263 | int srot_(int *n, float *sx, int *incx, float *sy, int *incy, 264 | float *c, float *s); 265 | 266 | int srotg_(float *sa, float *sb, float *c, float *s); 267 | 268 | int ssbmv_(char *uplo, int *n, int *k, float *alpha, float *a, 269 | int *lda, float *x, int *incx, float *beta, float *y, 270 | int *incy); 271 | 272 | int sscal_(int *n, float *sa, float *sx, int *incx); 273 | 274 | int sspmv_(char *uplo, int *n, float *alpha, float *ap, float *x, 275 | int *incx, float *beta, float *y, int *incy); 276 | 277 | int sspr_(char *uplo, int *n, float *alpha, float *x, int *incx, 278 | float *ap); 279 | 280 | int sspr2_(char *uplo, int *n, float *alpha, float *x, int *incx, 281 | float *y, int *incy, float *ap); 282 | 283 | int sswap_(int *n, float *sx, int *incx, float *sy, int *incy); 284 | 285 | int ssymm_(char *side, char *uplo, int *m, int *n, float *alpha, 286 | float *a, int *lda, float *b, int *ldb, float *beta, 287 | float *c, int *ldc); 288 | 289 | int ssymv_(char *uplo, int *n, float *alpha, float *a, int *lda, 290 | float *x, int *incx, float *beta, float *y, int *incy); 291 | 292 | int ssyr_(char *uplo, int *n, float *alpha, float *x, int *incx, 293 | float *a, int *lda); 294 | 295 | int ssyr2_(char *uplo, int *n, float *alpha, float *x, int *incx, 296 | float *y, int *incy, float *a, int *lda); 297 | 298 | int ssyr2k_(char *uplo, char *trans, int *n, int *k, float *alpha, 299 | float *a, int *lda, float *b, int *ldb, float *beta, 300 | float *c, int *ldc); 301 | 302 | int ssyrk_(char *uplo, char *trans, int *n, int *k, float *alpha, 303 | float *a, int *lda, float *beta, float *c, int *ldc); 304 | 305 | int stbmv_(char *uplo, char *trans, char *diag, int *n, int *k, 306 | float *a, int *lda, float *x, int *incx); 307 | 308 | int stbsv_(char *uplo, char *trans, char *diag, int *n, int *k, 309 | float *a, int *lda, float *x, int *incx); 310 | 311 | int stpmv_(char *uplo, char *trans, char *diag, int *n, float *ap, 312 | float *x, int *incx); 313 | 314 | int stpsv_(char *uplo, char *trans, char *diag, int *n, float *ap, 315 | float *x, int *incx); 316 | 317 | int strmm_(char *side, char *uplo, char *transa, char *diag, int *m, 318 | int *n, float *alpha, float *a, int *lda, float *b, 319 | int *ldb); 320 | 321 | int strmv_(char *uplo, char *trans, char *diag, int *n, float *a, 322 | int *lda, float *x, int *incx); 323 | 324 | int strsm_(char *side, char *uplo, char *transa, char *diag, int *m, 325 | int *n, float *alpha, float *a, int *lda, float *b, 326 | int *ldb); 327 | 328 | int strsv_(char *uplo, char *trans, char *diag, int *n, float *a, 329 | int *lda, float *x, int *incx); 330 | 331 | int zaxpy_(int *n, dcomplex *ca, dcomplex *cx, int *incx, dcomplex *cy, 332 | int *incy); 333 | 334 | int zcopy_(int *n, dcomplex *cx, int *incx, dcomplex *cy, int *incy); 335 | 336 | int zdscal_(int *n, double *sa, dcomplex *cx, int *incx); 337 | 338 | int zgbmv_(char *trans, int *m, int *n, int *kl, int *ku, 339 | dcomplex *alpha, dcomplex *a, int *lda, dcomplex *x, int *incx, 340 | dcomplex *beta, dcomplex *y, int *incy); 341 | 342 | int zgemm_(char *transa, char *transb, int *m, int *n, int *k, 343 | dcomplex *alpha, dcomplex *a, int *lda, dcomplex *b, int *ldb, 344 | dcomplex *beta, dcomplex *c, int *ldc); 345 | 346 | int zgemv_(char *trans, int *m, int *n, dcomplex *alpha, dcomplex *a, 347 | int *lda, dcomplex *x, int *incx, dcomplex *beta, dcomplex *y, 348 | int *incy); 349 | 350 | int zgerc_(int *m, int *n, dcomplex *alpha, dcomplex *x, int *incx, 351 | dcomplex *y, int *incy, dcomplex *a, int *lda); 352 | 353 | int zgeru_(int *m, int *n, dcomplex *alpha, dcomplex *x, int *incx, 354 | dcomplex *y, int *incy, dcomplex *a, int *lda); 355 | 356 | int zhbmv_(char *uplo, int *n, int *k, dcomplex *alpha, dcomplex *a, 357 | int *lda, dcomplex *x, int *incx, dcomplex *beta, dcomplex *y, 358 | int *incy); 359 | 360 | int zhemm_(char *side, char *uplo, int *m, int *n, dcomplex *alpha, 361 | dcomplex *a, int *lda, dcomplex *b, int *ldb, dcomplex *beta, 362 | dcomplex *c, int *ldc); 363 | 364 | int zhemv_(char *uplo, int *n, dcomplex *alpha, dcomplex *a, int *lda, 365 | dcomplex *x, int *incx, dcomplex *beta, dcomplex *y, int *incy); 366 | 367 | int zher_(char *uplo, int *n, double *alpha, dcomplex *x, int *incx, 368 | dcomplex *a, int *lda); 369 | 370 | int zher2_(char *uplo, int *n, dcomplex *alpha, dcomplex *x, int *incx, 371 | dcomplex *y, int *incy, dcomplex *a, int *lda); 372 | 373 | int zher2k_(char *uplo, char *trans, int *n, int *k, dcomplex *alpha, 374 | dcomplex *a, int *lda, dcomplex *b, int *ldb, double *beta, 375 | dcomplex *c, int *ldc); 376 | 377 | int zherk_(char *uplo, char *trans, int *n, int *k, double *alpha, 378 | dcomplex *a, int *lda, double *beta, dcomplex *c, int *ldc); 379 | 380 | int zhpmv_(char *uplo, int *n, dcomplex *alpha, dcomplex *ap, dcomplex *x, 381 | int *incx, dcomplex *beta, dcomplex *y, int *incy); 382 | 383 | int zhpr_(char *uplo, int *n, double *alpha, dcomplex *x, int *incx, 384 | dcomplex *ap); 385 | 386 | int zhpr2_(char *uplo, int *n, dcomplex *alpha, dcomplex *x, int *incx, 387 | dcomplex *y, int *incy, dcomplex *ap); 388 | 389 | int zrotg_(dcomplex *ca, dcomplex *cb, double *c, dcomplex *s); 390 | 391 | int zscal_(int *n, dcomplex *ca, dcomplex *cx, int *incx); 392 | 393 | int zswap_(int *n, dcomplex *cx, int *incx, dcomplex *cy, int *incy); 394 | 395 | int zsymm_(char *side, char *uplo, int *m, int *n, dcomplex *alpha, 396 | dcomplex *a, int *lda, dcomplex *b, int *ldb, dcomplex *beta, 397 | dcomplex *c, int *ldc); 398 | 399 | int zsyr2k_(char *uplo, char *trans, int *n, int *k, dcomplex *alpha, 400 | dcomplex *a, int *lda, dcomplex *b, int *ldb, dcomplex *beta, 401 | dcomplex *c, int *ldc); 402 | 403 | int zsyrk_(char *uplo, char *trans, int *n, int *k, dcomplex *alpha, 404 | dcomplex *a, int *lda, dcomplex *beta, dcomplex *c, int *ldc); 405 | 406 | int ztbmv_(char *uplo, char *trans, char *diag, int *n, int *k, 407 | dcomplex *a, int *lda, dcomplex *x, int *incx); 408 | 409 | int ztbsv_(char *uplo, char *trans, char *diag, int *n, int *k, 410 | dcomplex *a, int *lda, dcomplex *x, int *incx); 411 | 412 | int ztpmv_(char *uplo, char *trans, char *diag, int *n, dcomplex *ap, 413 | dcomplex *x, int *incx); 414 | 415 | int ztpsv_(char *uplo, char *trans, char *diag, int *n, dcomplex *ap, 416 | dcomplex *x, int *incx); 417 | 418 | int ztrmm_(char *side, char *uplo, char *transa, char *diag, int *m, 419 | int *n, dcomplex *alpha, dcomplex *a, int *lda, dcomplex *b, 420 | int *ldb); 421 | 422 | int ztrmv_(char *uplo, char *trans, char *diag, int *n, dcomplex *a, 423 | int *lda, dcomplex *x, int *incx); 424 | 425 | int ztrsm_(char *side, char *uplo, char *transa, char *diag, int *m, 426 | int *n, dcomplex *alpha, dcomplex *a, int *lda, dcomplex *b, 427 | int *ldb); 428 | 429 | int ztrsv_(char *uplo, char *trans, char *diag, int *n, dcomplex *a, 430 | int *lda, dcomplex *x, int *incx); 431 | -------------------------------------------------------------------------------- /liblinear-1.8/blas/blasp.h: -------------------------------------------------------------------------------- 1 | /* blasp.h -- C prototypes for BLAS Ver 1.0 */ 2 | /* Jesse Bennett March 23, 2000 */ 3 | 4 | /* Functions listed in alphabetical order */ 5 | 6 | #ifdef F2C_COMPAT 7 | 8 | void cdotc_(fcomplex *dotval, int *n, fcomplex *cx, int *incx, 9 | fcomplex *cy, int *incy); 10 | 11 | void cdotu_(fcomplex *dotval, int *n, fcomplex *cx, int *incx, 12 | fcomplex *cy, int *incy); 13 | 14 | double sasum_(int *n, float *sx, int *incx); 15 | 16 | double scasum_(int *n, fcomplex *cx, int *incx); 17 | 18 | double scnrm2_(int *n, fcomplex *x, int *incx); 19 | 20 | double sdot_(int *n, float *sx, int *incx, float *sy, int *incy); 21 | 22 | double snrm2_(int *n, float *x, int *incx); 23 | 24 | void zdotc_(dcomplex *dotval, int *n, dcomplex *cx, int *incx, 25 | dcomplex *cy, int *incy); 26 | 27 | void zdotu_(dcomplex *dotval, int *n, dcomplex *cx, int *incx, 28 | dcomplex *cy, int *incy); 29 | 30 | #else 31 | 32 | fcomplex cdotc_(int *n, fcomplex *cx, int *incx, fcomplex *cy, int *incy); 33 | 34 | fcomplex cdotu_(int *n, fcomplex *cx, int *incx, fcomplex *cy, int *incy); 35 | 36 | float sasum_(int *n, float *sx, int *incx); 37 | 38 | float scasum_(int *n, fcomplex *cx, int *incx); 39 | 40 | float scnrm2_(int *n, fcomplex *x, int *incx); 41 | 42 | float sdot_(int *n, float *sx, int *incx, float *sy, int *incy); 43 | 44 | float snrm2_(int *n, float *x, int *incx); 45 | 46 | dcomplex zdotc_(int *n, dcomplex *cx, int *incx, dcomplex *cy, int *incy); 47 | 48 | dcomplex zdotu_(int *n, dcomplex *cx, int *incx, dcomplex *cy, int *incy); 49 | 50 | #endif 51 | 52 | /* Remaining functions listed in alphabetical order */ 53 | 54 | int caxpy_(int *n, fcomplex *ca, fcomplex *cx, int *incx, fcomplex *cy, 55 | int *incy); 56 | 57 | int ccopy_(int *n, fcomplex *cx, int *incx, fcomplex *cy, int *incy); 58 | 59 | int cgbmv_(char *trans, int *m, int *n, int *kl, int *ku, 60 | fcomplex *alpha, fcomplex *a, int *lda, fcomplex *x, int *incx, 61 | fcomplex *beta, fcomplex *y, int *incy); 62 | 63 | int cgemm_(char *transa, char *transb, int *m, int *n, int *k, 64 | fcomplex *alpha, fcomplex *a, int *lda, fcomplex *b, int *ldb, 65 | fcomplex *beta, fcomplex *c, int *ldc); 66 | 67 | int cgemv_(char *trans, int *m, int *n, fcomplex *alpha, fcomplex *a, 68 | int *lda, fcomplex *x, int *incx, fcomplex *beta, fcomplex *y, 69 | int *incy); 70 | 71 | int cgerc_(int *m, int *n, fcomplex *alpha, fcomplex *x, int *incx, 72 | fcomplex *y, int *incy, fcomplex *a, int *lda); 73 | 74 | int cgeru_(int *m, int *n, fcomplex *alpha, fcomplex *x, int *incx, 75 | fcomplex *y, int *incy, fcomplex *a, int *lda); 76 | 77 | int chbmv_(char *uplo, int *n, int *k, fcomplex *alpha, fcomplex *a, 78 | int *lda, fcomplex *x, int *incx, fcomplex *beta, fcomplex *y, 79 | int *incy); 80 | 81 | int chemm_(char *side, char *uplo, int *m, int *n, fcomplex *alpha, 82 | fcomplex *a, int *lda, fcomplex *b, int *ldb, fcomplex *beta, 83 | fcomplex *c, int *ldc); 84 | 85 | int chemv_(char *uplo, int *n, fcomplex *alpha, fcomplex *a, int *lda, 86 | fcomplex *x, int *incx, fcomplex *beta, fcomplex *y, int *incy); 87 | 88 | int cher_(char *uplo, int *n, float *alpha, fcomplex *x, int *incx, 89 | fcomplex *a, int *lda); 90 | 91 | int cher2_(char *uplo, int *n, fcomplex *alpha, fcomplex *x, int *incx, 92 | fcomplex *y, int *incy, fcomplex *a, int *lda); 93 | 94 | int cher2k_(char *uplo, char *trans, int *n, int *k, fcomplex *alpha, 95 | fcomplex *a, int *lda, fcomplex *b, int *ldb, float *beta, 96 | fcomplex *c, int *ldc); 97 | 98 | int cherk_(char *uplo, char *trans, int *n, int *k, float *alpha, 99 | fcomplex *a, int *lda, float *beta, fcomplex *c, int *ldc); 100 | 101 | int chpmv_(char *uplo, int *n, fcomplex *alpha, fcomplex *ap, fcomplex *x, 102 | int *incx, fcomplex *beta, fcomplex *y, int *incy); 103 | 104 | int chpr_(char *uplo, int *n, float *alpha, fcomplex *x, int *incx, 105 | fcomplex *ap); 106 | 107 | int chpr2_(char *uplo, int *n, fcomplex *alpha, fcomplex *x, int *incx, 108 | fcomplex *y, int *incy, fcomplex *ap); 109 | 110 | int crotg_(fcomplex *ca, fcomplex *cb, float *c, fcomplex *s); 111 | 112 | int cscal_(int *n, fcomplex *ca, fcomplex *cx, int *incx); 113 | 114 | int csscal_(int *n, float *sa, fcomplex *cx, int *incx); 115 | 116 | int cswap_(int *n, fcomplex *cx, int *incx, fcomplex *cy, int *incy); 117 | 118 | int csymm_(char *side, char *uplo, int *m, int *n, fcomplex *alpha, 119 | fcomplex *a, int *lda, fcomplex *b, int *ldb, fcomplex *beta, 120 | fcomplex *c, int *ldc); 121 | 122 | int csyr2k_(char *uplo, char *trans, int *n, int *k, fcomplex *alpha, 123 | fcomplex *a, int *lda, fcomplex *b, int *ldb, fcomplex *beta, 124 | fcomplex *c, int *ldc); 125 | 126 | int csyrk_(char *uplo, char *trans, int *n, int *k, fcomplex *alpha, 127 | fcomplex *a, int *lda, fcomplex *beta, fcomplex *c, int *ldc); 128 | 129 | int ctbmv_(char *uplo, char *trans, char *diag, int *n, int *k, 130 | fcomplex *a, int *lda, fcomplex *x, int *incx); 131 | 132 | int ctbsv_(char *uplo, char *trans, char *diag, int *n, int *k, 133 | fcomplex *a, int *lda, fcomplex *x, int *incx); 134 | 135 | int ctpmv_(char *uplo, char *trans, char *diag, int *n, fcomplex *ap, 136 | fcomplex *x, int *incx); 137 | 138 | int ctpsv_(char *uplo, char *trans, char *diag, int *n, fcomplex *ap, 139 | fcomplex *x, int *incx); 140 | 141 | int ctrmm_(char *side, char *uplo, char *transa, char *diag, int *m, 142 | int *n, fcomplex *alpha, fcomplex *a, int *lda, fcomplex *b, 143 | int *ldb); 144 | 145 | int ctrmv_(char *uplo, char *trans, char *diag, int *n, fcomplex *a, 146 | int *lda, fcomplex *x, int *incx); 147 | 148 | int ctrsm_(char *side, char *uplo, char *transa, char *diag, int *m, 149 | int *n, fcomplex *alpha, fcomplex *a, int *lda, fcomplex *b, 150 | int *ldb); 151 | 152 | int ctrsv_(char *uplo, char *trans, char *diag, int *n, fcomplex *a, 153 | int *lda, fcomplex *x, int *incx); 154 | 155 | int daxpy_(int *n, double *sa, double *sx, int *incx, double *sy, 156 | int *incy); 157 | 158 | int dcopy_(int *n, double *sx, int *incx, double *sy, int *incy); 159 | 160 | int dgbmv_(char *trans, int *m, int *n, int *kl, int *ku, 161 | double *alpha, double *a, int *lda, double *x, int *incx, 162 | double *beta, double *y, int *incy); 163 | 164 | int dgemm_(char *transa, char *transb, int *m, int *n, int *k, 165 | double *alpha, double *a, int *lda, double *b, int *ldb, 166 | double *beta, double *c, int *ldc); 167 | 168 | int dgemv_(char *trans, int *m, int *n, double *alpha, double *a, 169 | int *lda, double *x, int *incx, double *beta, double *y, 170 | int *incy); 171 | 172 | int dger_(int *m, int *n, double *alpha, double *x, int *incx, 173 | double *y, int *incy, double *a, int *lda); 174 | 175 | int drot_(int *n, double *sx, int *incx, double *sy, int *incy, 176 | double *c, double *s); 177 | 178 | int drotg_(double *sa, double *sb, double *c, double *s); 179 | 180 | int dsbmv_(char *uplo, int *n, int *k, double *alpha, double *a, 181 | int *lda, double *x, int *incx, double *beta, double *y, 182 | int *incy); 183 | 184 | int dscal_(int *n, double *sa, double *sx, int *incx); 185 | 186 | int dspmv_(char *uplo, int *n, double *alpha, double *ap, double *x, 187 | int *incx, double *beta, double *y, int *incy); 188 | 189 | int dspr_(char *uplo, int *n, double *alpha, double *x, int *incx, 190 | double *ap); 191 | 192 | int dspr2_(char *uplo, int *n, double *alpha, double *x, int *incx, 193 | double *y, int *incy, double *ap); 194 | 195 | int dswap_(int *n, double *sx, int *incx, double *sy, int *incy); 196 | 197 | int dsymm_(char *side, char *uplo, int *m, int *n, double *alpha, 198 | double *a, int *lda, double *b, int *ldb, double *beta, 199 | double *c, int *ldc); 200 | 201 | int dsymv_(char *uplo, int *n, double *alpha, double *a, int *lda, 202 | double *x, int *incx, double *beta, double *y, int *incy); 203 | 204 | int dsyr_(char *uplo, int *n, double *alpha, double *x, int *incx, 205 | double *a, int *lda); 206 | 207 | int dsyr2_(char *uplo, int *n, double *alpha, double *x, int *incx, 208 | double *y, int *incy, double *a, int *lda); 209 | 210 | int dsyr2k_(char *uplo, char *trans, int *n, int *k, double *alpha, 211 | double *a, int *lda, double *b, int *ldb, double *beta, 212 | double *c, int *ldc); 213 | 214 | int dsyrk_(char *uplo, char *trans, int *n, int *k, double *alpha, 215 | double *a, int *lda, double *beta, double *c, int *ldc); 216 | 217 | int dtbmv_(char *uplo, char *trans, char *diag, int *n, int *k, 218 | double *a, int *lda, double *x, int *incx); 219 | 220 | int dtbsv_(char *uplo, char *trans, char *diag, int *n, int *k, 221 | double *a, int *lda, double *x, int *incx); 222 | 223 | int dtpmv_(char *uplo, char *trans, char *diag, int *n, double *ap, 224 | double *x, int *incx); 225 | 226 | int dtpsv_(char *uplo, char *trans, char *diag, int *n, double *ap, 227 | double *x, int *incx); 228 | 229 | int dtrmm_(char *side, char *uplo, char *transa, char *diag, int *m, 230 | int *n, double *alpha, double *a, int *lda, double *b, 231 | int *ldb); 232 | 233 | int dtrmv_(char *uplo, char *trans, char *diag, int *n, double *a, 234 | int *lda, double *x, int *incx); 235 | 236 | int dtrsm_(char *side, char *uplo, char *transa, char *diag, int *m, 237 | int *n, double *alpha, double *a, int *lda, double *b, 238 | int *ldb); 239 | 240 | int dtrsv_(char *uplo, char *trans, char *diag, int *n, double *a, 241 | int *lda, double *x, int *incx); 242 | 243 | 244 | int saxpy_(int *n, float *sa, float *sx, int *incx, float *sy, int *incy); 245 | 246 | int scopy_(int *n, float *sx, int *incx, float *sy, int *incy); 247 | 248 | int sgbmv_(char *trans, int *m, int *n, int *kl, int *ku, 249 | float *alpha, float *a, int *lda, float *x, int *incx, 250 | float *beta, float *y, int *incy); 251 | 252 | int sgemm_(char *transa, char *transb, int *m, int *n, int *k, 253 | float *alpha, float *a, int *lda, float *b, int *ldb, 254 | float *beta, float *c, int *ldc); 255 | 256 | int sgemv_(char *trans, int *m, int *n, float *alpha, float *a, 257 | int *lda, float *x, int *incx, float *beta, float *y, 258 | int *incy); 259 | 260 | int sger_(int *m, int *n, float *alpha, float *x, int *incx, 261 | float *y, int *incy, float *a, int *lda); 262 | 263 | int srot_(int *n, float *sx, int *incx, float *sy, int *incy, 264 | float *c, float *s); 265 | 266 | int srotg_(float *sa, float *sb, float *c, float *s); 267 | 268 | int ssbmv_(char *uplo, int *n, int *k, float *alpha, float *a, 269 | int *lda, float *x, int *incx, float *beta, float *y, 270 | int *incy); 271 | 272 | int sscal_(int *n, float *sa, float *sx, int *incx); 273 | 274 | int sspmv_(char *uplo, int *n, float *alpha, float *ap, float *x, 275 | int *incx, float *beta, float *y, int *incy); 276 | 277 | int sspr_(char *uplo, int *n, float *alpha, float *x, int *incx, 278 | float *ap); 279 | 280 | int sspr2_(char *uplo, int *n, float *alpha, float *x, int *incx, 281 | float *y, int *incy, float *ap); 282 | 283 | int sswap_(int *n, float *sx, int *incx, float *sy, int *incy); 284 | 285 | int ssymm_(char *side, char *uplo, int *m, int *n, float *alpha, 286 | float *a, int *lda, float *b, int *ldb, float *beta, 287 | float *c, int *ldc); 288 | 289 | int ssymv_(char *uplo, int *n, float *alpha, float *a, int *lda, 290 | float *x, int *incx, float *beta, float *y, int *incy); 291 | 292 | int ssyr_(char *uplo, int *n, float *alpha, float *x, int *incx, 293 | float *a, int *lda); 294 | 295 | int ssyr2_(char *uplo, int *n, float *alpha, float *x, int *incx, 296 | float *y, int *incy, float *a, int *lda); 297 | 298 | int ssyr2k_(char *uplo, char *trans, int *n, int *k, float *alpha, 299 | float *a, int *lda, float *b, int *ldb, float *beta, 300 | float *c, int *ldc); 301 | 302 | int ssyrk_(char *uplo, char *trans, int *n, int *k, float *alpha, 303 | float *a, int *lda, float *beta, float *c, int *ldc); 304 | 305 | int stbmv_(char *uplo, char *trans, char *diag, int *n, int *k, 306 | float *a, int *lda, float *x, int *incx); 307 | 308 | int stbsv_(char *uplo, char *trans, char *diag, int *n, int *k, 309 | float *a, int *lda, float *x, int *incx); 310 | 311 | int stpmv_(char *uplo, char *trans, char *diag, int *n, float *ap, 312 | float *x, int *incx); 313 | 314 | int stpsv_(char *uplo, char *trans, char *diag, int *n, float *ap, 315 | float *x, int *incx); 316 | 317 | int strmm_(char *side, char *uplo, char *transa, char *diag, int *m, 318 | int *n, float *alpha, float *a, int *lda, float *b, 319 | int *ldb); 320 | 321 | int strmv_(char *uplo, char *trans, char *diag, int *n, float *a, 322 | int *lda, float *x, int *incx); 323 | 324 | int strsm_(char *side, char *uplo, char *transa, char *diag, int *m, 325 | int *n, float *alpha, float *a, int *lda, float *b, 326 | int *ldb); 327 | 328 | int strsv_(char *uplo, char *trans, char *diag, int *n, float *a, 329 | int *lda, float *x, int *incx); 330 | 331 | int zaxpy_(int *n, dcomplex *ca, dcomplex *cx, int *incx, dcomplex *cy, 332 | int *incy); 333 | 334 | int zcopy_(int *n, dcomplex *cx, int *incx, dcomplex *cy, int *incy); 335 | 336 | int zdscal_(int *n, double *sa, dcomplex *cx, int *incx); 337 | 338 | int zgbmv_(char *trans, int *m, int *n, int *kl, int *ku, 339 | dcomplex *alpha, dcomplex *a, int *lda, dcomplex *x, int *incx, 340 | dcomplex *beta, dcomplex *y, int *incy); 341 | 342 | int zgemm_(char *transa, char *transb, int *m, int *n, int *k, 343 | dcomplex *alpha, dcomplex *a, int *lda, dcomplex *b, int *ldb, 344 | dcomplex *beta, dcomplex *c, int *ldc); 345 | 346 | int zgemv_(char *trans, int *m, int *n, dcomplex *alpha, dcomplex *a, 347 | int *lda, dcomplex *x, int *incx, dcomplex *beta, dcomplex *y, 348 | int *incy); 349 | 350 | int zgerc_(int *m, int *n, dcomplex *alpha, dcomplex *x, int *incx, 351 | dcomplex *y, int *incy, dcomplex *a, int *lda); 352 | 353 | int zgeru_(int *m, int *n, dcomplex *alpha, dcomplex *x, int *incx, 354 | dcomplex *y, int *incy, dcomplex *a, int *lda); 355 | 356 | int zhbmv_(char *uplo, int *n, int *k, dcomplex *alpha, dcomplex *a, 357 | int *lda, dcomplex *x, int *incx, dcomplex *beta, dcomplex *y, 358 | int *incy); 359 | 360 | int zhemm_(char *side, char *uplo, int *m, int *n, dcomplex *alpha, 361 | dcomplex *a, int *lda, dcomplex *b, int *ldb, dcomplex *beta, 362 | dcomplex *c, int *ldc); 363 | 364 | int zhemv_(char *uplo, int *n, dcomplex *alpha, dcomplex *a, int *lda, 365 | dcomplex *x, int *incx, dcomplex *beta, dcomplex *y, int *incy); 366 | 367 | int zher_(char *uplo, int *n, double *alpha, dcomplex *x, int *incx, 368 | dcomplex *a, int *lda); 369 | 370 | int zher2_(char *uplo, int *n, dcomplex *alpha, dcomplex *x, int *incx, 371 | dcomplex *y, int *incy, dcomplex *a, int *lda); 372 | 373 | int zher2k_(char *uplo, char *trans, int *n, int *k, dcomplex *alpha, 374 | dcomplex *a, int *lda, dcomplex *b, int *ldb, double *beta, 375 | dcomplex *c, int *ldc); 376 | 377 | int zherk_(char *uplo, char *trans, int *n, int *k, double *alpha, 378 | dcomplex *a, int *lda, double *beta, dcomplex *c, int *ldc); 379 | 380 | int zhpmv_(char *uplo, int *n, dcomplex *alpha, dcomplex *ap, dcomplex *x, 381 | int *incx, dcomplex *beta, dcomplex *y, int *incy); 382 | 383 | int zhpr_(char *uplo, int *n, double *alpha, dcomplex *x, int *incx, 384 | dcomplex *ap); 385 | 386 | int zhpr2_(char *uplo, int *n, dcomplex *alpha, dcomplex *x, int *incx, 387 | dcomplex *y, int *incy, dcomplex *ap); 388 | 389 | int zrotg_(dcomplex *ca, dcomplex *cb, double *c, dcomplex *s); 390 | 391 | int zscal_(int *n, dcomplex *ca, dcomplex *cx, int *incx); 392 | 393 | int zswap_(int *n, dcomplex *cx, int *incx, dcomplex *cy, int *incy); 394 | 395 | int zsymm_(char *side, char *uplo, int *m, int *n, dcomplex *alpha, 396 | dcomplex *a, int *lda, dcomplex *b, int *ldb, dcomplex *beta, 397 | dcomplex *c, int *ldc); 398 | 399 | int zsyr2k_(char *uplo, char *trans, int *n, int *k, dcomplex *alpha, 400 | dcomplex *a, int *lda, dcomplex *b, int *ldb, dcomplex *beta, 401 | dcomplex *c, int *ldc); 402 | 403 | int zsyrk_(char *uplo, char *trans, int *n, int *k, dcomplex *alpha, 404 | dcomplex *a, int *lda, dcomplex *beta, dcomplex *c, int *ldc); 405 | 406 | int ztbmv_(char *uplo, char *trans, char *diag, int *n, int *k, 407 | dcomplex *a, int *lda, dcomplex *x, int *incx); 408 | 409 | int ztbsv_(char *uplo, char *trans, char *diag, int *n, int *k, 410 | dcomplex *a, int *lda, dcomplex *x, int *incx); 411 | 412 | int ztpmv_(char *uplo, char *trans, char *diag, int *n, dcomplex *ap, 413 | dcomplex *x, int *incx); 414 | 415 | int ztpsv_(char *uplo, char *trans, char *diag, int *n, dcomplex *ap, 416 | dcomplex *x, int *incx); 417 | 418 | int ztrmm_(char *side, char *uplo, char *transa, char *diag, int *m, 419 | int *n, dcomplex *alpha, dcomplex *a, int *lda, dcomplex *b, 420 | int *ldb); 421 | 422 | int ztrmv_(char *uplo, char *trans, char *diag, int *n, dcomplex *a, 423 | int *lda, dcomplex *x, int *incx); 424 | 425 | int ztrsm_(char *side, char *uplo, char *transa, char *diag, int *m, 426 | int *n, dcomplex *alpha, dcomplex *a, int *lda, dcomplex *b, 427 | int *ldb); 428 | 429 | int ztrsv_(char *uplo, char *trans, char *diag, int *n, dcomplex *a, 430 | int *lda, dcomplex *x, int *incx); 431 | -------------------------------------------------------------------------------- /liblinear-1.8/README: -------------------------------------------------------------------------------- 1 | LIBLINEAR is a simple package for solving large-scale regularized 2 | linear classification. It currently supports L2-regularized logistic 3 | regression/L2-loss support vector classification/L1-loss support vector 4 | classification, and L1-regularized L2-loss support vector classification/ 5 | logistic regression. This document explains the usage of LIBLINEAR. 6 | 7 | To get started, please read the ``Quick Start'' section first. 8 | For developers, please check the ``Library Usage'' section to learn 9 | how to integrate LIBLINEAR in your software. 10 | 11 | Table of Contents 12 | ================= 13 | 14 | - When to use LIBLINEAR but not LIBSVM 15 | - Quick Start 16 | - Installation 17 | - `train' Usage 18 | - `predict' Usage 19 | - Examples 20 | - Library Usage 21 | - Building Windows Binaries 22 | - Additional Information 23 | - MATLAB/OCTAVE interface 24 | - PYTHON interface 25 | 26 | When to use LIBLINEAR but not LIBSVM 27 | ==================================== 28 | 29 | There are some large data for which with/without nonlinear mappings 30 | gives similar performances. Without using kernels, one can 31 | efficiently train a much larger set via a linear classifier. These 32 | data usually have a large number of features. Document classification 33 | is an example. 34 | 35 | Warning: While generally liblinear is very fast, its default solver 36 | may be slow under certain situations (e.g., data not scaled or C is 37 | large). See Appendix B of our SVM guide about how to handle such 38 | cases. 39 | http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf 40 | 41 | Warning: If you are a beginner and your data sets are not large, you 42 | should consider LIBSVM first. 43 | 44 | LIBSVM page: 45 | http://www.csie.ntu.edu.tw/~cjlin/libsvm 46 | 47 | 48 | Quick Start 49 | =========== 50 | 51 | See the section ``Installation'' for installing LIBLINEAR. 52 | 53 | After installation, there are programs `train' and `predict' for 54 | training and testing, respectively. 55 | 56 | About the data format, please check the README file of LIBSVM. Note 57 | that feature index must start from 1 (but not 0). 58 | 59 | A sample classification data included in this package is `heart_scale'. 60 | 61 | Type `train heart_scale', and the program will read the training 62 | data and output the model file `heart_scale.model'. If you have a test 63 | set called heart_scale.t, then type `predict heart_scale.t 64 | heart_scale.model output' to see the prediction accuracy. The `output' 65 | file contains the predicted class labels. 66 | 67 | For more information about `train' and `predict', see the sections 68 | `train' Usage and `predict' Usage. 69 | 70 | To obtain good performances, sometimes one needs to scale the 71 | data. Please check the program `svm-scale' of LIBSVM. For large and 72 | sparse data, use `-l 0' to keep the sparsity. 73 | 74 | Installation 75 | ============ 76 | 77 | On Unix systems, type `make' to build the `train' and `predict' 78 | programs. Run them without arguments to show the usages. 79 | 80 | On other systems, consult `Makefile' to build them (e.g., see 81 | 'Building Windows binaries' in this file) or use the pre-built 82 | binaries (Windows binaries are in the directory `windows'). 83 | 84 | This software uses some level-1 BLAS subroutines. The needed functions are 85 | included in this package. If a BLAS library is available on your 86 | machine, you may use it by modifying the Makefile: Unmark the following line 87 | 88 | #LIBS ?= -lblas 89 | 90 | and mark 91 | 92 | LIBS ?= blas/blas.a 93 | 94 | `train' Usage 95 | ============= 96 | 97 | Usage: train [options] training_set_file [model_file] 98 | options: 99 | -s type : set type of solver (default 1) 100 | 0 -- L2-regularized logistic regression (primal) 101 | 1 -- L2-regularized L2-loss support vector classification (dual) 102 | 2 -- L2-regularized L2-loss support vector classification (primal) 103 | 3 -- L2-regularized L1-loss support vector classification (dual) 104 | 4 -- multi-class support vector classification by Crammer and Singer 105 | 5 -- L1-regularized L2-loss support vector classification 106 | 6 -- L1-regularized logistic regression 107 | 7 -- L2-regularized logistic regression (dual) 108 | -c cost : set the parameter C (default 1) 109 | -e epsilon : set tolerance of termination criterion 110 | -s 0 and 2 111 | |f'(w)|_2 <= eps*min(pos,neg)/l*|f'(w0)|_2, 112 | where f is the primal function and pos/neg are # of 113 | positive/negative data (default 0.01) 114 | -s 1, 3, 4 and 7 115 | Dual maximal violation <= eps; similar to libsvm (default 0.1) 116 | -s 5 and 6 117 | |f'(w)|_inf <= eps*min(pos,neg)/l*|f'(w0)|_inf, 118 | where f is the primal function (default 0.01) 119 | -B bias : if bias >= 0, instance x becomes [x; bias]; if < 0, no bias term added (default -1) 120 | -wi weight: weights adjust the parameter C of different classes (see README for details) 121 | -v n: n-fold cross validation mode 122 | -q : quiet mode (no outputs) 123 | 124 | Option -v randomly splits the data into n parts and calculates cross 125 | validation accuracy on them. 126 | 127 | Formulations: 128 | 129 | For L2-regularized logistic regression (-s 0), we solve 130 | 131 | min_w w^Tw/2 + C \sum log(1 + exp(-y_i w^Tx_i)) 132 | 133 | For L2-regularized L2-loss SVC dual (-s 1), we solve 134 | 135 | min_alpha 0.5(alpha^T (Q + I/2/C) alpha) - e^T alpha 136 | s.t. 0 <= alpha_i, 137 | 138 | For L2-regularized L2-loss SVC (-s 2), we solve 139 | 140 | min_w w^Tw/2 + C \sum max(0, 1- y_i w^Tx_i)^2 141 | 142 | For L2-regularized L1-loss SVC dual (-s 3), we solve 143 | 144 | min_alpha 0.5(alpha^T Q alpha) - e^T alpha 145 | s.t. 0 <= alpha_i <= C, 146 | 147 | For L1-regularized L2-loss SVC (-s 5), we solve 148 | 149 | min_w \sum |w_j| + C \sum max(0, 1- y_i w^Tx_i)^2 150 | 151 | For L1-regularized logistic regression (-s 6), we solve 152 | 153 | min_w \sum |w_j| + C \sum log(1 + exp(-y_i w^Tx_i)) 154 | 155 | where 156 | 157 | Q is a matrix with Q_ij = y_i y_j x_i^T x_j. 158 | 159 | For L2-regularized logistic regression (-s 7), we solve 160 | 161 | min_alpha 0.5(alpha^T Q alpha) + \sum alpha_i*log(alpha_i) + \sum (C-alpha_i)*log(C-alpha_i) - a constant 162 | s.t. 0 <= alpha_i <= C, 163 | 164 | If bias >= 0, w becomes [w; w_{n+1}] and x becomes [x; bias]. 165 | 166 | The primal-dual relationship implies that -s 1 and -s 2 give the same 167 | model, and -s 0 and -s 7 give the same. 168 | 169 | We implement 1-vs-the rest multi-class strategy. In training i 170 | vs. non_i, their C parameters are (weight from -wi)*C and C, 171 | respectively. If there are only two classes, we train only one 172 | model. Thus weight1*C vs. weight2*C is used. See examples below. 173 | 174 | We also implement multi-class SVM by Crammer and Singer (-s 4): 175 | 176 | min_{w_m, \xi_i} 0.5 \sum_m ||w_m||^2 + C \sum_i \xi_i 177 | s.t. w^T_{y_i} x_i - w^T_m x_i >= \e^m_i - \xi_i \forall m,i 178 | 179 | where e^m_i = 0 if y_i = m, 180 | e^m_i = 1 if y_i != m, 181 | 182 | Here we solve the dual problem: 183 | 184 | min_{\alpha} 0.5 \sum_m ||w_m(\alpha)||^2 + \sum_i \sum_m e^m_i alpha^m_i 185 | s.t. \alpha^m_i <= C^m_i \forall m,i , \sum_m \alpha^m_i=0 \forall i 186 | 187 | where w_m(\alpha) = \sum_i \alpha^m_i x_i, 188 | and C^m_i = C if m = y_i, 189 | C^m_i = 0 if m != y_i. 190 | 191 | `predict' Usage 192 | =============== 193 | 194 | Usage: predict [options] test_file model_file output_file 195 | options: 196 | -b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0) 197 | 198 | Examples 199 | ======== 200 | 201 | > train data_file 202 | 203 | Train linear SVM with L2-loss function. 204 | 205 | > train -s 0 data_file 206 | 207 | Train a logistic regression model. 208 | 209 | > train -v 5 -e 0.001 data_file 210 | 211 | Do five-fold cross-validation using L2-loss svm. 212 | Use a smaller stopping tolerance 0.001 than the default 213 | 0.1 if you want more accurate solutions. 214 | 215 | > train -c 10 -w1 2 -w2 5 -w3 2 four_class_data_file 216 | 217 | Train four classifiers: 218 | positive negative Cp Cn 219 | class 1 class 2,3,4. 20 10 220 | class 2 class 1,3,4. 50 10 221 | class 3 class 1,2,4. 20 10 222 | class 4 class 1,2,3. 10 10 223 | 224 | > train -c 10 -w3 1 -w2 5 two_class_data_file 225 | 226 | If there are only two classes, we train ONE model. 227 | The C values for the two classes are 10 and 50. 228 | 229 | > predict -b 1 test_file data_file.model output_file 230 | 231 | Output probability estimates (for logistic regression only). 232 | 233 | Library Usage 234 | ============= 235 | 236 | - Function: model* train(const struct problem *prob, 237 | const struct parameter *param); 238 | 239 | This function constructs and returns a linear classification model 240 | according to the given training data and parameters. 241 | 242 | struct problem describes the problem: 243 | 244 | struct problem 245 | { 246 | int l, n; 247 | int *y; 248 | struct feature_node **x; 249 | double bias; 250 | }; 251 | 252 | where `l' is the number of training data. If bias >= 0, we assume 253 | that one additional feature is added to the end of each data 254 | instance. `n' is the number of feature (including the bias feature 255 | if bias >= 0). `y' is an array containing the target values. And 256 | `x' is an array of pointers, 257 | each of which points to a sparse representation (array of feature_node) of one 258 | training vector. 259 | 260 | For example, if we have the following training data: 261 | 262 | LABEL ATTR1 ATTR2 ATTR3 ATTR4 ATTR5 263 | ----- ----- ----- ----- ----- ----- 264 | 1 0 0.1 0.2 0 0 265 | 2 0 0.1 0.3 -1.2 0 266 | 1 0.4 0 0 0 0 267 | 2 0 0.1 0 1.4 0.5 268 | 3 -0.1 -0.2 0.1 1.1 0.1 269 | 270 | and bias = 1, then the components of problem are: 271 | 272 | l = 5 273 | n = 6 274 | 275 | y -> 1 2 1 2 3 276 | 277 | x -> [ ] -> (2,0.1) (3,0.2) (6,1) (-1,?) 278 | [ ] -> (2,0.1) (3,0.3) (4,-1.2) (6,1) (-1,?) 279 | [ ] -> (1,0.4) (6,1) (-1,?) 280 | [ ] -> (2,0.1) (4,1.4) (5,0.5) (6,1) (-1,?) 281 | [ ] -> (1,-0.1) (2,-0.2) (3,0.1) (4,1.1) (5,0.1) (6,1) (-1,?) 282 | 283 | struct parameter describes the parameters of a linear classification model: 284 | 285 | struct parameter 286 | { 287 | int solver_type; 288 | 289 | /* these are for training only */ 290 | double eps; /* stopping criteria */ 291 | double C; 292 | int nr_weight; 293 | int *weight_label; 294 | double* weight; 295 | }; 296 | 297 | solver_type can be one of L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, MCSVM_CS, L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL. 298 | 299 | L2R_LR L2-regularized logistic regression (primal) 300 | L2R_L2LOSS_SVC_DUAL L2-regularized L2-loss support vector classification (dual) 301 | L2R_L2LOSS_SVC L2-regularized L2-loss support vector classification (primal) 302 | L2R_L1LOSS_SVC_DUAL L2-regularized L1-loss support vector classification (dual) 303 | MCSVM_CS multi-class support vector classification by Crammer and Singer 304 | L1R_L2LOSS_SVC L1-regularized L2-loss support vector classification 305 | L1R_LR L1-regularized logistic regression 306 | L2R_LR_DUAL L2-regularized logistic regression (dual) 307 | 308 | C is the cost of constraints violation. 309 | eps is the stopping criterion. 310 | 311 | nr_weight, weight_label, and weight are used to change the penalty 312 | for some classes (If the weight for a class is not changed, it is 313 | set to 1). This is useful for training classifier using unbalanced 314 | input data or with asymmetric misclassification cost. 315 | 316 | nr_weight is the number of elements in the array weight_label and 317 | weight. Each weight[i] corresponds to weight_label[i], meaning that 318 | the penalty of class weight_label[i] is scaled by a factor of weight[i]. 319 | 320 | If you do not want to change penalty for any of the classes, 321 | just set nr_weight to 0. 322 | 323 | *NOTE* To avoid wrong parameters, check_parameter() should be 324 | called before train(). 325 | 326 | struct model stores the model obtained from the training procedure: 327 | 328 | struct model 329 | { 330 | struct parameter param; 331 | int nr_class; /* number of classes */ 332 | int nr_feature; 333 | double *w; 334 | int *label; /* label of each class */ 335 | double bias; 336 | }; 337 | 338 | param describes the parameters used to obtain the model. 339 | 340 | nr_class and nr_feature are the number of classes and features, respectively. 341 | 342 | The nr_feature*nr_class array w gives feature weights. We use one 343 | against the rest for multi-class classification, so each feature 344 | index corresponds to nr_class weight values. Weights are 345 | organized in the following way 346 | 347 | +------------------+------------------+------------+ 348 | | nr_class weights | nr_class weights | ... 349 | | for 1st feature | for 2nd feature | 350 | +------------------+------------------+------------+ 351 | 352 | If bias >= 0, x becomes [x; bias]. The number of features is 353 | increased by one, so w is a (nr_feature+1)*nr_class array. The 354 | value of bias is stored in the variable bias. 355 | 356 | The array label stores class labels. 357 | 358 | - Function: void cross_validation(const problem *prob, const parameter *param, int nr_fold, int *target); 359 | 360 | This function conducts cross validation. Data are separated to 361 | nr_fold folds. Under given parameters, sequentially each fold is 362 | validated using the model from training the remaining. Predicted 363 | labels in the validation process are stored in the array called 364 | target. 365 | 366 | The format of prob is same as that for train(). 367 | 368 | - Function: int predict(const model *model_, const feature_node *x); 369 | 370 | This functions classifies a test vector using the given 371 | model. The predicted label is returned. 372 | 373 | - Function: int predict_values(const struct model *model_, 374 | const struct feature_node *x, double* dec_values); 375 | 376 | This function gives nr_w decision values in the array 377 | dec_values. nr_w is 1 if there are two classes except multi-class 378 | svm by Crammer and Singer (-s 4), and is the number of classes otherwise. 379 | 380 | We implement one-vs-the rest multi-class strategy (-s 0,1,2,3) and 381 | multi-class svm by Crammer and Singer (-s 4) for multi-class SVM. 382 | The class with the highest decision value is returned. 383 | 384 | - Function: int predict_probability(const struct model *model_, 385 | const struct feature_node *x, double* prob_estimates); 386 | 387 | This function gives nr_class probability estimates in the array 388 | prob_estimates. nr_class can be obtained from the function 389 | get_nr_class. The class with the highest probability is 390 | returned. Currently, we support only the probability outputs of 391 | logistic regression. 392 | 393 | - Function: int get_nr_feature(const model *model_); 394 | 395 | The function gives the number of attributes of the model. 396 | 397 | - Function: int get_nr_class(const model *model_); 398 | 399 | The function gives the number of classes of the model. 400 | 401 | - Function: void get_labels(const model *model_, int* label); 402 | 403 | This function outputs the name of labels into an array called label. 404 | 405 | - Function: const char *check_parameter(const struct problem *prob, 406 | const struct parameter *param); 407 | 408 | This function checks whether the parameters are within the feasible 409 | range of the problem. This function should be called before calling 410 | train() and cross_validation(). It returns NULL if the 411 | parameters are feasible, otherwise an error message is returned. 412 | 413 | - Function: int save_model(const char *model_file_name, 414 | const struct model *model_); 415 | 416 | This function saves a model to a file; returns 0 on success, or -1 417 | if an error occurs. 418 | 419 | - Function: struct model *load_model(const char *model_file_name); 420 | 421 | This function returns a pointer to the model read from the file, 422 | or a null pointer if the model could not be loaded. 423 | 424 | - Function: void free_model_content(struct model *model_ptr); 425 | 426 | This function frees the memory used by the entries in a model structure. 427 | 428 | - Function: void free_and_destroy_model(struct model **model_ptr_ptr); 429 | 430 | This function frees the memory used by a model and destroys the model 431 | structure. 432 | 433 | - Function: void destroy_param(struct parameter *param); 434 | 435 | This function frees the memory used by a parameter set. 436 | 437 | - Function: void set_print_string_function(void (*print_func)(const char *)); 438 | 439 | Users can specify their output format by a function. Use 440 | set_print_string_function(NULL); 441 | for default printing to stdout. 442 | 443 | Building Windows Binaries 444 | ========================= 445 | 446 | Windows binaries are in the directory `windows'. To build them via 447 | Visual C++, use the following steps: 448 | 449 | 1. Open a dos command box and change to liblinear directory. If 450 | environment variables of VC++ have not been set, type 451 | 452 | "C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat" 453 | 454 | You may have to modify the above command according which version of 455 | VC++ or where it is installed. 456 | 457 | 2. Type 458 | 459 | nmake -f Makefile.win clean all 460 | 461 | 462 | MATLAB/OCTAVE Interface 463 | ======================= 464 | 465 | Please check the file README in the directory `matlab'. 466 | 467 | PYTHON Interface 468 | ================ 469 | 470 | Please check the file README in the directory `python'. 471 | 472 | Additional Information 473 | ====================== 474 | 475 | If you find LIBLINEAR helpful, please cite it as 476 | 477 | R.-E. Fan, K.-W. Chang, C.-J. Hsieh, X.-R. Wang, and C.-J. Lin. 478 | LIBLINEAR: A Library for Large Linear Classification, Journal of 479 | Machine Learning Research 9(2008), 1871-1874. Software available at 480 | http://www.csie.ntu.edu.tw/~cjlin/liblinear 481 | 482 | For any questions and comments, please send your email to 483 | cjlin@csie.ntu.edu.tw 484 | 485 | 486 | --------------------------------------------------------------------------------