data[i][0]=-1.0-(a-b);
data[i][1]=-1.0-(a+b);
}
// Linear kernel
Svmlinkernel linkernel=new Svmlinkernel(data,y);
Svm linsvm=new Svm(linkernel);
lambda=10;
k=0;
do {
test=linsvm.relax(lambda,omega);
// System.out.printf(test);
k++;
} while (test > 1.e-3 && k < 100);
int nerror=0;
for (i=0;i<M;i++) {
// if (i%10 == 0) System.out.printf((y[i]==1.0) << " %f\n", (linsvm.predict(i) >= 1.0));
// if ((y[i] == 1.0) != (linsvm.predict(i) >= 1.0))
// System.out.printf(data[i][0] << " %f\n", data[i][1] << " %f\n", y[i] << " %f\n", linsvm.predict(i));
nerror += ((y[i]==1.0) != (linsvm.predict(i) >= 0.0) ? 1 : 0);
}
System.out.printf("Errors: %d\n", nerror);
// Polynomial kernel
Svmpolykernel polykernel=new Svmpolykernel(data,y,1.0,1.0,2.0);
Svm polysvm=new Svm(polykernel);
lambda=10;
k=0;
do {
test=polysvm.relax(lambda,omega);
// System.out.printf(test);
k++;
} while (test > 1.e-3 && k < 100);
nerror=0;
for (i=0;i<M;i++) {
nerror += ((y[i]==1.0) != (polysvm.predict(i) >= 0.0) ? 1 : 0);
}
System.out.printf("Errors: %d\n", nerror);
// Gaussian kernel
Svmgausskernel gausskernel=new Svmgausskernel(data,y,1.0);
Svm gausssvm=new Svm(gausskernel);
lambda=10;
k=0;
do {
test=gausssvm.relax(lambda,omega);
// System.out.printf(test);
k++;
} while (test > 1.e-3 && k < 100);
nerror=0;
for (i=0;i<M;i++) {
nerror += ((y[i]==1.0) != (gausssvm.predict(i) >= 0.0) ? 1 : 0);
}
System.out.printf("Errors: %d\n", nerror);
// Need to add tests for harder test case and resolve issue that the two
// support vectors give an erroneous indication for two of the kernels above
// Example similar to the book
Normaldev ndev=new Normaldev(0.0,0.5,17);
for (j=0;j<4;j++) { // Four quadrants
for (i=0;i<M/4;i++) {
k=(M/4)*j+i;
if (j == 0) {
y[k]=1.0;
data[k][0]=1.0+ndev.dev();
data[k][1]=1.0+ndev.dev();
} else if (j == 1) {
y[k]=-1.0;
data[k][0]=-1.0+ndev.dev();
data[k][1]=1.0+ndev.dev();
} else if (j == 2) {
y[k]=1.0;
data[k][0]=-1.0+ndev.dev();
data[k][1]=-1.0+ndev.dev();
} else {
y[k]=-1.0;
data[k][0]=1.0+ndev.dev();
data[k][1]=-1.0+ndev.dev();
}
}
}
// Linear kernel
Svmlinkernel linkernel2=new Svmlinkernel(data,y);
Svm linsvm2=new Svm(linkernel2);
System.out.printf("Errors: ");
for (lambda=0.001;lambda<10000;lambda *= 10) {
k=0;
do {