项目作者: yoshoku

项目描述 :
Numo::Libsvm is a Ruby gem binding to the LIBSVM
高级语言: C
项目地址: git://github.com/yoshoku/numo-libsvm.git
创建时间: 2019-07-20T17:30:40Z
项目社区:https://github.com/yoshoku/numo-libsvm

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Numo::Libsvm

Build Status
Gem Version
BSD 3-Clause License
Documentation

Numo::Libsvm is a Ruby gem binding to the LIBSVM library.
LIBSVM is one of the famous libraries that implemented Support Vector Machines,
and provides functions for support vector classifier, regression, and distribution estimation.
Numo::Libsvm makes to use the LIBSVM functions with dataset represented by Numo::NArray.

Note: There are other useful Ruby gems binding to LIBSVM:
rb-libsvm by C. Florian Ebeling,
libsvm-ruby-swig by Tom Zeng,
and jrb-libsvm by Andreas Eger.

Installation

Numo::Libsvm bundles LIBSVM. There is no need to install LIBSVM in advance.

Add this line to your application’s Gemfile:

  1. gem 'numo-libsvm'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install numo-libsvm

Usage

Preparation

In the following examples, we use red-datasets to download dataset.

  1. $ gem install red-datasets-numo-narray

Example 1. Cross-validation

We conduct cross validation of support vector classifier on Iris dataset.

  1. require 'numo/narray'
  2. require 'numo/libsvm'
  3. require 'datasets-numo-narray'
  4. # Download Iris dataset.
  5. puts 'Download dataset.'
  6. iris = Datasets::LIBSVM.new('iris').to_narray
  7. x = iris[true, 1..-1]
  8. y = iris[true, 0]
  9. # Define parameters of C-SVC with RBF Kernel.
  10. param = {
  11. svm_type: Numo::Libsvm::SvmType::C_SVC,
  12. kernel_type: Numo::Libsvm::KernelType::RBF,
  13. gamma: 1.0,
  14. C: 1
  15. }
  16. # Perform 5-cross validation.
  17. puts 'Perform cross validation.'
  18. n_folds = 5
  19. predicted = Numo::Libsvm.cv(x, y, param, n_folds)
  20. # Print mean accuracy.
  21. mean_accuracy = y.eq(predicted).count.fdiv(y.size)
  22. puts "Accuracy: %.1f %%" % (100 * mean_accuracy)

Execution result in the following:

  1. Download dataset.
  2. Perform cross validation.
  3. Accuracy: 96.0 %

Example 2. Pendigits dataset classification

We first train the support vector classifier with RBF kernel using training pendigits dataset.

  1. require 'numo/narray'
  2. require 'numo/libsvm'
  3. require 'datasets-numo-narray'
  4. # Download pendigits training dataset.
  5. puts 'Download dataset.'
  6. pendigits = Datasets::LIBSVM.new('pendigits').to_narray
  7. x = pendigits[true, 1..-1]
  8. y = pendigits[true, 0]
  9. # Define parameters of C-SVC with RBF Kernel.
  10. param = {
  11. svm_type: Numo::Libsvm::SvmType::C_SVC,
  12. kernel_type: Numo::Libsvm::KernelType::RBF,
  13. gamma: 0.0001,
  14. C: 10,
  15. shrinking: true
  16. }
  17. # Perform training procedure.
  18. puts 'Train support vector machine.'
  19. model = Numo::Libsvm.train(x, y, param)
  20. # Save parameters and trained model.
  21. puts 'Save parameters and model with Marshal.'
  22. File.open('pendigits.dat', 'wb') { |f| f.write(Marshal.dump([param, model])) }
  1. $ ruby train.rb
  2. Download dataset.
  3. Train support vector machine.
  4. Save paramters and model with Marshal.

We then predict labels of testing dataset, and evaluate the classifier.

  1. require 'numo/narray'
  2. require 'numo/libsvm'
  3. require 'datasets-numo-narray'
  4. # Download pendigits testing dataset.
  5. puts 'Download dataset.'
  6. pendigits_test = Datasets::LIBSVM.new('pendigits', note: 'testing').to_narray
  7. x = pendigits_test[true, 1..-1]
  8. y = pendigits_test[true, 0]
  9. # Load parameter and model.
  10. puts 'Load parameter and model.'
  11. param, model = Marshal.load(File.binread('pendigits.dat'))
  12. # Predict labels.
  13. puts 'Predict labels.'
  14. predicted = Numo::Libsvm.predict(x, param, model)
  15. # Evaluate classification results.
  16. mean_accuracy = y.eq(predicted).count.fdiv(y.size)
  17. puts "Accuracy: %.1f %%" % (100 * mean_accuracy)
  1. $ ruby test.rb
  2. Download dataset.
  3. Load parameter and model.
  4. Predict labels.
  5. Accuracy: 98.3 %

Note

The hyperparameter of SVM is given with Ruby Hash on Numo::Libsvm.
The hash key of hyperparameter and its meaning match the struct svm_parameter of LIBSVM.
The svm_parameter is detailed in LIBSVM README.

  1. param = {
  2. svm_type: # [Integer] Type of SVM
  3. Numo::Libsvm::SvmType::C_SVC,
  4. kernel_type: # [Integer] Type of kernel function
  5. Numo::Libsvm::KernelType::RBF,
  6. degree: 3, # [Integer] Degree in polynomial kernel function
  7. gamma: 0.5, # [Float] Gamma in poly/rbf/sigmoid kernel function
  8. coef0: 1.0, # [Float] Coefficient in poly/sigmoid kernel function
  9. # for training procedure
  10. cache_size: 100, # [Float] Cache memory size in MB
  11. eps: 1e-3, # [Float] Tolerance of termination criterion
  12. C: 1.0, # [Float] Parameter C of C-SVC, epsilon-SVR, and nu-SVR
  13. nr_weight: 3, # [Integer] Number of weights for C-SVC
  14. weight_label: # [Numo::Int32] Labels to add weight in C-SVC
  15. Numo::Int32[0, 1, 2],
  16. weight: # [Numo::DFloat] Weight values in C-SVC
  17. Numo::DFloat[0.4, 0.4, 0.2],
  18. nu: 0.5, # [Float] Parameter nu of nu-SVC, one-class SVM, and nu-SVR
  19. p: 0.1, # [Float] Parameter epsilon in loss function of epsilon-SVR
  20. shrinking: true, # [Boolean] Whether to use the shrinking heuristics
  21. probability: false, # [Boolean] Whether to train a SVC or SVR model for probability estimates
  22. verbose: false, # [Boolean] Whether to output learning process message
  23. random_seed: 1 # [Integer/Nil] Random seed
  24. }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-libsvm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the BSD-3-Clause License.