Created
July 21, 2014 20:11
-
-
Save dennysfredericci/2e3a7698e2ac62a67447 to your computer and use it in GitHub Desktop.
Machine Learning example using libsvm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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