Class: LIBSVMdata

Inherits:
String
  • Object
show all
Defined in:
lib/libsvmdata.rb

Overview

upon creation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, readonly = true, classborder = nil) ⇒ LIBSVMdata

Returns a new instance of LIBSVMdata.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/libsvmdata.rb', line 12

def initialize(path, readonly = true, classborder = nil)
  @readonly = readonly
  @data = open(path).read
  @classborder = classborder
  if @readonly
    super(path)
  else
    @file = Tempfile.new('libsvmdata')
    super(@file.path)
    self.fillSparseData!
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/libsvmdata.rb', line 10

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/libsvmdata.rb', line 10

def path
  @path
end

Instance Method Details

#absmaxObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/libsvmdata.rb', line 82

def absmax
  max = Array.new(dim){0}
  max = 
    @data.split("\n").inject(max) { |s,line|
      pred,*feats = line.split
      s[0] = [s[0],pred.to_f.abs].max
      fs = s[1..-1].zip(feats).map{|si,fi|
        [si, fi.split(':').last.to_f.abs].max}
      #puts s; gets
      [s[0]] + fs }
end

#dimObject



25
26
27
# File 'lib/libsvmdata.rb', line 25

def dim
  @dim ||= @data.split("\n").first.split.size
end

#fillSparseData!Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/libsvmdata.rb', line 94

def fillSparseData!
  return if @readonly
  ndim = @data.split("\n").map{|line|
          if (arr=line.split).size>1
            arr.last.split(':').first.to_i
          else 0 end
        }.max
  @data = 
    @data.split("\n").map{|line|
      pred,*feats = line.split
      if @classborder
        pred = (pred.to_f>=@classborder) ? '1' : '-1'
      end
      counter = 0
      pred + ' ' +
      (1..ndim).map { |index|
        if (element = feats[counter]) and
          (element.split(':').first.to_i == index)
          counter+=1
          "#{index}:#{element.split(':').last}"
        else
            "#{index}:0"
        end
      }.join(' ') + "\n"
  }.join
  self.update
end

#meanObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/libsvmdata.rb', line 69

def mean
  sum = Array.new(dim){0}
  sum = 
    @data.split("\n").inject(sum) { |s,line|
      pred,*feats = line.split
      if !@classborder then s[0] += pred.to_f end
      fs = s[1..-1].zip(feats).map{|si,fi|
        si += fi.split(':').last.to_f}
      #puts s; gets
      [s[0]] + fs }
  sum.map{|i| i / nexamples.to_f}
end

#nexamplesObject



29
30
31
# File 'lib/libsvmdata.rb', line 29

def nexamples
  @nexamples ||= @data.split("\n").size
end

#scale!(vector) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/libsvmdata.rb', line 56

def scale!(vector)
  return if @readonly
  @data = 
    @data.split("\n").map { |line|
      pred,*feats = line.split
      (@classborder ? "#{pred} " : "#{(pred.to_f*vector[0])} ") +
      feats.map{|feat|
        key,val = feat.split(':')
        "#{key}:#{val.to_f*vector[key.to_i]}"}.join(' ') + "\n"
    }.join
  self.update
end

#translate!(vector) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/libsvmdata.rb', line 43

def translate!(vector)
  return if @readonly
  @data = 
    @data.split("\n").map { |line|
      pred,*feats = line.split
      (@classborder ? "#{pred} " : "#{(pred.to_f+vector[0])} ") +
      feats.map{|feat|
        key,val = feat.split(':')
        "#{key}:#{val.to_f+vector[key.to_i]}"}.join(' ') + "\n"
    }.join
  self.update
end

#updateObject



33
34
35
36
37
38
39
40
41
# File 'lib/libsvmdata.rb', line 33

def update
  return if @readonly
  @dim = nil
  @nexamples = nil
  open(self,'w') do |f|
    f.puts @data
  end
  self
end