Class: Rumale::KernelMachine::KernelRidgeClassifier
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::KernelMachine::KernelRidgeClassifier
- Includes:
- Base::Classifier
- Defined in:
- rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb
Overview
KernelRidgeClassifier is a class that implements classifier based-on kernel ridge regression. It learns a classifier by converting labels to target values { -1, 1 } and performing kernel ridge regression.
Instance Attribute Summary collapse
-
#classes ⇒ Numo::Int32
readonly
Return the class labels.
-
#weight_vec ⇒ Numo::DFloat
readonly
Return the weight vector.
Attributes inherited from Base::Estimator
Instance Method Summary collapse
-
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
-
#fit(x, y) ⇒ KernelRidgeClassifier
Fit the model with given training data.
-
#initialize(reg_param: 1.0) ⇒ KernelRidgeClassifier
constructor
Create a new regressor with kernel ridge classifier.
-
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
Methods included from Base::Classifier
Constructor Details
#initialize(reg_param: 1.0) ⇒ KernelRidgeClassifier
Create a new regressor with kernel ridge classifier.
38 39 40 41 42 43 |
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 38 def initialize(reg_param: 1.0) super() @params = { reg_param: reg_param } end |
Instance Attribute Details
#classes ⇒ Numo::Int32 (readonly)
Return the class labels.
29 30 31 |
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 29 def classes @classes end |
#weight_vec ⇒ Numo::DFloat (readonly)
Return the weight vector.
33 34 35 |
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 33 def weight_vec @weight_vec end |
Instance Method Details
#decision_function(x) ⇒ Numo::DFloat
Calculate confidence scores for samples.
73 74 75 76 77 |
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 73 def decision_function(x) x = ::Rumale::Validation.check_convert_sample_array(x) x.dot(@weight_vec) end |
#fit(x, y) ⇒ KernelRidgeClassifier
Fit the model with given training data.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 51 def fit(x, y) x = ::Rumale::Validation.check_convert_sample_array(x) y = ::Rumale::Validation.check_convert_label_array(y) ::Rumale::Validation.check_sample_size(x, y) raise ArgumentError, 'Expect the kernel matrix of training data to be square.' unless x.shape[0] == x.shape[1] raise 'KernelRidgeClassifier#fit requires Numo::Linalg but that is not loaded.' unless enable_linalg?(warning: false) y_encoded = Numo::DFloat.cast(::Rumale::Utils.binarize_labels(y)) * 2 - 1 @classes = Numo::NArray[*y.to_a.uniq.sort] n_samples = x.shape[0] reg_kernel_mat = x + Numo::DFloat.eye(n_samples) * @params[:reg_param] @weight_vec = Numo::Linalg.solve(reg_kernel_mat, y_encoded, driver: 'sym') self end |
#predict(x) ⇒ Numo::Int32
Predict class labels for samples.
84 85 86 87 88 89 90 91 |
# File 'rumale-kernel_machine/lib/rumale/kernel_machine/kernel_ridge_classifier.rb', line 84 def predict(x) x = ::Rumale::Validation.check_convert_sample_array(x) scores = decision_function(x) n_samples, n_classes = scores.shape label_ids = scores.max_index(axis: 1) - Numo::Int32.new(n_samples).seq * n_classes @classes[label_ids].dup end |