-
Notifications
You must be signed in to change notification settings - Fork 5
/
LinearSVC.java
184 lines (160 loc) · 6.01 KB
/
LinearSVC.java
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
class SVC {
private enum Kernel { LINEAR, POLY, RBF, SIGMOID }
private int nClasses;
private int nRows;
private int[] classes;
private double[][] vectors;
private double[][] coefficients;
private double[] intercepts;
private int[] weights;
private Kernel kernel;
private double gamma;
private double coef0;
private double degree;
public SVC (int nClasses, int nRows, double[][] vectors, double[][] coefficients, double[] intercepts, int[] weights, String kernel, double gamma, double coef0, double degree) {
this.nClasses = nClasses;
this.classes = new int[nClasses];
for (int i = 0; i < nClasses; i++) {
this.classes[i] = i;
}
this.nRows = nRows;
this.vectors = vectors;
this.coefficients = coefficients;
this.intercepts = intercepts;
this.weights = weights;
this.kernel = Kernel.valueOf(kernel.toUpperCase());
this.gamma = gamma;
this.coef0 = coef0;
this.degree = degree;
}
public int predict(double[] features) {
double[] kernels = new double[vectors.length];
double kernel;
switch (this.kernel) {
case LINEAR:
// <x,x'>
for (int i = 0; i < this.vectors.length; i++) {
kernel = 0.;
for (int j = 0; j < this.vectors[i].length; j++) {
kernel += this.vectors[i][j] * features[j];
}
kernels[i] = kernel;
}
break;
case POLY:
// (y<x,x'>+r)^d
for (int i = 0; i < this.vectors.length; i++) {
kernel = 0.;
for (int j = 0; j < this.vectors[i].length; j++) {
kernel += this.vectors[i][j] * features[j];
}
kernels[i] = Math.pow((this.gamma * kernel) + this.coef0, this.degree);
}
break;
case RBF:
// exp(-y|x-x'|^2)
for (int i = 0; i < this.vectors.length; i++) {
kernel = 0.;
for (int j = 0; j < this.vectors[i].length; j++) {
kernel += Math.pow(this.vectors[i][j] - features[j], 2);
}
kernels[i] = Math.exp(-this.gamma * kernel);
}
break;
case SIGMOID:
// tanh(y<x,x'>+r)
for (int i = 0; i < this.vectors.length; i++) {
kernel = 0.;
for (int j = 0; j < this.vectors[i].length; j++) {
kernel += this.vectors[i][j] * features[j];
}
kernels[i] = Math.tanh((this.gamma * kernel) + this.coef0);
}
break;
}
int[] starts = new int[this.nRows];
for (int i = 0; i < this.nRows; i++) {
if (i != 0) {
int start = 0;
for (int j = 0; j < i; j++) {
start += this.weights[j];
}
starts[i] = start;
} else {
starts[0] = 0;
}
}
int[] ends = new int[this.nRows];
for (int i = 0; i < this.nRows; i++) {
ends[i] = this.weights[i] + starts[i];
}
if (this.nClasses == 2) {
for (int i = 0; i < kernels.length; i++) {
kernels[i] = -kernels[i];
}
double decision = 0.;
for (int k = starts[1]; k < ends[1]; k++) {
decision += kernels[k] * this.coefficients[0][k];
}
for (int k = starts[0]; k < ends[0]; k++) {
decision += kernels[k] * this.coefficients[0][k];
}
decision += this.intercepts[0];
if (decision > 0) {
return 0;
}
return 1;
}
double[] decisions = new double[this.intercepts.length];
for (int i = 0, d = 0, l = this.nRows; i < l; i++) {
for (int j = i + 1; j < l; j++) {
double tmp = 0.;
for (int k = starts[j]; k < ends[j]; k++) {
tmp += this.coefficients[i][k] * kernels[k];
}
for (int k = starts[i]; k < ends[i]; k++) {
tmp += this.coefficients[j - 1][k] * kernels[k];
}
decisions[d] = tmp + this.intercepts[d];
d++;
}
}
int[] votes = new int[this.intercepts.length];
for (int i = 0, d = 0, l = this.nRows; i < l; i++) {
for (int j = i + 1; j < l; j++) {
votes[d] = decisions[d] > 0 ? i : j;
d++;
}
}
int[] amounts = new int[this.nClasses];
for (int i = 0, l = votes.length; i < l; i++) {
amounts[votes[i]] += 1;
}
int classVal = -1, classIdx = -1;
for (int i = 0, l = amounts.length; i < l; i++) {
if (amounts[i] > classVal) {
classVal = amounts[i];
classIdx= i;
}
}
return this.classes[classIdx];
}
public static void main(String[] args) {
if (args.length == 2) {
// Features:
double[] features = new double[args.length];
for (int i = 0, l = args.length; i < l; i++) {
features[i] = Double.parseDouble(args[i]);
}
// Parameters:
double[][] vectors = {{3.0, 6.0}, {4.0, 8.0}};
double[][] coefficients = {{-0.4, 0.4}};
double[] intercepts = {7.0};
int[] weights = {1, 1};
// Prediction:
SVC clf = new SVC(2, 2, vectors, coefficients, intercepts, weights, "linear", 0.001, 0.0, 3);
int estimation = clf.predict(features);
System.out.println(estimation);
}
}
}