Method: #predplot

Defined in:
lib/svmlab-plot.rb

#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