forked from ewalker544/libsvm-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictor.go
152 lines (127 loc) · 3.89 KB
/
predictor.go
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
/*
** Copyright 2014 Edward Walker
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http ://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** Description: Prediciton related APIs
** @author: Ed Walker
*/
package libSvm
/**
* This function gives decision values on a test vector x given a
model, and return the predicted label (classification) or
the function value (regression).
For a classification model with nrClass classes, this function
gives nrClass*(nrClass-1)/2 decision values in the slice
decisionValues. The order is label[0] vs. label[1], ...,
label[0] vs. label[nr_class-1], label[1] vs. label[2], ...,
label[nrClass-2] vs. label[nrClass-1]. The returned returnValue is
the predicted class for x. Note that when nrClass = 1, this
function does not give any decision value.
For a regression model, decisionValues[0] and the returned returnValue are
both the function value of x calculated using the model. For a
one-class model, decisionValues[0] is the decision value of x, while
the returned returnValue is +1/-1.
*/
func (model Model) PredictValues(x map[int]float64) (returnValue float64, decisionValues []float64) {
returnValue = 0
px := MapToSnode(x)
switch model.param.SvmType {
case ONE_CLASS, EPSILON_SVR, NU_SVR:
var svCoef []float64 = model.svCoef[0]
var sum float64 = 0
for i := 0; i < model.l; i++ {
var idx_y int = model.sV[i]
py := model.svSpace[idx_y:]
sum += svCoef[i] * computeKernelValue(px, py, model.param)
}
sum -= model.rho[0]
decisionValues = append(decisionValues, sum)
if model.param.SvmType == ONE_CLASS {
if sum > 0 {
returnValue = 1 // return
} else {
returnValue = -1 // return
}
return // returnValue, decisionValues
} else {
returnValue = sum
return // returnValue, decisionValues
}
case C_SVC, NU_SVC:
var nrClass int = model.nrClass
var l int = model.l
kvalue := make([]float64, l)
for i := 0; i < l; i++ {
var idx_y int = model.sV[i]
py := model.svSpace[idx_y:]
kvalue[i] = computeKernelValue(px, py, model.param)
}
start := make([]int, nrClass)
start[0] = 0
for i := 1; i < nrClass; i++ {
start[i] = start[i-1] + model.nSV[i-1]
}
vote := make([]int, nrClass)
for i := 0; i < nrClass; i++ {
vote[i] = 0
}
var p int = 0
for i := 0; i < nrClass; i++ {
for j := i + 1; j < nrClass; j++ {
var sum float64 = 0
var si int = start[i]
var sj int = start[j]
var ci int = model.nSV[i]
var cj int = model.nSV[j]
coef1 := model.svCoef[j-1]
coef2 := model.svCoef[i]
for k := 0; k < ci; k++ {
sum += coef1[si+k] * kvalue[si+k]
}
for k := 0; k < cj; k++ {
sum += coef2[sj+k] * kvalue[sj+k]
}
sum -= model.rho[p]
decisionValues = append(decisionValues, sum)
if sum > 0 {
vote[i]++
} else {
vote[j]++
}
p++
}
}
var maxIdx int = 0
for i := 1; i < nrClass; i++ {
if vote[i] > vote[maxIdx] {
maxIdx = i
}
}
returnValue = float64(model.label[maxIdx])
return // returnValue, decisionValues
}
return
}
/**
* This function does classification or regression on a test vector x
given a model.
For a classification model, the predicted class for x is returned.
For a regression model, the function value of x calculated using
the model is returned. For an one-class model, +1 or -1 is
returned.
*/
func (model Model) Predict(x map[int]float64) float64 {
predict, _ := model.PredictValues(x)
return predict
}