Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Machine Learning example using libsvm
#gem install rb-libsvm
require 'libsvm'
class Predictor
def initialize
parameter = Libsvm::SvmParameter.new
parameter.cache_size = 3 # in megabytes
parameter.eps = 0.001
parameter.c = 10
examples = [ [1,0,1], [-1,0,-1] ].map {|ary| Libsvm::Node.features(ary) }
labels = [ 3 , 7 ]
problem = Libsvm::Problem.new
problem.set_examples(labels, examples)
@model = Libsvm::Model.train(problem, parameter)
end
def sayHi
pred = @model.predict(Libsvm::Node.features(1, 0, 1))
puts "Example [1, 1, 1] - Predicted #{pred}"
pred = @model.predict(Libsvm::Node.features(-1, 0, -1))
puts "Example [-1, 0, -1] - Predicted #{pred}"
pred = @model.predict(Libsvm::Node.features(-10, 0, 25))
puts "Example [-10, 0, 25] - Predicted #{pred}"
pred = @model.predict(Libsvm::Node.features(12, 0, 30))
puts "Example [12, 0, 30] - Predicted #{pred}"
pred = @model.predict(Libsvm::Node.features(65, 0, -54))
puts "Example [65, 0, -54] - Predicted #{pred}"
end
end
hello = Predictor.new()
hello.sayHi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment