class Rumale::SVM::OneClassSVM
OneClassSVM
is a class that provides One-class Support Vector Machine in LIBSVM with Rumale
interface.
@example
estimator = Rumale::SVM::OneClassSVM.new(nu: 0.5, kernel: 'rbf', gamma: 10.0, random_seed: 1) estimator.fit(training_samples, traininig_labels) results = estimator.predict(testing_samples)
Public Class Methods
Create a new estimator with One-class Support Vector Machine.
@param nu [Float] The regularization parameter. The interval of nu is (0, 1]. @param kernel [String] The type of kernel function ('rbf', 'linear', 'poly', 'sigmoid', and 'precomputed'). @param degree [Integer] The degree parameter in polynomial kernel function. @param gamma [Float] The gamma parameter in rbf/poly/sigmoid kernel function. @param coef0 [Float] The coefficient in poly/sigmoid kernel function. @param shrinking [Boolean] The flag indicating whether to use the shrinking heuristics. @param cache_size [Float] The cache memory size in MB. @param tol [Float] The tolerance of termination criterion. @param verbose [Boolean] The flag indicating whether to output learning process message @param random_seed [Integer/Nil] The seed value using to initialize the random generator.
# File lib/rumale/svm/one_class_svm.rb, line 31 def initialize(nu: 1.0, kernel: 'rbf', degree: 3, gamma: 1.0, coef0: 0.0, shrinking: true, cache_size: 200.0, tol: 1e-3, verbose: false, random_seed: nil) check_params_numeric(nu: nu, degree: degree, gamma: gamma, coef0: coef0, cache_size: cache_size, tol: tol) check_params_string(kernel: kernel) check_params_boolean(shrinking: shrinking, verbose: verbose) check_params_numeric_or_nil(random_seed: random_seed) @params = {} @params[:nu] = nu.to_f @params[:kernel] = kernel @params[:degree] = degree.to_i @params[:gamma] = gamma.to_f @params[:coef0] = coef0.to_f @params[:shrinking] = shrinking @params[:cache_size] = cache_size.to_f @params[:tol] = tol.to_f @params[:verbose] = verbose @params[:random_seed] = random_seed.nil? ? nil : random_seed.to_i end
Public Instance Methods
Calculate confidence scores for samples.
@param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to compute the scores.
If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
@return [Numo::DFloat] (shape: [n_samples, n_classes]) Confidence score per sample.
# File lib/rumale/svm/one_class_svm.rb, line 68 def decision_function(x) raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained? x = check_convert_sample_array(x) Numo::Libsvm.decision_function(x, libsvm_params, @model) end
Return the coefficients of the support vector in decision function. @return [Numo::DFloat] (shape: [1, n_support_vectors])
# File lib/rumale/svm/one_class_svm.rb, line 120 def duel_coef @model[:sv_coef] end
Fit the model with given training data.
@overload fit(x) -> OneClassSVM
@param x [Numo::DFloat] (shape: [n_samples, n_features]) The training data to be used for fitting the model. If the kernel is 'precomputed', x must be a square distance matrix (shape: [n_samples, n_samples]).
@return [OneClassSVM] The learned estimator itself.
# File lib/rumale/svm/one_class_svm.rb, line 56 def fit(x, _y = nil) x = check_convert_sample_array(x) dummy = Numo::DFloat.ones(x.shape[0]) @model = Numo::Libsvm.train(x, dummy, libsvm_params) self end
Return the intercepts in decision function. @return [Numo::DFloat] (shape: [1])
# File lib/rumale/svm/one_class_svm.rb, line 126 def intercept @model[:rho] end
Dump marshal data. @return [Hash] The marshal data about SVC
.
# File lib/rumale/svm/one_class_svm.rb, line 87 def marshal_dump { params: @params, model: @model } end
Load marshal data. @return [nil]
# File lib/rumale/svm/one_class_svm.rb, line 94 def marshal_load(obj) @params = obj[:params] @model = obj[:model] nil end
Return the number of support vectors. @return [Integer]
# File lib/rumale/svm/one_class_svm.rb, line 114 def n_support @model[:sv_indices].size end
Predict class labels for samples.
@param x [Numo::DFloat] (shape: [n_samples, n_features]) The samples to predict the labels.
If the kernel is 'precomputed', the shape of x must be [n_samples, n_training_samples].
@return [Numo::Int32] (shape: [n_samples]) Predicted label per sample.
# File lib/rumale/svm/one_class_svm.rb, line 79 def predict(x) raise "#{self.class.name}\##{__method__} expects to be called after training the model with the fit method." unless trained? x = check_convert_sample_array(x) Numo::Int32.cast(Numo::Libsvm.predict(x, libsvm_params, @model)) end
Return the indices of support vectors. @return [Numo::Int32] (shape: [n_support_vectors])
# File lib/rumale/svm/one_class_svm.rb, line 102 def support @model[:sv_indices] end
Return the support_vectors. @return [Numo::DFloat] (shape: [n_support_vectors, n_features])
# File lib/rumale/svm/one_class_svm.rb, line 108 def support_vectors @model[:SV] end
Private Instance Methods
# File lib/rumale/svm/one_class_svm.rb, line 132 def libsvm_params res = @params.merge(svm_type: Numo::Libsvm::SvmType::ONE_CLASS) res[:kernel_type] = case res.delete(:kernel) when 'linear' Numo::Libsvm::KernelType::LINEAR when 'poly' Numo::Libsvm::KernelType::POLY when 'sigmoid' Numo::Libsvm::KernelType::SIGMOID when 'precomputed' Numo::Libsvm::KernelType::PRECOMPUTED else Numo::Libsvm::KernelType::RBF end res[:eps] = res.delete(:tol) res end
# File lib/rumale/svm/one_class_svm.rb, line 150 def trained? !@model.nil? end