@Test public void testClassify() { double [] positiveSample = {1.0,2.0,3.0}; double [] negativeSample = {7.0,8.0,9.0}; double [] malformedSample = {7.0,8.0,9.0,10.0}; FeatureVector positive = new FeatureVector(positiveSample, ClassLabel.UNKNOWN); FeatureVector negative = new FeatureVector(negativeSample, ClassLabel.UNKNOWN); FeatureVector malformed = new FeatureVector(malformedSample, ClassLabel.UNKNOWN); try { assertEquals("Test of NOT_CONFIGURED state of classifier failed", knnClassifier.classify(null), 0); fail("Failed to catch Illegal state exception"); } catch(Exception e) { } if (!knnClassifier.train(data)) fail("KnnClassifier.Classify failed in train"); assertEquals("Test of malformed FeatureVector for classify failed", knnClassifier.classify(malformed), 0); assertEquals("Test of known positive sample failed", knnClassifier.classify(positive), 1); assertEquals("Test of known negative sample failed", knnClassifier.classify(negative), -1); }
@Override public int classify(FeatureVector featureVector) throws IllegalStateException{ if (getState() == ClassifierState.NOT_TRAINED) throw new IllegalStateException("Classifier state not trained"); if (featureVector.size() != model.data.get(0).size()) { System.out.println("FeatureVector size mismatch in Classify"); return 0; } List<ComputedDistance> computedDistance = new ArrayList<ComputedDistance>(); for (FeatureVector fv : model.data) { double eucDistance = 0; for (int i = 0; i < model.numFeatures; i++) eucDistance += Math.pow(fv.get(i)- featureVector.get(i), 2); eucDistance = Math.sqrt(eucDistance); computedDistance.add(new ComputedDistance(fv.getIntClassLabel(), eucDistance)); } List<ComputedDistance> sortedDistance = sortDistances(computedDistance); return getMajorityLabel(sortedDistance); }
KNNClassifier extends Classifier { @Override public int classify(FeatureVector featureVector) throws IllegalStateException{ if (getState() == ClassifierState.NOT_TRAINED) throw new IllegalStateException("Classifier state not trained"); if (featureVector.size() != model.data.get(0).size()) { System.out.println("FeatureVector size mismatch in Classify"); return 0; } List<ComputedDistance> computedDistance = new ArrayList<ComputedDistance>(); for (FeatureVector fv : model.data) { double eucDistance = 0; for (int i = 0; i < model.numFeatures; i++) eucDistance += Math.pow(fv.get(i)- featureVector.get(i), 2); eucDistance = Math.sqrt(eucDistance); computedDistance.add(new ComputedDistance(fv.getIntClassLabel(), eucDistance)); } List<ComputedDistance> sortedDistance = sortDistances(computedDistance); return getMajorityLabel(sortedDistance); } }
KNNClassifier extends Classifier { @Override public int classify(FeatureVector featureVector) throws IllegalStateException{ if (getState() == ClassifierState.NOT_TRAINED) throw new IllegalStateException("Classifier state not trained"); if (featureVector.size() != model.data.get(0).size()) { System.out.println("FeatureVector size mismatch in Classify"); return 0; } List<ComputedDistance> computedDistance = new ArrayList<ComputedDistance>(); for (FeatureVector fv : model.data) { double eucDistance = 0; for (int i = 0; i < model.numFeatures; i++) eucDistance += Math.pow(fv.get(i)- featureVector.get(i), 2); eucDistance = Math.sqrt(eucDistance); computedDistance.add(new ComputedDistance(fv.getIntClassLabel(), eucDistance)); } List<ComputedDistance> sortedDistance = sortDistances(computedDistance); return getMajorityLabel(sortedDistance); } KNNClassifier(int k, int numFeatures);  KNNClassifier(Object objModel); }
KNNClassifier extends Classifier { @Override public int classify(FeatureVector featureVector) throws IllegalStateException{ if (getState() == ClassifierState.NOT_TRAINED) throw new IllegalStateException("Classifier state not trained"); if (featureVector.size() != model.data.get(0).size()) { System.out.println("FeatureVector size mismatch in Classify"); return 0; } List<ComputedDistance> computedDistance = new ArrayList<ComputedDistance>(); for (FeatureVector fv : model.data) { double eucDistance = 0; for (int i = 0; i < model.numFeatures; i++) eucDistance += Math.pow(fv.get(i)- featureVector.get(i), 2); eucDistance = Math.sqrt(eucDistance); computedDistance.add(new ComputedDistance(fv.getIntClassLabel(), eucDistance)); } List<ComputedDistance> sortedDistance = sortDistances(computedDistance); return getMajorityLabel(sortedDistance); } KNNClassifier(int k, int numFeatures);  KNNClassifier(Object objModel); @Override boolean train(List<FeatureVector> data); @Override int classify(FeatureVector featureVector); @Override Object getModel(); ArrayList<ComputedDistance> sortDistances(List<ComputedDistance> 
	computedDistance); int getMajorityLabel(List<ComputedDistance> sortedDistance ); int getK(); void setK(int k); int getNumFeatures(); boolean setNumFeatures(int numFeatures); }
KNNClassifier extends Classifier { @Override public int classify(FeatureVector featureVector) throws IllegalStateException{ if (getState() == ClassifierState.NOT_TRAINED) throw new IllegalStateException("Classifier state not trained"); if (featureVector.size() != model.data.get(0).size()) { System.out.println("FeatureVector size mismatch in Classify"); return 0; } List<ComputedDistance> computedDistance = new ArrayList<ComputedDistance>(); for (FeatureVector fv : model.data) { double eucDistance = 0; for (int i = 0; i < model.numFeatures; i++) eucDistance += Math.pow(fv.get(i)- featureVector.get(i), 2); eucDistance = Math.sqrt(eucDistance); computedDistance.add(new ComputedDistance(fv.getIntClassLabel(), eucDistance)); } List<ComputedDistance> sortedDistance = sortDistances(computedDistance); return getMajorityLabel(sortedDistance); } KNNClassifier(int k, int numFeatures);  KNNClassifier(Object objModel); @Override boolean train(List<FeatureVector> data); @Override int classify(FeatureVector featureVector); @Override Object getModel(); ArrayList<ComputedDistance> sortDistances(List<ComputedDistance> 
	computedDistance); int getMajorityLabel(List<ComputedDistance> sortedDistance ); int getK(); void setK(int k); int getNumFeatures(); boolean setNumFeatures(int numFeatures); public KNNModel model; }