@Test public void testClassify() { double [] positiveSample = {1.0,2.0,3.0}; double [] negativeSample = {10.0,9.0,9.0}; FeatureVector positive = new FeatureVector(positiveSample, ClassLabel.UNKNOWN); FeatureVector negative = new FeatureVector(negativeSample, ClassLabel.UNKNOWN); try { svmClassifier.classify(positive); fail("Test of NOT_CONFIGURED state of classifier failed"); } catch (Exception e) { } if(!svmClassifier.train(data)) fail("SVM train failed"); assertEquals("Test of known positive sample failed", svmClassifier.classify(positive), 1); assertEquals("Test of known negative sample failed", svmClassifier.classify(negative), -1); }
@Override public int classify(FeatureVector fv) throws IllegalStateException { if (this.model == null || getState() == ClassifierState.NOT_TRAINED) throw new IllegalArgumentException("Ivalid state of classifier"); svm_node[] nodes = new svm_node[this.numFeatures]; for (int i = 0; i < this.numFeatures; i++) { svm_node node = new svm_node(); node.index = i; node.value = fv.get(i, 0); nodes[i] = node; } int totalClasses = 2; int[] labels = new int[totalClasses]; svm.svm_get_labels(this.model, labels); double[] prob_estimates = new double[totalClasses]; svm.svm_predict_probability(this.model, nodes, prob_estimates); if (prob_estimates[0] >= prob_estimates[1]) return 1; else return -1; }
SVMClassifier extends Classifier { @Override public int classify(FeatureVector fv) throws IllegalStateException { if (this.model == null || getState() == ClassifierState.NOT_TRAINED) throw new IllegalArgumentException("Ivalid state of classifier"); svm_node[] nodes = new svm_node[this.numFeatures]; for (int i = 0; i < this.numFeatures; i++) { svm_node node = new svm_node(); node.index = i; node.value = fv.get(i, 0); nodes[i] = node; } int totalClasses = 2; int[] labels = new int[totalClasses]; svm.svm_get_labels(this.model, labels); double[] prob_estimates = new double[totalClasses]; svm.svm_predict_probability(this.model, nodes, prob_estimates); if (prob_estimates[0] >= prob_estimates[1]) return 1; else return -1; } }
SVMClassifier extends Classifier { @Override public int classify(FeatureVector fv) throws IllegalStateException { if (this.model == null || getState() == ClassifierState.NOT_TRAINED) throw new IllegalArgumentException("Ivalid state of classifier"); svm_node[] nodes = new svm_node[this.numFeatures]; for (int i = 0; i < this.numFeatures; i++) { svm_node node = new svm_node(); node.index = i; node.value = fv.get(i, 0); nodes[i] = node; } int totalClasses = 2; int[] labels = new int[totalClasses]; svm.svm_get_labels(this.model, labels); double[] prob_estimates = new double[totalClasses]; svm.svm_predict_probability(this.model, nodes, prob_estimates); if (prob_estimates[0] >= prob_estimates[1]) return 1; else return -1; } SVMClassifier(int numFeatures); }
SVMClassifier extends Classifier { @Override public int classify(FeatureVector fv) throws IllegalStateException { if (this.model == null || getState() == ClassifierState.NOT_TRAINED) throw new IllegalArgumentException("Ivalid state of classifier"); svm_node[] nodes = new svm_node[this.numFeatures]; for (int i = 0; i < this.numFeatures; i++) { svm_node node = new svm_node(); node.index = i; node.value = fv.get(i, 0); nodes[i] = node; } int totalClasses = 2; int[] labels = new int[totalClasses]; svm.svm_get_labels(this.model, labels); double[] prob_estimates = new double[totalClasses]; svm.svm_predict_probability(this.model, nodes, prob_estimates); if (prob_estimates[0] >= prob_estimates[1]) return 1; else return -1; } SVMClassifier(int numFeatures); void setSVMParameter(int probability, double gamma, 
			double nu, double C, int svmType, int kernelType,
			double cacheSize, double eps); @Override boolean train(List<FeatureVector> data); @Override int classify(FeatureVector fv); @Override Object getModel(); }
SVMClassifier extends Classifier { @Override public int classify(FeatureVector fv) throws IllegalStateException { if (this.model == null || getState() == ClassifierState.NOT_TRAINED) throw new IllegalArgumentException("Ivalid state of classifier"); svm_node[] nodes = new svm_node[this.numFeatures]; for (int i = 0; i < this.numFeatures; i++) { svm_node node = new svm_node(); node.index = i; node.value = fv.get(i, 0); nodes[i] = node; } int totalClasses = 2; int[] labels = new int[totalClasses]; svm.svm_get_labels(this.model, labels); double[] prob_estimates = new double[totalClasses]; svm.svm_predict_probability(this.model, nodes, prob_estimates); if (prob_estimates[0] >= prob_estimates[1]) return 1; else return -1; } SVMClassifier(int numFeatures); void setSVMParameter(int probability, double gamma, 
			double nu, double C, int svmType, int kernelType,
			double cacheSize, double eps); @Override boolean train(List<FeatureVector> data); @Override int classify(FeatureVector fv); @Override Object getModel(); public svm_model model; final double threshold; }