This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.rb
114 lines (93 loc) · 2.2 KB
/
predict.rb
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require_relative 'models'
require_relative 'county'
require 'libsvm'
FINAL_YEAR = 2012
TRAIN = 10
TEST = 0
LIMIT = TRAIN + TEST
X = []
Y = []
counties = []
keys = []
# TODO Normalise y by pop
# TODO Intervention cost as X input
sql = "SELECT name FROM county_regions LIMIT #{LIMIT};"
results = DataMapper.repository(:default).adapter.select(sql)
results.shuffle!
results.each do |name|
puts "County: #{name}"
county = County.new name
resp = county.flat_output FINAL_YEAR
x = []
y = nil
resp.each do |key, value|
key = key.to_s
value = value.to_f
if key == "alcohol_cost_expected" or key == "alcohol_cost_unit"
next
elsif key == "admission_episodes_0"
y = value
# elsif key == "alcohol_cost_actual"
# y = value
else
puts key
x << value
end
end
X << x
Y << y
counties << name
end
X.shuffle!
Xtrain = X[0...TRAIN].map { |ary| Libsvm::Node.features(ary) }
Ytrain = Y[0...TRAIN]
countiestrain = counties[0...TRAIN]
Xtest = X[TRAIN...TRAIN+TEST].map { |ary| Libsvm::Node.features(ary) }
Ytest = Y[TRAIN...TRAIN+TEST]
countiestest = Y[TRAIN...TRAIN+TEST]
problem = Libsvm::Problem.new
parameter = Libsvm::SvmParameter.new
parameter.cache_size = 100
parameter.eps = 1
parameter.c = 100
problem.set_examples(Ytrain, Xtrain)
$model = Libsvm::Model.train(problem, parameter)
# Xtest.zip(Ytest, countiestest).map do |x, y, county|
# puts county
# pred = model.predict(x)
# puts ((pred-y).abs)/y
# end
class PredictCounty
def initialize county_name
county = County.new county_name
resp = county.flat_output 2013
x = []
y = nil
@ac_ind = 0
resp.each do |key, value|
key = key.to_s
value = value.to_f
@ac_ind = x.count if key == "alcohol_cost_actual"
if key == "alcohol_cost_expected" or key == "alcohol_cost_unit"
next
elsif key == "admission_episodes_0"
y = value
else
x << value
end
end
@y = y
@x = x
@cy = $model.predict(Libsvm::Node.features(@x))
end
def predict num
puts num.inspect
x = @x.clone
x[9] = num
{
current: @cy,
predicted: $model.predict(Libsvm::Node.features(x)),
from_data: @y
}
end
end