Skip to content

Commit

Permalink
fixed a number of .rb scripts and removed broken non-trivial to fix ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Aug 31, 2011
1 parent e3c14a4 commit 52fb28d
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 375 deletions.
2 changes: 1 addition & 1 deletion data
Submodule data updated from 0d3c49 to 90a72b

This file was deleted.

@@ -1,35 +1,35 @@
require 'modshogun'
require 'narray'
require './shogun_helpers'
require 'load'

@num = 1000
@dist = 1
@width = 2.1
C = 1

puts "generating training data"
traindata_real = gen_rand_ary
testdata_real = gen_rand_ary

puts "generating labels"
trainlab = gen_ones_vec
testlab = gen_ones_vec

puts "doing feature stuff"
feats_train = Modshogun::RealFeatures.new
feats_train.set_feature_matrix traindata_real
feats_test = Modshogun::RealFeatures.new
feats_test.set_feature_matrix testdata_real
kernel = Modshogun::GaussianKernel.new feats_train, feats_train, @width

puts "labeling stuff"
labels = Modshogun::Labels.new
labels.set_labels trainlab
svm = Modshogun::LibSVM.new C, kernel, labels
svm.train

puts "the grand finale"
kernel.init feats_train, feats_test
out = svm.apply.get_labels
testerr = mean out.sign.eql_items? testlab
puts testerr
#puts "generating training data"
#traindata_real = gen_rand_ary(@num, @dist)
#testdata_real = gen_rand_ary(@num, @dist)
#
#puts "generating labels"
#trainlab = gen_labels_vec(@num)
#testlab = gen_labels_vec(@num)
#
#puts "doing feature stuff"
#feats_train = Modshogun::RealFeatures.new
#feats_train.set_feature_matrix traindata_real
#feats_test = Modshogun::RealFeatures.new
#feats_test.set_feature_matrix testdata_real
#kernel = Modshogun::GaussianKernel.new feats_train, feats_train, @width
#
#puts "labeling stuff"
#labels = Modshogun::Labels.new
#labels.set_labels trainlab
#svm = Modshogun::LibSVM.new C, kernel, labels
#svm.train
#
#puts "the grand finale"
#kernel.init feats_train, feats_test
#out = svm.apply.get_labels
#testerr = mean out.sign.eql_items? testlab
#puts testerr
@@ -1,5 +1,6 @@
require 'modshogun'
require 'pp'
require 'load'

traindat = LoadMatrix.load_numbers('../data/fm_train_real.dat')

Expand Down
@@ -1,5 +1,6 @@
require 'modshogun'
require 'pp'
require 'load'

traindat = LoadMatrix.load_numbers('../data/fm_train_real.dat')
testdat = LoadMatrix.load_numbers('../data/fm_test_real.dat')
Expand Down
@@ -1,5 +1,6 @@
require 'modshogun'
require 'pp'
require 'load'

traindat = LoadMatrix.load_numbers('../data/fm_train_real.dat')
testdat = LoadMatrix.load_numbers('../data/fm_test_real.dat')
Expand Down
@@ -1,5 +1,6 @@
require 'modshogun'
require 'pp'
require 'load'

traindna = LoadMatrix.load_dna('../data/fm_train_dna.dat')

Expand Down
1 change: 1 addition & 0 deletions examples/undocumented/ruby_modular/kernel_anova_modular.rb
@@ -1,5 +1,6 @@
require 'modshogun'
require 'pp'
require 'load'
###########################################################################
# anova kernel
###########################################################################
Expand Down
@@ -1,5 +1,6 @@
require 'modshogun'
require 'pp'
require 'load'

traindat = LoadMatrix.load_numbers('../data/fm_train_real.dat')
testdat = LoadMatrix.load_numbers('../data/fm_test_real.dat')
Expand Down
1 change: 1 addition & 0 deletions examples/undocumented/ruby_modular/kernel_chi2_modular.rb
@@ -1,5 +1,6 @@
require 'modshogun'
require 'pp'
require 'load'
###########################################################################
# chi2 kernel
###########################################################################
Expand Down
@@ -1,5 +1,6 @@
require 'modshogun'
require 'pp'
require 'load'

traindat = LoadMatrix.load_numbers('../data/fm_train_real.dat')
testdat = LoadMatrix.load_numbers('../data/fm_test_real.dat')
Expand Down
Expand Up @@ -2,6 +2,7 @@
# ...and fixifikated by the awesum fixifikator
require 'modshogun'
require 'pp'
require 'load'

