svm_toolkit 1.1.7-java → 1.1.8-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/libsvm.jar CHANGED
Binary file
@@ -1,169 +1,169 @@
1
- module SvmToolkit
2
-
3
- # The Evaluator classes provides some classes and methods to construct
4
- # classes for evaluating the performance of a model against a dataset.
5
- # Different evaluators measure different kinds of performance.
6
- #
7
- # Evaluator classes are accessed by name, with an optional positive label name.
8
- # For example:
9
- #
10
- # Evaluator::OverallAccuracy # => class evaluates overall accuracy
11
- # Evaluator::ClassPrecision(label) # => class evaluates precision for class "label"
12
- #
13
- # Evaluators are wrapped around confusion matrices, outputting the required
14
- # statistical measure, and support the following methods:
15
- #
16
- # add_result(actual, prediction):: called to add information about each instance
17
- # when testing a model.
18
- # value:: retrieves the appropriate measure of performance, based on the class name.
19
- # to_s:: returns a string naming the evaluator and giving its value.
20
- #
21
- class Evaluator
22
-
23
- # Defines an Evaluator returning the value of precision for given class
24
- # label.
25
- #
26
- def Evaluator.ClassPrecision label
27
- Class.new(Evaluator) do
28
- @@label = label
29
-
30
- # Returns the precision.
31
- #
32
- def value
33
- @cm.precision(@@label)
34
- end
35
-
36
- def to_s # :nodoc:
37
- "Precision for label #{@@label}: #{value}"
38
- end
39
- end
40
- end
41
-
42
- # Defines an Evaluator returning the value of recall for given class
43
- # label.
44
- #
45
- def Evaluator.ClassRecall label
46
- Class.new(Evaluator) do
47
- @@label = label
48
-
49
- def value # :nodoc:
50
- @cm.recall(@@label)
51
- end
52
-
53
- def to_s # :nodoc:
54
- "Recall for label #{@@label}: #{value}"
55
- end
56
- end
57
- end
58
-
59
- # Defines an Evaluator returning the value of the F-measure for given class
60
- # label.
61
- #
62
- def Evaluator.FMeasure label
63
- Class.new(Evaluator) do
64
- @@label = label
65
-
66
- def value # :nodoc:
67
- @cm.f_measure(@@label)
68
- end
69
-
70
- def to_s # :nodoc:
71
- "F-measure for label #{@@label}: #{value}"
72
- end
73
- end
74
- end
75
-
76
- # Defines an Evaluator returning the value of Cohen's Kappa statistics for
77
- # given class label.
78
- #
79
- def Evaluator.Kappa label
80
- Class.new(Evaluator) do
81
- @@label = label
82
-
83
- def value # :nodoc:
84
- @cm.kappa(@@label)
85
- end
86
-
87
- def to_s # :nodoc:
88
- "Kappa for label #{@@label}: #{value}"
89
- end
90
- end
91
- end
92
-
93
- # Defines an Evaluator returning the value of the Matthews Correlation
94
- # Coefficient for given class label.
95
- #
96
- def Evaluator.MatthewsCorrelationCoefficient label
97
- Class.new(Evaluator) do
98
- @@label = label
99
-
100
- def value # :nodoc:
101
- @cm.matthews_correlation(@@label)
102
- end
103
-
104
- def to_s # :nodoc:
105
- "Matthews correlation coefficient: #{value}"
106
- end
107
- end
108
- end
109
-
110
- # Creates a new Evaluator, with a confusion matrix to store results.
111
- #
112
- def initialize
113
- @cm = ConfusionMatrix.new
114
- end
115
-
116
- # Adds result to the underlying confusion matrix.
117
- #
118
- def add_result(actual, prediction)
119
- @cm.add_for(actual, prediction)
120
- end
121
-
122
- # This object is better than given object, if the given object is an
123
- # instance of nil, or the value of this object is better.
124
- #
125
- def better_than? other
126
- other.nil? or self.value > other.value
127
- end
128
-
129
- # Prints the confusion matrix.
130
- #
131
- def display
132
- puts @cm
133
- end
134
- end
135
-
136
- # Defines an Evaluator returning the value of overall accuracy.
137
- #
138
- class OverallAccuracy < Evaluator
139
- # Returns the overall accuracy, as a percentage.
140
- #
141
- def value
142
- 100 * @cm.overall_accuracy
143
- end
144
-
145
- # Returns a string naming this evaluator and giving its value.
146
- #
147
- def to_s
148
- "Overall accuracy: #{value}%"
149
- end
150
- end
151
-
152
- # Defines an Evaluator returning the value of geometric mean.
153
- #
154
- class GeometricMean < Evaluator
155
- # Returns the geometric mean.
156
- #
157
- def value
158
- @cm.geometric_mean
159
- end
160
-
161
- # Returns a string naming this evaluator and giving its value.
162
- #
163
- def to_s
164
- "Geometric mean: #{value}"
165
- end
166
- end
167
-
168
- end
169
-
1
+ module SvmToolkit
2
+
3
+ # The Evaluator classes provides some classes and methods to construct
4
+ # classes for evaluating the performance of a model against a dataset.
5
+ # Different evaluators measure different kinds of performance.
6
+ #
7
+ # Evaluator classes are accessed by name, with an optional positive label name.
8
+ # For example:
9
+ #
10
+ # Evaluator::OverallAccuracy # => class evaluates overall accuracy
11
+ # Evaluator::ClassPrecision(label) # => class evaluates precision for class "label"
12
+ #
13
+ # Evaluators are wrapped around confusion matrices, outputting the required
14
+ # statistical measure, and support the following methods:
15
+ #
16
+ # add_result(actual, prediction):: called to add information about each instance
17
+ # when testing a model.
18
+ # value:: retrieves the appropriate measure of performance, based on the class name.
19
+ # to_s:: returns a string naming the evaluator and giving its value.
20
+ #
21
+ class Evaluator
22
+
23
+ # Defines an Evaluator returning the value of precision for given class
24
+ # label.
25
+ #
26
+ def Evaluator.ClassPrecision label
27
+ Class.new(Evaluator) do
28
+ @@label = label
29
+
30
+ # Returns the precision.
31
+ #
32
+ def value
33
+ @cm.precision(@@label)
34
+ end
35
+
36
+ def to_s # :nodoc:
37
+ "Precision for label #{@@label}: #{value}"
38
+ end
39
+ end
40
+ end
41
+
42
+ # Defines an Evaluator returning the value of recall for given class
43
+ # label.
44
+ #
45
+ def Evaluator.ClassRecall label
46
+ Class.new(Evaluator) do
47
+ @@label = label
48
+
49
+ def value # :nodoc:
50
+ @cm.recall(@@label)
51
+ end
52
+
53
+ def to_s # :nodoc:
54
+ "Recall for label #{@@label}: #{value}"
55
+ end
56
+ end
57
+ end
58
+
59
+ # Defines an Evaluator returning the value of the F-measure for given class
60
+ # label.
61
+ #
62
+ def Evaluator.FMeasure label
63
+ Class.new(Evaluator) do
64
+ @@label = label
65
+
66
+ def value # :nodoc:
67
+ @cm.f_measure(@@label)
68
+ end
69
+
70
+ def to_s # :nodoc:
71
+ "F-measure for label #{@@label}: #{value}"
72
+ end
73
+ end
74
+ end
75
+
76
+ # Defines an Evaluator returning the value of Cohen's Kappa statistics for
77
+ # given class label.
78
+ #
79
+ def Evaluator.Kappa label
80
+ Class.new(Evaluator) do
81
+ @@label = label
82
+
83
+ def value # :nodoc:
84
+ @cm.kappa(@@label)
85
+ end
86
+
87
+ def to_s # :nodoc:
88
+ "Kappa for label #{@@label}: #{value}"
89
+ end
90
+ end
91
+ end
92
+
93
+ # Defines an Evaluator returning the value of the Matthews Correlation
94
+ # Coefficient for given class label.
95
+ #
96
+ def Evaluator.MatthewsCorrelationCoefficient label
97
+ Class.new(Evaluator) do
98
+ @@label = label
99
+
100
+ def value # :nodoc:
101
+ @cm.matthews_correlation(@@label)
102
+ end
103
+
104
+ def to_s # :nodoc:
105
+ "Matthews correlation coefficient: #{value}"
106
+ end
107
+ end
108
+ end
109
+
110
+ # Creates a new Evaluator, with a confusion matrix to store results.
111
+ #
112
+ def initialize
113
+ @cm = ConfusionMatrix.new
114
+ end
115
+
116
+ # Adds result to the underlying confusion matrix.
117
+ #
118
+ def add_result(actual, prediction)
119
+ @cm.add_for(actual, prediction)
120
+ end
121
+
122
+ # This object is better than given object, if the given object is an
123
+ # instance of nil, or the value of this object is better.
124
+ #
125
+ def better_than? other
126
+ other.nil? or self.value > other.value
127
+ end
128
+
129
+ # Prints the confusion matrix.
130
+ #
131
+ def display
132
+ puts @cm
133
+ end
134
+ end
135
+
136
+ # Defines an Evaluator returning the value of overall accuracy.
137
+ #
138
+ class OverallAccuracy < Evaluator
139
+ # Returns the overall accuracy, as a percentage.
140
+ #
141
+ def value
142
+ 100 * @cm.overall_accuracy
143
+ end
144
+
145
+ # Returns a string naming this evaluator and giving its value.
146
+ #
147
+ def to_s
148
+ "Overall accuracy: #{value}%"
149
+ end
150
+ end
151
+
152
+ # Defines an Evaluator returning the value of geometric mean.
153
+ #
154
+ class GeometricMean < Evaluator
155
+ # Returns the geometric mean.
156
+ #
157
+ def value
158
+ @cm.geometric_mean
159
+ end
160
+
161
+ # Returns a string naming this evaluator and giving its value.
162
+ #
163
+ def to_s
164
+ "Geometric mean: #{value}"
165
+ end
166
+ end
167
+
168
+ end
169
+
@@ -1,124 +1,122 @@
1
- module SvmToolkit
2
-
3
- # Extends the Java Model class with some additional methods.
4
- #
5
- class Model
6
-
7
- # Evaluate model on given data set (an instance of Problem),
8
- # returning the number of errors made.
9
- # Optional parameters include:
10
- # * :evaluator => Evaluator::OverallAccuracy, the name of the class to use for computing performance
11
- # * :print_results => false, whether to print the result for each instance
12
- def evaluate_dataset(data, params = {})
13
- evaluator = params.fetch(:evaluator, Evaluator::OverallAccuracy)
14
- print_results = params.fetch(:print_results, false)
15
- performance = evaluator.new
16
- data.l.times do |i|
17
- pred = Svm.svm_predict(self, data.x[i])
18
- performance.add_result(data.y[i], pred)
19
- if print_results
20
- puts "Instance #{i}, Prediction: #{pred}, True label: #{data.y[i]}"
21
- end
22
- end
23
- return performance
24
- end
25
-
26
- # Return the value of w squared for the hyperplane.
27
- # -- returned as an array if there is not just one value.
28
- def w_squared
29
- if self.w_2.size == 1
30
- self.w_2[0]
31
- else
32
- self.w_2.to_a
33
- end
34
- end
35
-
36
- # Return an array of indices of the training instances used as
37
- # support vectors.
38
- def support_vector_indices
39
- result = []
40
- unless sv_indices.nil?
41
- sv_indices.size.times do |i|
42
- result << sv_indices[i]
43
- end
44
- end
45
-
46
- return result
47
- end
48
-
49
- # Return the SVM problem type for this model
50
- def svm_type
51
- self.param.svm_type
52
- end
53
-
54
- # Return the kernel type for this model
55
- def kernel_type
56
- self.param.kernel_type
57
- end
58
-
59
- # Return the value of the degree parameter
60
- def degree
61
- self.param.degree
62
- end
63
-
64
- # Return the value of the gamma parameter
65
- def gamma
66
- self.param.gamma
67
- end
68
-
69
- # Return the value of the cost parameter
70
- def cost
71
- self.param.cost
72
- end
73
-
74
- # Return the number of classes handled by this model.
75
- def number_classes
76
- self.nr_class
77
- end
78
-
79
- # Save model to given filename.
80
- # Raises IOError on any error.
81
- def save filename
82
- begin
83
- Svm.svm_save_model(filename, self)
84
- rescue java.io.IOException
85
- raise IOError.new "Error in saving SVM model to file"
86
- end
87
- end
88
-
89
- # Load model from given filename.
90
- # Raises IOError on any error.
91
- def self.load filename
92
- begin
93
- Svm.svm_load_model(filename)
94
- rescue java.io.IOException
95
- raise IOError.new "Error in loading SVM model from file"
96
- end
97
- end
98
-
99
- #
100
- # Predict the class of given instance number in given problem.
101
- #
102
- def predict(problem, instance_number)
103
- Svm.svm_predict(self, problem.x[instance_number])
104
- end
105
-
106
- #
107
- # Return the values of given instance number of given problem against
108
- # each decision boundary.
109
- # (This is the distance of the instance from each boundary.)
110
- #
111
- # Return value is an array if more than one decision boundary.
112
- #
113
- def predict_values(problem, instance_number)
114
- dist = Array.new(number_classes*(number_classes-1)/2, 0).to_java(:double)
115
- Svm.svm_predict_values(self, problem.x[instance_number], dist)
116
- if dist.size == 1
117
- return dist[0]
118
- else
119
- return dist.to_a
120
- end
121
- end
122
- end
123
- end
124
-
1
+ module SvmToolkit
2
+
3
+ # Holds information for a trained SVM model.
4
+ class Model
5
+
6
+ # Evaluate model on given data set (an instance of Problem),
7
+ # returning the number of errors made.
8
+ # Optional parameters include:
9
+ # * :evaluator => Evaluator::OverallAccuracy, the name of the class to use for computing performance
10
+ # * :print_results => false, whether to print the result for each instance
11
+ def evaluate_dataset(data, params = {})
12
+ evaluator = params.fetch(:evaluator, Evaluator::OverallAccuracy)
13
+ print_results = params.fetch(:print_results, false)
14
+ performance = evaluator.new
15
+ data.l.times do |i|
16
+ pred = Svm.svm_predict(self, data.x[i])
17
+ performance.add_result(data.y[i], pred)
18
+ if print_results
19
+ puts "Instance #{i}, Prediction: #{pred}, True label: #{data.y[i]}"
20
+ end
21
+ end
22
+ return performance
23
+ end
24
+
25
+ # Return the value of w squared for the hyperplane.
26
+ # -- returned as an array if there is not just one value.
27
+ def w_squared
28
+ if self.w_2.size == 1
29
+ self.w_2[0]
30
+ else
31
+ self.w_2.to_a
32
+ end
33
+ end
34
+
35
+ # Return an array of indices of the training instances used as
36
+ # support vectors.
37
+ def support_vector_indices
38
+ result = []
39
+ unless sv_indices.nil?
40
+ sv_indices.size.times do |i|
41
+ result << sv_indices[i]
42
+ end
43
+ end
44
+
45
+ return result
46
+ end
47
+
48
+ # Return the SVM problem type for this model
49
+ def svm_type
50
+ self.param.svm_type
51
+ end
52
+
53
+ # Return the kernel type for this model
54
+ def kernel_type
55
+ self.param.kernel_type
56
+ end
57
+
58
+ # Return the value of the degree parameter
59
+ def degree
60
+ self.param.degree
61
+ end
62
+
63
+ # Return the value of the gamma parameter
64
+ def gamma
65
+ self.param.gamma
66
+ end
67
+
68
+ # Return the value of the cost parameter
69
+ def cost
70
+ self.param.cost
71
+ end
72
+
73
+ # Return the number of classes handled by this model.
74
+ def number_classes
75
+ self.nr_class
76
+ end
77
+
78
+ # Save model to given filename.
79
+ # Raises IOError on any error.
80
+ def save filename
81
+ begin
82
+ Svm.svm_save_model(filename, self)
83
+ rescue java.io.IOException
84
+ raise IOError.new "Error in saving SVM model to file"
85
+ end
86
+ end
87
+
88
+ # Load model from given filename.
89
+ # Raises IOError on any error.
90
+ def self.load filename
91
+ begin
92
+ Svm.svm_load_model(filename)
93
+ rescue java.io.IOException
94
+ raise IOError.new "Error in loading SVM model from file"
95
+ end
96
+ end
97
+
98
+ #
99
+ # Predict the class of given instance number in given problem.
100
+ #
101
+ def predict(problem, instance_number)
102
+ Svm.svm_predict(self, problem.x[instance_number])
103
+ end
104
+
105
+ # Return the values of given instance number of given problem against
106
+ # each decision boundary.
107
+ # (This is the distance of the instance from each boundary.)
108
+ #
109
+ # Return value is an array if more than one decision boundary.
110
+ #
111
+ def predict_values(problem, instance_number)
112
+ dist = Array.new(number_classes*(number_classes-1)/2, 0).to_java(:double)
113
+ Svm.svm_predict_values(self, problem.x[instance_number], dist)
114
+ if dist.size == 1
115
+ return dist[0]
116
+ else
117
+ return dist.to_a
118
+ end
119
+ end
120
+ end
121
+ end
122
+
@@ -1,21 +1,17 @@
1
- module SvmToolkit
2
-
3
- # Extends the Java Node class.
4
- #
5
- # Node is used to store the index/value pair for an individual
6
- # feature of an instance.
7
- #
8
- class Node
9
-
10
- # Constructor:
11
- # index:: Index of this node in feature set.
12
- # value:: Value of this node in feature set.
13
- def initialize(index, value)
14
- super()
15
- self.index = index
16
- self.value = value
17
- end
18
- end
19
-
20
- end
21
-
1
+ module SvmToolkit
2
+
3
+ # Used to store the index/value pair for an individual
4
+ # feature of an instance.
5
+ class Node
6
+
7
+ # Constructor accepts index and value of this node
8
+ # in feature set.
9
+ def initialize(index, value)
10
+ super()
11
+ self.index = index
12
+ self.value = value
13
+ end
14
+ end
15
+
16
+ end
17
+