-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml.rb
252 lines (243 loc) · 8.68 KB
/
ml.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#Instalar a gem rb-libsvm
#$ sudo apt-get install libsvm2 libsvm-dev libsvm-tools
require 'libsvm'
# This library is namespaced.
problem = Libsvm::Problem.new
parameter = Libsvm::SvmParameter.new
parameter.cache_size = 10 # in megabytes
parameter.nu = Libsvm::SvmType::NU_SVC
parameter.eps = 0.001
parameter.c = 10
examples = [
[1,62444,0.6293,0.581,0.4991],
[1,69024,0.6535,0.6201,0.6019],
[1,107010,0.6151,0.6214,0.5675],
[1,46804,0.565,0.5875,0.5558],
[1,170183,0.574,0.6121,0.63],
[1,6799,0.5154,0.6301,0.499],
[1,7302,0.577,0.573,0.5414],
[1,272726,0.5914,0.6391,0.6575],
[2,70417,0.5965,0.6231,0.533000037243947],
[2,20892,0.5708,0.6232,0.5625],
[2,27817,0.5939,0.6224,0.5043],
[2,149803,0.5772,0.6516,0.5826],
[2,844245,0.6171,0.6514,0.6463],
[2,16288,0.4331,0.4984,0.4186],
[2,26134,0.5175,0.5602,0.5564],
[2,20687,0.506,0.5534,0.5177],
[2,52310,0.5606,0.6042,0.4706],
[2,62473,0.5712,0.6496,0.542],
[2,21800,0.5244,0.5453,0.5133],
[2,70047,0.4893,0.6334,0.4991],
[2,353932,0.4931,0.5772,0.5133],
[2,19192,0.5027,0.5271,0.4206],
[2,101386,0.5522,0.5867,0.6281],
[2,38540,0.5104,0.5048,0.533000037243947],
[2,19724,0.5011,0.672999999999999,0.5425],
[2,40398,0.5312,0.5947,0.5863],
[2,221504,0.4423,0.5164,0.4776],
[2,123570,0.4666,0.5435,0.5124],
[2,25123,0.5326,0.5833,0.4515],
[2,12895,0.6364,0.6013,0.522],
[2,75018,0.4696,0.5326,0.5735],
[2,17468,0.5672,0.6387,0.537],
[2,26547,0.5179,0.6047,0.6651],
[2,54418,0.5561,0.5718,0.506],
[2,18136,0.5346,0.5592,0.5637],
[2,44887,0.5912,0.5996,0.6114],
[2,13355,0.5461,0.5171,0.5605],
[2,405072,0.5859,0.6346,0.6136],
[2,12144,0.512,0.5153,0.4693],
[2,134940,0.476,0.5084,0.4674],
[2,75462,0.5181,0.551,0.5205],
[2,154054,0.532,0.5929,0.5391],
[2,81054,0.521,0.6623,0.5667],
[2,347088,0.5422,0.5786,0.5614],
[2,136949,0.599,0.6125,0.6088],
[2,686122,0.5961,0.6469,0.6223],
[2,389494,0.5678,0.6053,0.6086],
[2,1617183,0.6894,0.6789,0.6739],
[2,46659,0.5205,0.6041,0.5685],
[2,25047,0.5168,0.6464,0.5554],
[2,101485,0.4661,0.5246,0.5478],
[2,35367,0.6111,0.6185,0.5131],
[2,135805,0.5537,0.5682,0.5526],
[2,47820,0.6244,0.6889,0.5653],
[2,102257,0.5069,0.5565,0.5154],
[2,153918,0.5704,0.6357,0.621],
[2,69526,0.6048,0.6381,0.5633],
[2,69255,0.5226,0.5828,0.7113],
[2,52531,0.5993,0.6662,0.6237],
[2,33268,0.5197,0.6035,0.5597],
[2,14667,0.4461,0.5568,0.5314],
[2,26855,0.5108,0.5407,0.4727],
[2,34478,0.5391,0.5693,0.5194],
[2,617528,0.6079,0.6176,0.6212],
[2,180213,0.5875,0.6435,0.6413],
[2,9225,0.6354,0.5997,0.4865],
[2,161528,0.5659,0.5939,0.6361],
[2,14488,0.5144,0.5098,0.482],
[2,145431,0.5693,0.6342,0.6613],
[2,2921087,0.6449,0.6569,0.6576],
[3,19307,0.5774,0.6535,0.6294],
[3,25694,0.5328,0.57,0.5633],
[3,116267,0.4786,0.5519,0.5337],
[3,2502557,0.6106,0.6203,0.6107],
[3,417307,0.484,0.5234,0.5332],
[3,28163,0.4269,0.5376,0.5438],
[3,11170,0.4259,0.578,0.4531],
[3,15186,0.4052,0.5297,0.568],
[3,33463,0.4904,0.6304,0.5787],
[3,19971,0.5448,0.6254,0.6569],
[3,9985,0.5397,0.6011,0.533000037243947],
[3,74171,0.512,0.5553,0.5881],
[3,7072,0.6067,0.5543,0.5471],
[3,27827,0.4053,0.4653,0.5252],
[3,6895,0.4478,0.6192,0.5328],
[3,230848,0.4797,0.5333,0.5388],
[3,19464,0.5237,0.5382,0.5447],
[3,257345,0.5236,0.5572,0.5524],
[3,23585,0.5228,0.653,0.6808],
[3,91453,0.4924,0.5514,0.5168],
[3,103333,0.5093,0.5769,0.5768],
[3,37700,0.4713,0.5924,0.5638],
[3,78583,0.496,0.5236,0.5047],
[3,555284,0.5655,0.5772,0.573],
[3,3298,0.4631,0.4648,0.5859],
[3,6591,0.4603,0.5341,0.533000037243947],
[3,19622,0.6867,0.5776,0.6547],
[3,86844,0.5063,0.6,0.5698],
[3,47937,0.4979,0.521,0.5735],
[3,394350,0.5391,0.6164,0.6139],
[3,107263,0.5167,0.557,0.6152],
[3,89859,0.3606,0.6135,0.4349],
[3,62951,0.4761,0.5684,0.5646],
[3,10818,0.4774,0.5462,0.5105],
[3,56229,0.5515,0.6103,0.6199],
[3,34075,0.4741,0.581,0.5364],
[3,322659,0.408,0.4483,0.4536],
[3,25630,0.4481,0.5292,0.5705],
[3,216254,0.4495,0.4792,0.4685],
[3,15982,0.6193,0.67,0.593],
[3,31604,0.4314,0.5192,0.533000037243947],
[3,27752,0.4941,0.4972,0.5372],
[3,3834,0.4981,0.5351,0.533000037243947],
[3,232107,0.5353,0.5758,0.5672],
[3,141046,0.5853,0.6073,0.6342],
[3,87542,0.4893,0.5335,0.5266],
[3,111012,0.5044,0.5481,0.5497],
[3,12558,0.4113,0.5767,0.533000037243947],
[3,322126,0.5052,0.5663,0.5401],
[3,662362,0.5122,0.5639,0.5296],
[3,95056,0.5091,0.602,0.5887],
[3,208702,0.5301,0.5474,0.5698],
[3,381802,0.4728,0.5205,0.484],
[3,119802,0.5923,0.5953,0.5592],
[3,163662,0.5243,0.6093,0.5949],
[3,36328,0.6072,0.5537,0.5661],
[3,485376,0.491,0.5369,0.5108],
[3,472762,0.5681,0.5767,0.5261],
[3,355875,0.6124,0.6083,0.5835],
[3,96865,0.5234,0.5289,0.5412],
[3,882729,0.4875,0.5241,0.474],
[3,22779,0.5004,0.625,0.5857],
[3,807492,0.5141,0.526,0.4815],
[3,1038081,0.461,0.4863,0.4714],
[3,73725,0.5243,0.5524,0.5803],
[3,57250,0.5204,0.5889,0.5251],
[3,8773,0.4387,0.6228,0.4789],
[3,77627,0.508,0.5652,0.4953],
[3,113317,0.5202,0.5589,0.5453],
[3,34110,0.4302,0.5138,0.5559],
[3,81082,0.5396,0.5589,0.5335],
[3,127006,0.472,0.4909,0.4373],
[3,311230,0.5191,0.5289,0.487],
[3,1324781,0.5345,0.5457,0.4888],
[3,167095,0.5398,0.5719,0.5012],
[3,17851,0.5338,0.6102,0.5397],
[3,19359,0.4947,0.6338,0.4894],
[3,299261,0.5108,0.5717,0.4959],
[3,43535,0.4855,0.5722,0.5933],
[3,433966,0.5624,0.536,0.5218],
[3,442548,0.5081,0.5607,0.5035],
[3,11967825,0.6453,0.6182,0.5706],
[3,56587,0.4679,0.5628,0.5172],
[4,15612,0.4741,0.58,0.5517],
[4,13470,0.4986,0.5636,0.5339],
[4,30859,0.4823,0.6042,0.5906],
[4,86773,0.4805,0.5699,0.5585],
[4,14144,0.5536,0.5609,0.5549],
[4,21864,0.4784,0.5471,0.626],
[4,108218,0.495,0.5443,0.5496],
[5,24414,0.4802,0.5459,0.5186],
[5,47162,0.5622,0.6519,0.6124],
[5,21047,0.5452,0.6118,0.5717],
[5,28437,0.5054,0.5851,0.6276],
[5,13731,0.5023,0.5748,0.5586],
[5,853622,0.572,0.6125,0.5914],
[5,5513,0.5368,0.5346,0.5418],
[5,108656,0.5589,0.6259,0.611],
[5,10965,0.5851,0.5969,0.5304],
[5,212870,0.5311,0.6186,0.5857],
[5,19220,0.5247,0.5826,0.6047],
[5,27104,0.5526,0.7859,0.6367],
[5,50893,0.487,0.5599,0.5348],
[5,4306,0.4063,0.5039,0.533000037243947],
[5,41495,0.5007,0.6323,0.6273],
[5,34776,0.5309,0.5796,0.6326],
[5,19462,0.4632,0.5562,0.5438],
[5,51355,0.5847,0.650999999999999,0.5929],
[5,17483,0.5147,0.6462,0.5206],
[5,113619,0.5099,0.5728,0.5366],
[5,6027,0.4681,0.5968,0.4218],
[5,20657,0.5827,0.5734,0.6389],
[5,58398,0.5905,0.5994,0.6043],
[5,31985,0.4589,0.6904,0.546],
[5,10364,0.4831,0.5192,0.533000037243947],
[5,11546,0.5445,0.6922,0.467],
[5,580489,0.6008,0.6442,0.6008],
[5,26401,0.5278,0.5493,0.5267],
[5,33610,0.5011,0.6112,0.5477],
[5,57285,0.4786,0.5398,0.6231],
[5,6128,0.4906,0.5505,0.533000037243947],
[5,57423,0.5437,0.5606,0.6156],
[5,22665,0.4993,0.456,0.533000037243947],
[5,80298,0.5556,0.6246,0.5935],
[5,94289,0.5671,0.6009,0.5809],
[5,7895,0.4258,0.5005,0.533000037243947],
[5,366491,0.5152,0.5763,0.5615],
[5,21478,0.4789,0.5865,0.4737],
[5,521910,0.4865,0.4708,0.4372],
[5,8746,0.517,0.5176,0.4617],
[5,23257,0.5119,0.5875,0.5235],
[5,8694,0.4454,0.5176,0.4786],
[5,11683,0.5152,0.5997,0.6286],
[5,18148,0.5321,0.5563,0.5727],
[5,12071,0.4933,0.5465,0.6141],
[5,21909,0.5312,0.5936,0.6109],
[5,19115,0.4911,0.5239,0.533000037243947],
[5,26373,0.513,0.4879,0.5167],
[5,39629,0.5588,0.6344,0.5915],
[5,26639,0.489,0.6118,0.5625],
[5,95998,0.5653,0.5925,0.6071],
[5,3024,0.4196,0.5241,0.5056],
[5,8224,0.4963,0.5376,0.6033],
[5,31129,0.5716,0.6281,0.529],
[5,10547,0.4197,0.5607,0.4765],
[5,24444,0.4966,0.557,0.5426],
[5,30703,0.4674,0.6025,0.5762],
[5,44798,0.5746,0.7099,0.5623],
[5,10969,0.4548,0.6833,0.4717],
[5,3095,0.4876,0.5492,0.5266],
[5,5527,0.441,0.5001,0.533000037243947],
[5,32491,0.4854,0.5719,0.4818],
[5,19110,0.5602,0.5899,0.4839],
[5,100367,0.4378,0.461,0.4116],
[5,117454,0.4343,0.4952,0.4849],
[5,39483,0.5751,0.5827,0.5753]
].map {|ary| Libsvm::Node.features(ary) }
labels = [4,2,4,4,4,4,4,4,4,4,4,3,2,3,1,3,3,3,4,4,3,2,3,3,1,4,2,3,2,4,2,4,4,3,4,4,4,4,3,4,4,1,4,4,4,2,3,3,2,4,4,4,1,4,3,4,4,3,4,4,4,2,2,4,2,4,3,4,4,1,4,4,4,3,3,4,4,3,4,4,4,4,4,4,4,2,4,1,4,3,1,4,1,1,4,4,4,4,4,2,1,4,2,4,4,4,4,4,4,2,2,4,4,4,4,1,2,4,4,4,2,4,1,4,3,4,1,3,4,2,4,4,3,1,4,3,4,3,1,2,1,2,1,1,1,1,1,2,2,2,1,2,3,2,2,4,2,2,2,2,4,4,4,4,4,2,2,4,3,4,2,4,4,4,4,3,4,4,3,3,4,2,3,2,4,4,3,4,3,4,4,4,1,3,1,4,3,4,1,1,2,3,3,2,4,3,4,1,2,4,4,4,4,3,2,1,3,4,4,2,4,4,4,3,4,1]
problem.set_examples(labels, examples)
model = Libsvm::Model.train(problem, parameter)
pred = model.predict(Libsvm::Node.features([5,14190,0.482]))
puts "Example [1, 1, 1] - Predicted #{pred}"