traindat = LoadMatrix.load_numbers('../data/fm_train_real.dat')
testdat = LoadMatrix.load_numbers('../data/fm_test_real.dat')
Expand Down
214 changes: 165 additions & 49 deletions examples/undocumented/ruby_modular/load.rb
@@ -1,11 +1,15 @@
require 'narray'
require 'pp'
require 'modshogun.so'

# for debugging purposes...
#require 'rubygems'
#require "pry"

def LoadMatrix(filename, type = :numbers)
case type
when :numbers
x = LoadMatrix.load_numbers(filename)
pp x
pp x
when :dna
LoadMatrix.load_dna(filename)
when :cubes
Expand All @@ -16,55 +20,167 @@ def LoadMatrix(filename, type = :numbers)
end

module LoadMatrix
def load_numbers(filename)
matrix = []
File.open(filename) do |file|
file.each_line do |line|
ary = []
line.split(" ").each{ |n| ary << n.to_f }
matrix << ary
end
end
#pp matrix
matrix = NArray.to_na(matrix)
#pp matrix
matrix = matrix.transpose(1,0)
#pp matrix
end

def load_dna(filename)
matrix=[]
File.open(filename) do |file|
file.each_line do |line|
line.split("\n").each{|n| matrix << n }
end
end
matrix
end


def load_cubes(filename)
matrix=[]
File.open(filename) do |file|
file.each_line do |line|
line.split("\n").each{|n| matrix << n }
end
end
matrix
end


def load_labels(filename)
vector = []
File.open(filename) do |file|
file.each_line do |line|
vector << line.to_f
end
end
vector = NArray.to_na(vector)
def load_numbers(filename)
matrix = []
File.open(filename) do |file|
file.each_line do |line|
ary = []
line.split(" ").each{ |n| ary << n.to_f }
matrix << ary
end
end
matrix = NArray.to_na(matrix)
matrix = matrix.transpose(1,0)
end

def load_dna(filename)
puts 'loading dna'
matrix=[]
File.open(filename) do |file|
file.each_line do |line|
line.split("\n").each{|n| matrix << n }
end
end
matrix
end


def load_cubes(filename)
matrix=[]
File.open(filename) do |file|
file.each_line do |line|
line.split("\n").each{|n| matrix << n }
end
end
matrix
end


def load_labels(filename)
vector = []
File.open(filename) do |file|
file.each_line do |line|
vector << line.to_f
end
end
vector = NArray.to_na(vector)
# loading vectors is not yet implemented,
# use a 1D array instead (for now)
#vector = vector.reshape(1, vector.total)
#vector = vector.reshape(1, vector.total)
end
extend self
end

def randn mean = 0.0, std_dev = 1.0
Modshogun::Math.normal_random mean.to_f, std_dev.to_f
end

# some stuff added to the Modshogun:: namespace

Modshogun::Math.class_eval do
def rand
# do something later
end
end



Numeric.class_eval do
def sign
return -1 if self < 0
return 0 if self == 0
return 1 if self > 0
end
end

module ArrayHelpers
def sign
a = []
self.each do |x|
a << x.sign
end
a
end

def eql_items? other
raise(ArgumentError, "Arrays dont' have the same number of elements") if self.size != other.size
output = []
self.each_with_index do |x, i|
output[i] = x == other[i]
end
return output
end

# implement like numpy diag
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.diag.html?highlight=diag#numpy.diag
def diag
end
end

NArray.class_eval do
include ArrayHelpers
end

Array.class_eval do
include ArrayHelpers
end

# maybe include some of the following in the Modshogun:: namespace proper...

# yes this is a very mean & dirty mean alg...
# no checking of stuff, can give wonky errors, but hey!
# i'm being lazy & not doing cool metaprogramming type stuff for now...
def mean ary
num_items = ary.size
tot = ary.inject(0) do |sum, n|
if n == true
sum + 1
elsif (n == false) or (n == nil)
sum + 0 # yes it's a dummy
else
sum + n
end
end
tot.to_f / num_items.to_f
end

# some array generating methods
def gen_pos_ones_vec num
ary = []
num.times do
ary << 1
end
return ary
end

# some label generating method
def gen_labels_vec num
num=num/2
ary = []
num.times do
ary << 1
end
num.times do
ary << -1
end
return ary
end

# 2 high, num wide random arrays, concatenated together [] + []
# randn - dist, randn + dist
def gen_rand_ary num, dist=0
ary = [[],[]]
ary.each do |p|
p << ary_fill( dist ) + ary_fill( -dist )
p.flatten!
end
return ary
end

def ary_fill num, dist=0
ary = []
num.times do
ary << randn + dist
end
return ary
end

0 comments on commit 52fb28d

Please sign in to comment.