{"id":19466121,"url":"https://github.com/yoshoku/numo-libsvm","last_synced_at":"2025-06-11T18:34:31.669Z","repository":{"id":56885929,"uuid":"197962109","full_name":"yoshoku/numo-libsvm","owner":"yoshoku","description":"Numo::Libsvm is a Ruby gem binding to the LIBSVM ","archived":false,"fork":false,"pushed_at":"2025-05-31T11:15:38.000Z","size":366,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-31T23:25:54.432Z","etag":null,"topics":["libsvm","machine-learning","ml","ruby","rubyml","svm"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/numo-libsvm","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yoshoku.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-07-20T17:30:40.000Z","updated_at":"2025-05-31T11:15:42.000Z","dependencies_parsed_at":"2024-01-01T10:25:40.931Z","dependency_job_id":"e02e9d1a-68bf-4ceb-88ef-041701984937","html_url":"https://github.com/yoshoku/numo-libsvm","commit_stats":{"total_commits":114,"total_committers":1,"mean_commits":114.0,"dds":0.0,"last_synced_commit":"09ea03610f66cd7638a5e23997b2b2b948a0a178"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/yoshoku/numo-libsvm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Fnumo-libsvm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Fnumo-libsvm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Fnumo-libsvm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Fnumo-libsvm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yoshoku","download_url":"https://codeload.github.com/yoshoku/numo-libsvm/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Fnumo-libsvm/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259316848,"owners_count":22839531,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["libsvm","machine-learning","ml","ruby","rubyml","svm"],"created_at":"2024-11-10T18:25:46.379Z","updated_at":"2025-06-11T18:34:31.608Z","avatar_url":"https://github.com/yoshoku.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Numo::Libsvm\n\n[![Build Status](https://github.com/yoshoku/numo-libsvm/workflows/build/badge.svg)](https://github.com/yoshoku/numo-libsvm/actions?query=workflow%3Abuild)\n[![Gem Version](https://badge.fury.io/rb/numo-libsvm.svg)](https://badge.fury.io/rb/numo-libsvm)\n[![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/numo-libsvm/blob/main/LICENSE.txt)\n[![Documentation](https://img.shields.io/badge/api-reference-blue.svg)](https://yoshoku.github.io/numo-libsvm/doc/)\n\nNumo::Libsvm is a Ruby gem binding to the [LIBSVM](https://github.com/cjlin1/libsvm) library.\nLIBSVM is one of the famous libraries that implemented Support Vector Machines,\nand provides functions for support vector classifier, regression, and distribution estimation.\nNumo::Libsvm makes to use the LIBSVM functions with dataset represented by [Numo::NArray](https://github.com/ruby-numo/numo-narray).\n\nNote: There are other useful Ruby gems binding to LIBSVM:\n[rb-libsvm](https://github.com/febeling/rb-libsvm) by C. Florian Ebeling,\n[libsvm-ruby-swig](https://github.com/tomz/libsvm-ruby-swig) by Tom Zeng,\nand [jrb-libsvm](https://github.com/andreaseger/jrb-libsvm) by Andreas Eger.\n\n## Installation\nNumo::Libsvm bundles LIBSVM. There is no need to install LIBSVM in advance.\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'numo-libsvm'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install numo-libsvm\n\n## Usage\n\n### Preparation\n\nIn the following examples, we use [red-datasets](https://github.com/red-data-tools/red-datasets) to download dataset.\n\n    $ gem install red-datasets-numo-narray\n\n### Example 1. Cross-validation\n\nWe conduct cross validation of support vector classifier on [Iris dataset](https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass.html#iris).\n\n```ruby\nrequire 'numo/narray'\nrequire 'numo/libsvm'\nrequire 'datasets-numo-narray'\n\n# Download Iris dataset.\nputs 'Download dataset.'\niris = Datasets::LIBSVM.new('iris').to_narray\nx = iris[true, 1..-1]\ny = iris[true, 0]\n\n# Define parameters of C-SVC with RBF Kernel.\nparam = {\n  svm_type: Numo::Libsvm::SvmType::C_SVC,\n  kernel_type: Numo::Libsvm::KernelType::RBF,\n  gamma: 1.0,\n  C: 1\n}\n\n# Perform 5-cross validation.\nputs 'Perform cross validation.'\nn_folds = 5\npredicted = Numo::Libsvm.cv(x, y, param, n_folds)\n\n# Print mean accuracy.\nmean_accuracy = y.eq(predicted).count.fdiv(y.size)\nputs \"Accuracy: %.1f %%\" % (100 * mean_accuracy)\n```\n\nExecution result in the following:\n\n```sh\nDownload dataset.\nPerform cross validation.\nAccuracy: 96.0 %\n```\n\n### Example 2. Pendigits dataset classification\n\nWe first train the support vector classifier with RBF kernel using training [pendigits dataset](https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass.html#pendigits).\n\n```ruby\nrequire 'numo/narray'\nrequire 'numo/libsvm'\nrequire 'datasets-numo-narray'\n\n# Download pendigits training dataset.\nputs 'Download dataset.'\npendigits = Datasets::LIBSVM.new('pendigits').to_narray\nx = pendigits[true, 1..-1]\ny = pendigits[true, 0]\n\n# Define parameters of C-SVC with RBF Kernel.\nparam = {\n  svm_type: Numo::Libsvm::SvmType::C_SVC,\n  kernel_type: Numo::Libsvm::KernelType::RBF,\n  gamma: 0.0001,\n  C: 10,\n  shrinking: true\n}\n\n# Perform training procedure.\nputs 'Train support vector machine.'\nmodel = Numo::Libsvm.train(x, y, param)\n\n# Save parameters and trained model.\nputs 'Save parameters and model with Marshal.'\nFile.open('pendigits.dat', 'wb') { |f| f.write(Marshal.dump([param, model])) }\n```\n\n```sh\n$ ruby train.rb\nDownload dataset.\nTrain support vector machine.\nSave paramters and model with Marshal.\n```\n\nWe then predict labels of testing dataset, and evaluate the classifier.\n\n```ruby\nrequire 'numo/narray'\nrequire 'numo/libsvm'\nrequire 'datasets-numo-narray'\n\n# Download pendigits testing dataset.\nputs 'Download dataset.'\npendigits_test = Datasets::LIBSVM.new('pendigits', note: 'testing').to_narray\nx = pendigits_test[true, 1..-1]\ny = pendigits_test[true, 0]\n\n# Load parameter and model.\nputs 'Load parameter and model.'\nparam, model = Marshal.load(File.binread('pendigits.dat'))\n\n# Predict labels.\nputs 'Predict labels.'\npredicted = Numo::Libsvm.predict(x, param, model)\n\n# Evaluate classification results.\nmean_accuracy = y.eq(predicted).count.fdiv(y.size)\nputs \"Accuracy: %.1f %%\" % (100 * mean_accuracy)\n```\n\n```sh\n$ ruby test.rb\nDownload dataset.\nLoad parameter and model.\nPredict labels.\nAccuracy: 98.3 %\n```\n\n### Note\nThe hyperparameter of SVM is given with Ruby Hash on Numo::Libsvm.\nThe hash key of hyperparameter and its meaning match the struct svm_parameter of LIBSVM.\nThe svm_parameter is detailed in [LIBSVM README](https://github.com/cjlin1/libsvm/blob/master/README).\n\n```ruby\nparam = {\n  svm_type:                         # [Integer] Type of SVM\n    Numo::Libsvm::SvmType::C_SVC,\n  kernel_type:                      # [Integer] Type of kernel function\n    Numo::Libsvm::KernelType::RBF,\n  degree: 3,                        # [Integer] Degree in polynomial kernel function\n  gamma: 0.5,                       # [Float] Gamma in poly/rbf/sigmoid kernel function\n  coef0: 1.0,                       # [Float] Coefficient in poly/sigmoid kernel function\n  # for training procedure\n  cache_size: 100,                  # [Float] Cache memory size in MB\n  eps: 1e-3,                        # [Float] Tolerance of termination criterion\n  C: 1.0,                           # [Float] Parameter C of C-SVC, epsilon-SVR, and nu-SVR\n  nr_weight: 3,                     # [Integer] Number of weights for C-SVC\n  weight_label:                     # [Numo::Int32] Labels to add weight in C-SVC\n    Numo::Int32[0, 1, 2],\n  weight:                           # [Numo::DFloat] Weight values in C-SVC\n    Numo::DFloat[0.4, 0.4, 0.2],\n  nu: 0.5,                          # [Float] Parameter nu of nu-SVC, one-class SVM, and nu-SVR\n  p: 0.1,                           # [Float] Parameter epsilon in loss function of epsilon-SVR\n  shrinking: true,                  # [Boolean] Whether to use the shrinking heuristics\n  probability: false,               # [Boolean] Whether to train a SVC or SVR model for probability estimates\n  verbose: false,                   # [Boolean] Whether to output learning process message\n  random_seed: 1                    # [Integer/Nil] Random seed\n}\n```\n\n## Contributing\n\nBug 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](https://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoshoku%2Fnumo-libsvm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyoshoku%2Fnumo-libsvm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoshoku%2Fnumo-libsvm/lists"}