liblinear-ruby 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -3
  3. data/ext/liblinear_wrap.cxx +99 -0
  4. data/ext/linear.cpp +61 -11
  5. data/ext/linear.h +3 -0
  6. data/lib/liblinear/model.rb +37 -6
  7. data/lib/liblinear/version.rb +1 -1
  8. data/{liblinear-1.94 → liblinear-1.95}/COPYRIGHT +1 -1
  9. data/{liblinear-1.94 → liblinear-1.95}/Makefile +1 -1
  10. data/{liblinear-1.94 → liblinear-1.95}/Makefile.win +1 -1
  11. data/{liblinear-1.94 → liblinear-1.95}/README +32 -1
  12. data/{liblinear-1.94 → liblinear-1.95}/blas/Makefile +0 -0
  13. data/{liblinear-1.94 → liblinear-1.95}/blas/blas.h +0 -0
  14. data/{liblinear-1.94 → liblinear-1.95}/blas/blasp.h +8 -0
  15. data/{liblinear-1.94 → liblinear-1.95}/blas/daxpy.c +8 -0
  16. data/{liblinear-1.94 → liblinear-1.95}/blas/ddot.c +8 -0
  17. data/{liblinear-1.94 → liblinear-1.95}/blas/dnrm2.c +8 -0
  18. data/{liblinear-1.94 → liblinear-1.95}/blas/dscal.c +8 -0
  19. data/{liblinear-1.94 → liblinear-1.95}/heart_scale +0 -0
  20. data/{liblinear-1.94 → liblinear-1.95}/linear.cpp +61 -11
  21. data/{liblinear-1.94 → liblinear-1.95}/linear.def +3 -0
  22. data/{liblinear-1.94 → liblinear-1.95}/linear.h +3 -0
  23. data/{liblinear-1.94 → liblinear-1.95}/matlab/Makefile +2 -11
  24. data/{liblinear-1.94 → liblinear-1.95}/matlab/README +0 -0
  25. data/{liblinear-1.94 → liblinear-1.95}/matlab/libsvmread.c +3 -4
  26. data/{liblinear-1.94 → liblinear-1.95}/matlab/libsvmwrite.c +6 -7
  27. data/{liblinear-1.94 → liblinear-1.95}/matlab/linear_model_matlab.c +1 -1
  28. data/{liblinear-1.94 → liblinear-1.95}/matlab/linear_model_matlab.h +0 -0
  29. data/{liblinear-1.94 → liblinear-1.95}/matlab/make.m +21 -21
  30. data/{liblinear-1.94 → liblinear-1.95}/matlab/predict.c +4 -6
  31. data/{liblinear-1.94 → liblinear-1.95}/matlab/train.c +27 -17
  32. data/{liblinear-1.94 → liblinear-1.95}/predict.c +1 -3
  33. data/{liblinear-1.94 → liblinear-1.95}/python/Makefile +0 -0
  34. data/{liblinear-1.94 → liblinear-1.95}/python/README +30 -0
  35. data/{liblinear-1.94 → liblinear-1.95}/python/liblinear.py +35 -8
  36. data/{liblinear-1.94 → liblinear-1.95}/python/liblinearutil.py +7 -1
  37. data/{liblinear-1.94 → liblinear-1.95}/train.c +1 -1
  38. data/{liblinear-1.94 → liblinear-1.95}/tron.cpp +0 -0
  39. data/{liblinear-1.94 → liblinear-1.95}/tron.h +0 -0
  40. data/{liblinear-1.94 → liblinear-1.95}/windows/liblinear.dll +0 -0
  41. data/liblinear-1.95/windows/libsvmread.mexw64 +0 -0
  42. data/liblinear-1.95/windows/libsvmwrite.mexw64 +0 -0
  43. data/{liblinear-1.94 → liblinear-1.95}/windows/predict.exe +0 -0
  44. data/liblinear-1.95/windows/predict.mexw64 +0 -0
  45. data/{liblinear-1.94 → liblinear-1.95}/windows/train.exe +0 -0
  46. data/liblinear-1.95/windows/train.mexw64 +0 -0
  47. data/spec/liblinear/model_spec.rb +50 -11
  48. metadata +43 -42
  49. data/liblinear-1.94/windows/libsvmread.mexw64 +0 -0
  50. data/liblinear-1.94/windows/libsvmwrite.mexw64 +0 -0
  51. data/liblinear-1.94/windows/predict.mexw64 +0 -0
  52. data/liblinear-1.94/windows/train.mexw64 +0 -0
