Skip to content

Instantly share code, notes, and snippets.

@dennysfredericci
Created July 21, 2014 20:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennysfredericci/2e3a7698e2ac62a67447 to your computer and use it in GitHub Desktop.
Save dennysfredericci/2e3a7698e2ac62a67447 to your computer and use it in GitHub Desktop.
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