Class: SVMKit::Multiclass::OneVsRestClassifier
- Inherits:
-
Object
- Object
- SVMKit::Multiclass::OneVsRestClassifier
- Includes:
- Base::BaseEstimator, Base::Classifier
- Defined in:
- lib/svmkit/multiclass/one_vs_rest_classifier.rb
Overview
OneVsRestClassifier is a class that implements One-vs-Rest (OvR) strategy for multi-label classification.
Instance Attribute Summary collapse
-
#classes ⇒ NMatrix
readonly
Return the class labels.
-
#estimators ⇒ Array<Classifier>
readonly
Return the set of estimators.
Attributes included from Base::BaseEstimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ NMatrix
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ OneVsRestClassifier
Fit the model with given training data.
-
#new(estimator: base_estimator) ⇒ OneVsRestClassifier
constructor
Create a new multi-label classifier with the one-vs-rest startegy.
-
#marshal_dump ⇒ Hash
Dump marshal data.
-
#marshal_load(obj) ⇒ nil
Load marshal data.
-
#predict(x) ⇒ NMatrix
Predict class labels for samples.
-
#score(x, y) ⇒ Float
Claculate the mean accuracy of the given testing data.
Constructor Details
#new(estimator: base_estimator) ⇒ OneVsRestClassifier
Create a new multi-label classifier with the one-vs-rest startegy.
37 38 39 40 41 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 37 def initialize(params = {}) self.params = DEFAULT_PARAMS.merge(Hash[params.map { |k, v| [k.to_sym, v] }]) @estimators = nil @classes = nil end |
Instance Attribute Details
#classes ⇒ NMatrix (readonly)
Return the class labels.
30 31 32 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 30 def classes @classes end |
#estimators ⇒ Array<Classifier> (readonly)
Return the set of estimators.
26 27 28 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 26 def estimators @estimators end |
Instance Method Details
#decision_function(x) ⇒ NMatrix
Calculate confidence scores for samples.
61 62 63 64 65 66 67 68 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 61 def decision_function(x) n_samples, = x.shape n_classes = @classes.size NMatrix.new( [n_classes, n_samples], Array.new(n_classes) { |m| @estimators[m].decision_function(x).to_a }.flatten ).transpose end |
#fit(x, y) ⇒ OneVsRestClassifier
Fit the model with given training data.
48 49 50 51 52 53 54 55 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 48 def fit(x, y) @classes = y.uniq.sort @estimators = @classes.map do |label| bin_y = y.map { |l| l == label ? 1 : -1 } params[:estimator].dup.fit(x, bin_y) end self end |
#marshal_dump ⇒ Hash
Dump marshal data.
94 95 96 97 98 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 94 def marshal_dump { params: params, classes: @classes, estimators: @estimators.map { |e| Marshal.dump(e) } } end |
#marshal_load(obj) ⇒ nil
Load marshal data.
102 103 104 105 106 107 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 102 def marshal_load(obj) self.params = obj[:params] @classes = obj[:classes] @estimators = obj[:estimators].map { |e| Marshal.load(e) } nil end |
#predict(x) ⇒ NMatrix
Predict class labels for samples.
74 75 76 77 78 79 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 74 def predict(x) n_samples, = x.shape decision_values = decision_function(x) NMatrix.new([1, n_samples], decision_values.each_row.map { |vals| @classes[vals.to_a.index(vals.to_a.max)] }) end |
#score(x, y) ⇒ Float
Claculate the mean accuracy of the given testing data.
86 87 88 89 90 |
# File 'lib/svmkit/multiclass/one_vs_rest_classifier.rb', line 86 def score(x, y) p = predict(x) n_hits = (y.to_flat_a.map.with_index { |l, n| l == p[n] ? 1 : 0 }).inject(:+) n_hits / y.size.to_f end |