Skip to content

Instantly share code, notes, and snippets.

@hannic
Created December 2, 2012 19:51
Show Gist options
  • Save hannic/4190696 to your computer and use it in GitHub Desktop.
Save hannic/4190696 to your computer and use it in GitHub Desktop.
distance matrix to kernel matrix for libsvm
#!/usr/bin/ruby
#
if ARGV.length==0 then
puts "Usage #{$0} inputmatrix classlabels trainlabels gamma"
exit
end
@maxclasses=1000
gamma=2**ARGV[2].to_f
rowNumber = 0
File.open(ARGV[2]){|f| @classlabels=f.read.split}
File.open(ARGV[0]) do |matrix|
File.open(ARGV[1]) do |labels|
eof=false
while !eof do
row=matrix.gets
label=labels.gets
eof=true if !row or !label
if !eof then
elems=row.split
if label.to_i < @maxclasses then
if rowNumber < elems.size then
rowNumber += 1
print label.chomp," 0:",rowNumber
end
elems.length.times { |i| print " #{i+1}:#{Math.exp(-gamma*elems[i].to_f)}" if(@classlabels[i].to_i< @maxclasses) }
puts
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment