use of smile.classification.SVM in project smile by haifengl.
the class SVMDemo method learn.
@Override
public double[][] learn(double[] x, double[] y) {
try {
gamma = Double.parseDouble(gammaField.getText().trim());
if (gamma <= 0) {
JOptionPane.showMessageDialog(this, "Invalid ˠ: " + gamma, "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Invalid ˠ: " + gammaField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
try {
C = Double.parseDouble(cField.getText().trim());
if (C < 0) {
JOptionPane.showMessageDialog(this, "Invalid C: " + C, "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Invalid C: " + cField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
SVM<double[]> svm = new SVM<>(new GaussianKernel(gamma), C);
svm.learn(data, label);
svm.finish();
for (int i = 0; i < label.length; i++) {
label[i] = svm.predict(data[i]);
}
double trainError = error(label, label);
System.out.format("training error = %.2f%%\n", 100 * trainError);
double[][] z = new double[y.length][x.length];
for (int i = 0; i < y.length; i++) {
for (int j = 0; j < x.length; j++) {
double[] p = { x[j], y[i] };
z[i][j] = svm.predict(p);
}
}
return z;
}
Aggregations