Top Level Namespace
Defined Under Namespace
Classes: Array, Hash, LIBSVMdata, SVMFeature, SVMFeaturesConfig, SVMLab, SVMLabConfig, SVMPrediction
Constant Summary collapse
- ANSI_RESET =
IRB.conf[:SVMLab] = { # name of prompt mode
:PROMPT_I => "SVMLab:%03n:%i> ", # normal prompt :PROMPT_S => "SVMLab:%03n:%i%l ", # prompt for continuing strings :PROMPT_C => "SVMLab:%03n:%i* ", # prompt for continuing statement :RETURN => " ==>%s\n" # format to return value
}
"\033[0m"
- ANSI_GREEN =
"\033[2;32m"
- ANSI_BLUE =
"\033[2;34m"
- ANSI_BOLD =
Sebastian Delmont Pretty print methods
"\033[1m"
- ANSI_LGRAY =
ANSI_RESET = “033[0m”
"\033[0;37m"
- ANSI_GRAY =
"\033[1;30m"
Instance Method Summary collapse
- #correlation(x, y) ⇒ Object
-
#genericplot(plotdata, file, title = 'Plot', xtitle = 'X', ytitle = 'Y') ⇒ Object
Each should be an array giving more than one plot.
- #method?(arg) ⇒ Boolean
-
#pm(obj, *options) ⇒ Object
Print methods.
-
#predplot(predarr, legends = [], title = 'SVM Prediction', err = nil, file = '') ⇒ Object
— predplot — PredictionPlot: Plots true value on the X axis vs.
Instance Method Details
#correlation(x, y) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/arraymethods.rb', line 57 def correlation(x, y) xymean = (x * y) / x.size.to_f sx = x.stddev sy = y.stddev (xymean - x.mean * y.mean) / (sx * sy) end |
#genericplot(plotdata, file, title = 'Plot', xtitle = 'X', ytitle = 'Y') ⇒ Object
Each should be an array giving more than one plot
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/svmlab-plot.rb', line 8 def genericplot(plotdata, file, title='Plot', xtitle='X', ytitle='Y') Gnuplot.open do |gp| # This could be either a file or the gnuplot process that we pipe to Gnuplot::Plot.new( gp ) do |plot| plot.title title plot.xlabel xtitle plot.ylabel ytitle plot.set "grid" if file =~ /(png)|(ps)$/ # Remember to add following line to your .baschrc file : # export GDFONTPATH=/usr/share/fonts/truetype/ttf-bitstream-vera/ plot.terminal "png size 800,600 font Vera 16" if file =~ /png$/ #plot.terminal "png size 800,600 large" if file =~ /png$/ plot.terminal "postscript color \"Helvetica\" 16" if file =~ /ps$/ plot.output file end plot.data = plotdata end end end |
#method?(arg) ⇒ Boolean
6 7 8 9 10 11 12 13 |
# File 'lib/svmlab-config.rb', line 6 def method?(arg) begin method(arg) true rescue false end end |
#pm(obj, *options) ⇒ Object
Print methods
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/svmlab-irb.rb', line 38 def pm(obj, *) # Print methods methods = obj.methods methods -= Object.methods unless .include? :more filter = .select {|opt| opt.kind_of? Regexp}.first methods = methods.select {|name| name =~ filter} if filter data = methods.sort.collect do |name| method = obj.method(name) if method.arity == 0 args = "()" elsif method.arity > 0 n = method.arity args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})" elsif method.arity < 0 n = -method.arity args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)" end klass = $1 if method.inspect =~ /Method: (.*?)#/ [name, args, klass] end max_name = data.collect {|item| item[0].size}.max max_args = data.collect {|item| item[1].size}.max data.each do |item| print " #{ANSI_BOLD}#{item[0].rjust(max_name)}#{ANSI_RESET}" print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}" print " #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n" end data.size end |
#predplot(predarr, legends = [], title = 'SVM Prediction', err = nil, file = '') ⇒ Object
— predplot — PredictionPlot: Plots true value on the X axis vs. predicted value on the Y axis.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/svmlab-plot.rb', line 30 def predplot(predarr, legends = [], title = 'SVM Prediction', err = nil, file = '') predarr = [predarr] if !predarr.is_a? Array dataarr = predarr.map do |predictions| x, y = predictions.inject([[],[]]) { |data,(example,val)| data[0] << val['truth'] data[1] << val['pred'] data } end from = dataarr.inject(dataarr[0][0][0]) { |m,a| [m, a[0].min, a[1].min].min }.floor to = dataarr.inject(dataarr[0][0][0]) { |m,a| [m, a[0].max, a[1].max].max }.ceil sampleindex = 0 # Fiddling with legends legends = dataarr.map{|d| "Sample #{sampleindex+=1}"} if legends.size==0 if err legends = legends.zip(predarr).map { | legend, pred | begin #args = if err.split(/,/).size==1 then 'pred' # else (['pred'] + err.split(/,/)[1..-1]).join(',') end #legend + " (#{err} = ".upcase + "%.2f"%eval("#{err.split(/,/)[0].downcase}(#{args})") + ")" legend + " (#{err} = ".upcase + "%.2f"%eval("pred.#{err}") + ')' rescue legend raise end } end # Setting plotdata plotdata = [ Gnuplot::DataSet.new( dataarr.first ) { |ds| ds.using = '1:2' ds.with = "points" ds.title = legends.first ds.linewidth = 2 ds.matrix = nil } ] + [ Gnuplot::DataSet.new( [[from,to], [from,to]] ) { |ds| ds.using = '1:2' ds.with = "lines" ds.title = "Correct diagonal" ds.linewidth = 1 ds.matrix = nil } ] + dataarr[1..-1].zip(legends[1..-1]).inject([]) { |arr,((x,y),legend)| arr.push(Gnuplot::DataSet.new( [x,y] ) { |ds| ds.using = '1:2' ds.with = "points" ds.title = legend ds.linewidth = 2 ds.matrix = nil }) } genericplot(plotdata, file, title, 'Experimental value', 'Predicted value') nil end |