Module: Numo::Libsvm

Defined in:
ext/numo/libsvm/libsvmext.cpp,
lib/numo/libsvm/version.rb,
ext/numo/libsvm/libsvmext.cpp

Overview

Numo::Libsvm is a binding library for LIBSVM that handles dataset with Numo::NArray.

Defined Under Namespace

Modules: KernelType, SvmType

Constant Summary collapse

VERSION =

The version of Numo::Libsvm you are using.

'2.2.0'
LIBSVM_VERSION =

The version of LIBSVM used in backgroud library.

INT2NUM(LIBSVM_VERSION)

Class Method Summary collapse

Class Method Details

.cv(x, y, param, n_folds) ⇒ Numo::DFloat

Perform cross validation under given parameters. The given samples are separated to n_fols folds. The predicted labels or values in the validation process are returned.

Examples:

require 'numo/libsvm'

# x: samples
# y: labels

# Define parameters of C-SVC with RBF Kernel.
param = {
  svm_type: Numo::Libsvm::SvmType::C_SVC,
  kernel_type: Numo::Libsvm::KernelType::RBF,
  gamma: 1.0,
  C: 1,
  random_seed: 1,
  verbose: true
}

# Perform 5-cross validation.
n_folds = 5
res = Numo::Libsvm.cv(x, y, param, n_folds)

# Print mean accuracy.
mean_accuracy = y.eq(res).count.fdiv(y.size)
puts "Accuracy: %.1f %%" % (100 * mean_accuracy)

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to be used for training the model.

  • y (Numo::DFloat)

    (shape: [n_samples]) The labels or target values for samples.

  • param (Hash)

    The parameters of an SVM model.

  • n_folds (Integer)

    The number of folds.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples]) The predicted class label or value of each sample.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, the label array is not 1-dimensional, the sample array and label array do not have the same number of samples, or the hyperparameter has an invalid value, this error is raised.

.decision_function(x, param, model) ⇒ Numo::DFloat

Calculate decision values for given samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate the scores.

  • param (Hash)

    The parameters of the trained SVM model.

  • model (Hash)

    The model obtained from the training procedure.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes * (n_classes - 1) / 2]) The decision value of each sample.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, this error is raised.

.load_svm_model(filename) ⇒ Array

Load the SVM parameters and model from a text file with LIBSVM format.

Parameters:

  • filename (String)

    The path to a file to load.

Returns:

  • (Array)

    Array contains the SVM parameters and model.

Raises:

  • (IOError)

    This error raises when failed to load the model file.

.predict(x, param, model) ⇒ Numo::DFloat

Predict class labels or values for given samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to calculate the scores.

  • param (Hash)

    The parameters of the trained SVM model.

  • model (Hash)

    The model obtained from the training procedure.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples]) The predicted class label or value of each sample.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, this error is raised.

.predict_proba(x, param, model) ⇒ Numo::DFloat

Predict class probability for given samples. The model must have probability information calcualted in training procedure. The parameter ‘:probability’ set to 1 in training procedure.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the class probabilities.

  • param (Hash)

    The parameters of the trained SVM model.

  • model (Hash)

    The model obtained from the training procedure.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) Predicted probablity of each class per sample.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, this error is raised.

.save_svm_model(filename, param, model) ⇒ Boolean

Save the SVM parameters and model as a text file with LIBSVM format. The saved file can be used with the libsvm tools. Note that the svm_save_model saves only the parameters necessary for estimation with the trained model.

Parameters:

  • filename (String)

    The path to a file to save.

  • param (Hash)

    The parameters of the trained SVM model.

  • model (Hash)

    The model obtained from the training procedure.

Returns:

  • (Boolean)

    true on success, or false if an error occurs.

Raises:

  • (IOError)

    This error raises when failed to save the model file.

.train(x, y, param) ⇒ Hash

Train the SVM model according to the given training data.

Examples:

require 'numo/libsvm'

# Prepare XOR data.
x = Numo::DFloat[[-0.8, -0.7], [0.9, 0.8], [-0.7, 0.9], [0.8, -0.9]]
y = Numo::Int32[-1, -1, 1, 1]

# Train C-Support Vector Classifier with RBF kernel.
param = {
  svm_type: Numo::Libsvm::SvmType::C_SVC,
  kernel_type: Numo::Libsvm::KernelType::RBF,
  gamma: 2.0,
  C: 1,
  random_seed: 1
}
model = Numo::Libsvm.train(x, y, param)

# Predict labels of test data.
x_test = Numo::DFloat[[-0.4, -0.5], [0.5, -0.4]]
result = Numo::Libsvm.predict(x_test, param, model)
p result
# Numo::DFloat#shape=[2]
# [-1, 1]

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to be used for training the model.

  • y (Numo::DFloat)

    (shape: [n_samples]) The labels or target values for samples.

  • param (Hash)

    The parameters of an SVM model.

Returns:

  • (Hash)

    The model obtained from the training procedure.

Raises:

  • (ArgumentError)

    If the sample array is not 2-dimensional, the label array is not 1-dimensional, the sample array and label array do not have the same number of samples, or the hyperparameter has an invalid value, this error is raised.