@Test(expected = Test.None.class) public void testAdult() throws IOException { System.out.println("adult"); MathEx.setSeed(19650218); Dataset<Instance<SparseArray>> train = Read.libsvm(smile.util.Paths.getTestData("libsvm/data_lasvm_adult_adult.trn")); Dataset<Instance<SparseArray>> test = Read.libsvm(smile.util.Paths.getTestData("libsvm/data_lasvm_adult_adult.tst")); int n = train.size(); int[][] x = new int[n][]; int[] y = new int[n]; for (int i = 0; i < n; i++) { Instance<SparseArray> sample = train.get(i); x[i] = new int[sample.x().size()]; int j = 0; for (SparseArray.Entry e : sample.x()) { x[i][j++] = e.i + 1; } y[i] = sample.label(); } n = test.size(); int[][] testx = new int[n][]; int[] testy = new int[n]; for (int i = 0; i < n; i++) { Instance<SparseArray> sample = test.get(i); testx[i] = new int[sample.x().size()]; int j = 0; for (SparseArray.Entry e : sample.x()) { testx[i][j++] = e.i + 1; } testy[i] = sample.label(); } BinarySparseGaussianKernel kernel = new BinarySparseGaussianKernel(31.6); Classifier<int[]> model = SVM.fit(x, y, kernel, 100, 1E-3); int[] prediction = Validation.test(model, testx); int error = Error.of(testy, prediction); System.out.format("Test Error = %d, Accuracy = %.2f%%%n", error, 100.0 - 100.0 * error / testx.length); assertEquals(2451, error); }
public static Classifier<double[]> fit(double[][] x, int[] y, double C, double tol) { LASVM<double[]> lasvm = new LASVM<>(new LinearKernel(), C, tol); KernelMachine<double[]> svm = lasvm.fit(x, y); return new Classifier<double[]>() { LinearKernelMachine model = LinearKernelMachine.of(svm); @Override public int predict(double[] x) { return model.f(x) > 0 ? +1 : -1; } }; }
SVM extends KernelMachine<T> implements Classifier<T> { public static Classifier<double[]> fit(double[][] x, int[] y, double C, double tol) { LASVM<double[]> lasvm = new LASVM<>(new LinearKernel(), C, tol); KernelMachine<double[]> svm = lasvm.fit(x, y); return new Classifier<double[]>() { LinearKernelMachine model = LinearKernelMachine.of(svm); @Override public int predict(double[] x) { return model.f(x) > 0 ? +1 : -1; } }; } }
SVM extends KernelMachine<T> implements Classifier<T> { public static Classifier<double[]> fit(double[][] x, int[] y, double C, double tol) { LASVM<double[]> lasvm = new LASVM<>(new LinearKernel(), C, tol); KernelMachine<double[]> svm = lasvm.fit(x, y); return new Classifier<double[]>() { LinearKernelMachine model = LinearKernelMachine.of(svm); @Override public int predict(double[] x) { return model.f(x) > 0 ? +1 : -1; } }; } SVM(MercerKernel<T> kernel, T[] instances, double[] weight, double b); }
SVM extends KernelMachine<T> implements Classifier<T> { public static Classifier<double[]> fit(double[][] x, int[] y, double C, double tol) { LASVM<double[]> lasvm = new LASVM<>(new LinearKernel(), C, tol); KernelMachine<double[]> svm = lasvm.fit(x, y); return new Classifier<double[]>() { LinearKernelMachine model = LinearKernelMachine.of(svm); @Override public int predict(double[] x) { return model.f(x) > 0 ? +1 : -1; } }; } SVM(MercerKernel<T> kernel, T[] instances, double[] weight, double b); @Override int predict(T x); static Classifier<double[]> fit(double[][] x, int[] y, double C, double tol); static Classifier<int[]> fit(int[][] x, int[] y, int p, double C, double tol); static Classifier<SparseArray> fit(SparseArray[] x, int[] y, int p, double C, double tol); static SVM<T> fit(T[] x, int[] y, MercerKernel<T> kernel, double C, double tol); }
SVM extends KernelMachine<T> implements Classifier<T> { public static Classifier<double[]> fit(double[][] x, int[] y, double C, double tol) { LASVM<double[]> lasvm = new LASVM<>(new LinearKernel(), C, tol); KernelMachine<double[]> svm = lasvm.fit(x, y); return new Classifier<double[]>() { LinearKernelMachine model = LinearKernelMachine.of(svm); @Override public int predict(double[] x) { return model.f(x) > 0 ? +1 : -1; } }; } SVM(MercerKernel<T> kernel, T[] instances, double[] weight, double b); @Override int predict(T x); static Classifier<double[]> fit(double[][] x, int[] y, double C, double tol); static Classifier<int[]> fit(int[][] x, int[] y, int p, double C, double tol); static Classifier<SparseArray> fit(SparseArray[] x, int[] y, int p, double C, double tol); static SVM<T> fit(T[] x, int[] y, MercerKernel<T> kernel, double C, double tol);  }