summaryrefslogtreecommitdiff
path: root/voted_test.rb
blob: c131ec26d4413cf5f3c39e9819fecc88a8176f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env ruby

require 'zipf'

STDOUT.sync = true

STDERR.write "reading test data...\n"
test = []
test_f = ReadFile.new ARGV[0]
n = 0
while i = test_f.gets
  test << SparseVector.from_kv(i.strip, '=', ' ')
  n += 1
  STDERR.write "#{n}\n" if n%1000==0
end
STDERR.write " test set size = #{test.size}\n"

errors = 0
ws = []
cs = []
ReadFile.readlines_strip(ARGV[1]).each { |l|
  c, s = l.split "\t"
  cs << c.to_i
  next if !s||s.strip==""
  ws << SparseVector.from_kv(s, "=", " ")
}

def sign(x)
  if x <= 0
    return -1.0
  else
    return 1.0
  end
end

test.each { |x|
  m = 0
  ws.each_with_index{ |w,j|
    m += sign(w.dot(x))*cs[j]
  }
  if m <= 0.0
    errors += 1
    puts -1
  else
    puts 1
  end
}

STDERR.write "accuracy = #{(test.size-errors)/test.size.to_f}\n"