Build Status Gem Version BSD 2-Clause License

SVMKit is a library for machine learninig in Ruby. SVMKit implements machine learning algorithms with an interface similar to Scikit-Learn in Python. However, since SVMKit is an experimental library, there are few machine learning algorithms implemented.


Add this line to your application's Gemfile:

gem 'svmkit'

And then execute:

$ bundle

Or install it yourself as:

$ gem install svmkit


Training phase:

require 'svmkit'
require 'libsvmloader'

samples, labels = LibSVMLoader.load_libsvm_file('pendigits', stype: :dense)

normalizer =
normalized = normalizer.fit_transform(samples)

transformer = 2.0, n_components: 1024, random_seed: 1)
transformed = transformer.fit_transform(normalized)

base_classifier = 1.0, max_iter: 50, batch_size: 20, random_seed: 1)
classifier = base_classifier), labels)'trained_normalizer.dat', 'wb') { |f| f.write(Marshal.dump(normalizer)) }'trained_transformer.dat', 'wb') { |f| f.write(Marshal.dump(transformer)) }'trained_classifier.dat', 'wb') { |f| f.write(Marshal.dump(classifier)) }

Testing phase:

require 'svmkit'
require 'libsvmloader'

samples, labels = LibSVMLoader.load_libsvm_file('pendigits.t', stype: :dense)

normalizer = Marshal.load(File.binread('trained_normalizer.dat'))
transformer = Marshal.load(File.binread('trained_transformer.dat'))
classifier = Marshal.load(File.binread('trained_classifier.dat'))

normalized = normalizer.transform(samples)
transformed = transformer.transform(normalized)

puts(sprintf("Accuracy: %.1f%%", 100.0 * classifier.score(transformed, labels)))


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at 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.


The gem is available as open source under the terms of the BSD 2-clause License.

Code of Conduct

Everyone interacting in the SVMKit project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.