@@ -158,9 +158,7 @@ void do_predict(FILE *input, FILE *output)
158
158
  sumpt += predict_label*target_label;
159
159
  ++total;
160
160
  }
161
- if(model_->param.solver_type==L2R_L2LOSS_SVR ||
162
- model_->param.solver_type==L2R_L1LOSS_SVR_DUAL ||
163
- model_->param.solver_type==L2R_L2LOSS_SVR_DUAL)
161
+ if(check_regression_model(model_))
164
162
  {
165
163
  info("Mean squared error = %g (regression)\n",error/total);
166
164
  info("Squared correlation coefficient = %g (regression)\n",
File without changes
@@ -205,6 +205,36 @@ LIBLINEAR shared library:
205
205
  >>> nr_class = model_.get_nr_class()
206
206
  >>> class_labels = model_.get_labels()
207
207
  >>> is_prob_model = model_.is_probability_model()
208
+ >>> is_regression_model = model_.is_regression_model()
209
+
210
+ The decision function is W*x + b, where
211
+ W is an nr_class-by-nr_feature matrix, and
212
+ b is a vector of size nr_class.
213
+ To access W_kj (i.e., coefficient for the k-th class and the j-th feature)
214
+ and b_k (i.e., bias for the k-th class), use the following functions.
215
+
216
+ >>> W_kj = model_.get_decfun_coef(feat_idx=j, label_idx=k)
217
+ >>> b_k = model_.get_decfun_bias(label_idx=k)
218
+
219
+ We also provide a function to extract w_k (i.e., the k-th row of W) and
220
+ b_k directly as follows.
221
+
222
+ >>> [w_k, b_k] = model_.get_decfun(label_idx=k)
223
+
224
+ Note that w_k is a Python list of length nr_feature, which means that
225
+ w_k[0] = W_k1.
226
+ For regression models, W is just a vector of length nr_feature. Either
227
+ set label_idx=0 or omit the label_idx parameter to access the coefficients.
228
+
229
+ >>> W_j = model_.get_decfun_coef(feat_idx=j)
230
+ >>> b = model_.get_decfun_bias()
231
+ >>> [W, b] = model_.get_decfun()
232
+
233
+ Note that in get_decfun_coef, get_decfun_bias, and get_decfun, feat_idx
234
+ starts from 1, while label_idx starts from 0. If label_idx is not in the
235
+ valid range (0 to nr_class-1), then a NaN will be returned; and if feat_idx
236
+ is not in the valid range (1 to nr_feature), then a zero value will be
237
+ returned. For regression models, label_idx is ignored.
208
238
 
209
239
  Utility Functions
210
240
  =================
@@ -5,12 +5,18 @@ from ctypes.util import find_library
5
5
  from os import path
6
6
  import sys
7
7
 
8
+ __all__ = ['liblinear', 'feature_node', 'gen_feature_nodearray', 'problem',
9
+ 'parameter', 'model', 'toPyModel', 'L2R_LR', 'L2R_L2LOSS_SVC_DUAL',
10
+ 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL', 'MCSVM_CS',
11
+ 'L1R_L2LOSS_SVC', 'L1R_LR', 'L2R_LR_DUAL', 'L2R_L2LOSS_SVR',
12
+ 'L2R_L2LOSS_SVR_DUAL', 'L2R_L1LOSS_SVR_DUAL', 'print_null']
13
+
8
14
  try:
9
15
  dirname = path.dirname(path.abspath(__file__))
10
16
  if sys.platform == 'win32':
11
17
  liblinear = CDLL(path.join(dirname, r'..\windows\liblinear.dll'))
12
18
  else:
13
- liblinear = CDLL(path.join(dirname, '../liblinear.so.1'))
19
+ liblinear = CDLL(path.join(dirname, '../liblinear.so.2'))
14
20
  except:
15
21
  # For unix the prefix 'lib' is not considered.
16
22
  if find_library('linear'):
@@ -20,13 +26,17 @@ except:
20
26
  else:
21
27
  raise Exception('LIBLINEAR library not found.')
22
28
 
23
- # Construct constants
24
- SOLVER_TYPE = ['L2R_LR', 'L2R_L2LOSS_SVC_DUAL', 'L2R_L2LOSS_SVC', 'L2R_L1LOSS_SVC_DUAL',\
25
- 'MCSVM_CS', 'L1R_L2LOSS_SVC', 'L1R_LR', 'L2R_LR_DUAL', \
26
- None, None, None, \
27
- 'L2R_L2LOSS_SVR', 'L2R_L2LOSS_SVR_DUAL', 'L2R_L1LOSS_SVR_DUAL']
28
- for i, s in enumerate(SOLVER_TYPE):
29
- if s is not None: exec("%s = %d" % (s , i))
29
+ L2R_LR = 0
30
+ L2R_L2LOSS_SVC_DUAL = 1
31
+ L2R_L2LOSS_SVC = 2
32
+ L2R_L1LOSS_SVC_DUAL = 3
33
+ MCSVM_CS = 4
34
+ L1R_L2LOSS_SVC = 5
35
+ L1R_LR = 6
36
+ L2R_LR_DUAL = 7
37
+ L2R_L2LOSS_SVR = 11
38
+ L2R_L2LOSS_SVR_DUAL = 12
39
+ L2R_L1LOSS_SVR_DUAL = 13
30
40
 
31
41
  PRINT_STRING_FUN = CFUNCTYPE(None, c_char_p)
32
42
  def print_null(s):
@@ -240,9 +250,23 @@ class model(Structure):
240
250
  liblinear.get_labels(self, labels)
241
251
  return labels[:nr_class]
242
252
 
253
+ def get_decfun_coef(self, feat_idx, label_idx=0):
254
+ return liblinear.get_decfun_coef(self, feat_idx, label_idx)
255
+
256
+ def get_decfun_bias(self, label_idx=0):
257
+ return liblinear.get_decfun_bias(self, label_idx)
258
+
259
+ def get_decfun(self, label_idx=0):
260
+ w = [liblinear.get_decfun_coef(self, feat_idx, label_idx) for feat_idx in range(1, self.nr_feature+1)]
261
+ b = liblinear.get_decfun_bias(self, label_idx)
262
+ return (w, b)
263
+
243
264
  def is_probability_model(self):
244
265
  return (liblinear.check_probability_model(self) == 1)
245
266
 
267
+ def is_regression_model(self):
268
+ return (liblinear.check_regression_model(self) == 1)
269
+
246
270
  def toPyModel(model_ptr):
247
271
  """
248
272
  toPyModel(model_ptr) -> model
@@ -268,10 +292,13 @@ fillprototype(liblinear.load_model, POINTER(model), [c_char_p])
268
292
  fillprototype(liblinear.get_nr_feature, c_int, [POINTER(model)])
269
293
  fillprototype(liblinear.get_nr_class, c_int, [POINTER(model)])
270
294
  fillprototype(liblinear.get_labels, None, [POINTER(model), POINTER(c_int)])
295
+ fillprototype(liblinear.get_decfun_coef, c_double, [POINTER(model), c_int, c_int])
296
+ fillprototype(liblinear.get_decfun_bias, c_double, [POINTER(model), c_int])
271
297
 
272
298
  fillprototype(liblinear.free_model_content, None, [POINTER(model)])
273
299
  fillprototype(liblinear.free_and_destroy_model, None, [POINTER(POINTER(model))])
274
300
  fillprototype(liblinear.destroy_param, None, [POINTER(parameter)])
275
301
  fillprototype(liblinear.check_parameter, c_char_p, [POINTER(problem), POINTER(parameter)])
276
302
  fillprototype(liblinear.check_probability_model, c_int, [POINTER(model)])
303
+ fillprototype(liblinear.check_regression_model, c_int, [POINTER(model)])
277
304
  fillprototype(liblinear.set_print_string_function, None, [CFUNCTYPE(None, c_char_p)])
@@ -3,6 +3,12 @@
3
3
  import os, sys
4
4
  sys.path = [os.path.dirname(os.path.abspath(__file__))] + sys.path
5
5
  from liblinear import *
6
+ from liblinear import __all__ as liblinear_all
7
+ from ctypes import c_double
8
+
9
+ __all__ = ['svm_read_problem', 'load_model', 'save_model', 'evaluations',
10
+ 'train', 'predict'] + liblinear_all
11
+
6
12
 
7
13
  def svm_read_problem(data_file_name):
8
14
  """
@@ -241,7 +247,7 @@ def predict(y, x, m, options=""):
241
247
  y = [0] * len(x)
242
248
  ACC, MSE, SCC = evaluations(y, pred_labels)
243
249
  l = len(y)
244
- if solver_type in [L2R_L2LOSS_SVR, L2R_L2LOSS_SVR_DUAL, L2R_L1LOSS_SVR_DUAL]:
250
+ if m.is_regression_model():
245
251
  info("Mean squared error = %g (regression)" % MSE)
246
252
  info("Squared correlation coefficient = %g (regression)" % SCC)
247
253
  else:
@@ -300,7 +300,7 @@ void parse_command_line(int argc, char **argv, char *input_file_name, char *mode
300
300
  void read_problem(const char *filename)
301
301
  {
302
302
  int max_index, inst_max_index, i;
303
- long int elements, j;
303
+ size_t elements, j;
304
304
  FILE *fp = fopen(filename,"r");
305
305
  char *endptr;
306
306
  char *idx, *val, *label;
File without changes
File without changes
Binary file
@@ -4,8 +4,11 @@ require 'liblinear'
4
4
  describe Liblinear::Model do
5
5
  before do
6
6
  @prob = Liblinear::Problem.new([1, 2], [[1],[2]])
7
- @param = Liblinear::Parameter.new
8
- @model = Liblinear::Model.new(@prob, @param)
7
+ @param_1 = Liblinear::Parameter.new
8
+ @model_classification = Liblinear::Model.new(@prob, @param_1)
9
+
10
+ @param_2 = Liblinear::Parameter.new({ solver_type: Liblinear::L2R_L2LOSS_SVR })
11
+ @model_regression = Liblinear::Model.new(@prob, @param_2)
9
12
  end
10
13
 
11
14
  describe '#initialize' do
@@ -30,33 +33,69 @@ describe Liblinear::Model do
30
33
  end
31
34
  end
32
35
 
33
- describe '#nr_class' do
36
+ describe '#class_size' do
34
37
  it 'returns the number of classes' do
35
- expect(@model.nr_class).to eq(2)
38
+ expect(@model_classification.class_size).to eq(2)
39
+ end
40
+ end
41
+
42
+ describe '#feature_size' do
43
+ it 'returns the number of features' do
44
+ expect(@model_classification.feature_size).to eq(1)
36
45
  end
37
46
  end
38
47
 
39
48
  describe '#labels' do
40
49
  it 'returns labels' do
41
- expect(@model.labels).to eq([1, 2])
50
+ expect(@model_classification.labels).to eq([1, 2])
42
51
  end
43
52
  end
44
53
 
45
54
  describe '#predict' do
46
- it 'return predicted class' do
47
- expect(@model.predict([1]).class).to eq(Float)
55
+ it 'returns predicted class' do
56
+ expect(@model_classification.predict([1]).class).to eq(Float)
48
57
  end
49
58
  end
50
59
 
51
60
  describe '#predict_probability' do
52
- it 'return predict_probability' do
53
- expect(@model.predict_probability([1]).class).to eq(Hash)
61
+ it 'returns predict_probability' do
62
+ expect(@model_classification.predict_probability([1]).class).to eq(Hash)
54
63
  end
55
64
  end
56
65
 
57
66
  describe '#predict_values' do
58
- it 'return predict_values' do
59
- expect(@model.predict_values([1]).class).to eq(Hash)
67
+ it 'returns predict_values' do
68
+ expect(@model_classification.predict_values([1]).class).to eq(Hash)
69
+ end
70
+ end
71
+
72
+ describe '#coefficient' do
73
+ it 'returns a coefficient' do
74
+ expect(@model_classification.coefficient(1, 0).class).to eq(Float)
75
+ end
76
+
77
+ it 'returns a coefficient' do
78
+ expect(@model_classification.coefficient(1).class).to eq(Float)
79
+ end
80
+
81
+ it 'returns all coefficients' do
82
+ expect(@model_classification.coefficient.class).to eq(Array)
83
+ end
84
+ end
85
+
86
+ describe '#bias' do
87
+ it 'return a bias' do
88
+ expect(@model_classification.bias.class).to eq(Float)
89
+ end
90
+ end
91
+
92
+ describe '#regression_model?' do
93
+ it 'returns true' do
94
+ expect(@model_regression.regression_model?).to eq(true)
95
+ end
96
+
97
+ it 'returns false' do
98
+ expect(@model_classification.regression_model?).to eq (false)
60
99
  end
61
100
  end
62
101
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liblinear-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kei Tsuchiya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-07 00:00:00.000000000 Z
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,45 +70,45 @@ files:
70
70
  - lib/liblinear/parameter.rb
71
71
  - lib/liblinear/problem.rb
72
72
  - lib/liblinear/version.rb
73
- - liblinear-1.94/COPYRIGHT
74
- - liblinear-1.94/Makefile
75
- - liblinear-1.94/Makefile.win
76
- - liblinear-1.94/README
77
- - liblinear-1.94/blas/Makefile
78
- - liblinear-1.94/blas/blas.h
79
- - liblinear-1.94/blas/blasp.h
80
- - liblinear-1.94/blas/daxpy.c
81
- - liblinear-1.94/blas/ddot.c
82
- - liblinear-1.94/blas/dnrm2.c
83
- - liblinear-1.94/blas/dscal.c
84
- - liblinear-1.94/heart_scale
85
- - liblinear-1.94/linear.cpp
86
- - liblinear-1.94/linear.def
87
- - liblinear-1.94/linear.h
88
- - liblinear-1.94/matlab/Makefile
89
- - liblinear-1.94/matlab/README
90
- - liblinear-1.94/matlab/libsvmread.c
91
- - liblinear-1.94/matlab/libsvmwrite.c
92
- - liblinear-1.94/matlab/linear_model_matlab.c
93
- - liblinear-1.94/matlab/linear_model_matlab.h
94
- - liblinear-1.94/matlab/make.m
95
- - liblinear-1.94/matlab/predict.c
96
- - liblinear-1.94/matlab/train.c
97
- - liblinear-1.94/predict.c
98
- - liblinear-1.94/python/Makefile
99
- - liblinear-1.94/python/README
100
- - liblinear-1.94/python/liblinear.py
101
- - liblinear-1.94/python/liblinearutil.py
102
- - liblinear-1.94/train.c
103
- - liblinear-1.94/tron.cpp
104
- - liblinear-1.94/tron.h
105
- - liblinear-1.94/windows/liblinear.dll
106
- - liblinear-1.94/windows/libsvmread.mexw64
107
- - liblinear-1.94/windows/libsvmwrite.mexw64
108
- - liblinear-1.94/windows/predict.exe
109
- - liblinear-1.94/windows/predict.mexw64
110
- - liblinear-1.94/windows/train.exe
111
- - liblinear-1.94/windows/train.mexw64
73
+ - liblinear-1.95/COPYRIGHT
74
+ - liblinear-1.95/Makefile
75
+ - liblinear-1.95/Makefile.win
76
+ - liblinear-1.95/README
77
+ - liblinear-1.95/blas/Makefile
78
+ - liblinear-1.95/blas/blas.h
79
+ - liblinear-1.95/blas/blasp.h
80
+ - liblinear-1.95/blas/daxpy.c
81
+ - liblinear-1.95/blas/ddot.c
82
+ - liblinear-1.95/blas/dnrm2.c
83
+ - liblinear-1.95/blas/dscal.c
84
+ - liblinear-1.95/heart_scale
85
+ - liblinear-1.95/linear.cpp
86
+ - liblinear-1.95/linear.def
87
+ - liblinear-1.95/linear.h
88
+ - liblinear-1.95/matlab/Makefile
89
+ - liblinear-1.95/matlab/README
90
+ - liblinear-1.95/matlab/libsvmread.c
91
+ - liblinear-1.95/matlab/libsvmwrite.c
92
+ - liblinear-1.95/matlab/linear_model_matlab.c
93
+ - liblinear-1.95/matlab/linear_model_matlab.h
94
+ - liblinear-1.95/matlab/make.m
95
+ - liblinear-1.95/matlab/predict.c
96
+ - liblinear-1.95/matlab/train.c
97
+ - liblinear-1.95/predict.c
98
+ - liblinear-1.95/python/Makefile
99
+ - liblinear-1.95/python/README
100
+ - liblinear-1.95/python/liblinear.py
101
+ - liblinear-1.95/python/liblinearutil.py
102
+ - liblinear-1.95/train.c
103
+ - liblinear-1.95/tron.cpp
104
+ - liblinear-1.95/tron.h
105
+ - liblinear-1.95/windows/liblinear.dll
106
+ - liblinear-1.95/windows/libsvmread.mexw64
107
+ - liblinear-1.95/windows/libsvmwrite.mexw64
108
+ - liblinear-1.95/windows/predict.exe
109
+ - liblinear-1.95/windows/predict.mexw64
110
+ - liblinear-1.95/windows/train.exe
111
+ - liblinear-1.95/windows/train.mexw64
112
112
  - liblinear-ruby.gemspec
113
113
  - spec/liblinear/model_spec.rb
114
114
  - spec/liblinear/parameter_spec.rb
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  version: '0'
135
135
  requirements: []
136
136
  rubyforge_project:
137
- rubygems_version: 2.0.14
137
+ rubygems_version: 2.0.2
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: Ruby wrapper of LIBLINEAR using SWIG
@@ -143,3 +143,4 @@ test_files:
143
143
  - spec/liblinear/parameter_spec.rb
144
144
  - spec/liblinear/problem_spec.rb
145
145
  - spec/liblinear_spec.rb
146
+ has_rdoc:
Binary file
Binary file