11package com .github .felipexw .classifiers .neighbors ;
22
33import com .github .felipexw .evaluations .EvaluatorMetric ;
4- import com .github .felipexw .metrics .SimilarityCalculator ;
4+ import com .github .felipexw .evaluations . metrics .SimilarityCalculator ;
55import com .github .felipexw .core .Instance ;
66import com .github .felipexw .core .LabeledInstance ;
77import com .github .felipexw .core .Prediction ;
1414/**
1515 * Created by felipe.appio on 23/08/2016.
1616 */
17- public class SimpleKNNClassifier extends KNNClassifier {
17+ public class SimpleKNNClassifier extends KNNClassifier {
1818
1919
2020 public SimpleKNNClassifier (SimilarityCalculator similarityCalculator ) {
@@ -40,12 +40,12 @@ public void train(List<LabeledInstance> instances, int k) {
4040 }
4141
4242 protected List <LabeledInstance > getInstancesthatMaximizeAccuracy () {
43- List <List <LabeledInstance >> partitionedInstances = Lists .partition (instances , instances .size ()/ k );
43+ List <List <LabeledInstance >> partitionedInstances = Lists .partition (instances , instances .size () / k );
4444 int testIndex = 0 ;
4545
4646 double [] accuraciesAndInstanceTestIndex = new double [k ];
4747
48- do {
48+ do {
4949 features = new HashMap <>();
5050 List <Instance > testIntances = new ArrayList <>();
5151
@@ -56,10 +56,8 @@ protected List<LabeledInstance> getInstancesthatMaximizeAccuracy() {
5656 Neighbor neighbor = new Neighbor (instance , -1d );
5757 List <Neighbor > neighbors = getNeighborsWithDistanceFromARootNeighboor (neighbor , k );
5858 features .put (neighbor , neighbors );
59- }
60- else {
59+ } else
6160 testIntances .add (instance );
62- }
6361 }
6462 }
6563 }
@@ -71,7 +69,7 @@ protected List<LabeledInstance> getInstancesthatMaximizeAccuracy() {
7169 accuraciesAndInstanceTestIndex [testIndex ] = accuracy ;
7270
7371 testIndex ++;
74- }while (testIndex < accuraciesAndInstanceTestIndex .length - 1 );
72+ } while (testIndex < accuraciesAndInstanceTestIndex .length - 1 );
7573
7674 testIndex = getTestIndexWithGreaterAccuracy (accuraciesAndInstanceTestIndex );
7775 return getTrainingLabeledInstances (partitionedInstances , testIndex );
@@ -140,21 +138,22 @@ protected void calculateFeatureSimilarities() {
140138
141139 protected List <Neighbor > getNeighborsWithDistanceFromARootNeighboor (Neighbor neighbor , int threshold ) {
142140 throw new UnsupportedOperationException ("Continue the implementation" );
143- /*
141+
144142 List <Neighbor > neighbors = new ArrayList <>();
145143 LabeledInstance instance = neighbor .getInstance ();
146144
147145 for (int j = -1 ; j < instances .size () - 1 ; j ++) {
148146 LabeledInstance neighborInstance = instances .get (j + 1 );
149- double similarity = similarityCalculator.calculate(instance.getFeatures(), neighborInstance.getFeatures());
147+ // double similarity = similarityCalculator.calculate(instance.getFeatures(), neighborInstance.getFeatures());
148+ double similarity = 0d ;
150149 Neighbor neighborRoot = new Neighbor (neighborInstance , similarity );
151150 neighbors .add (neighborRoot );
152151 if (neighbors .size () == threshold )
153152 return neighbors ;
154153 }
155154
156155 return neighbors ;
157- */
156+
158157 }
159158
160159 public List <Neighbor > similarNeighbors (LabeledInstance trainingInstance , int k ) {
@@ -183,7 +182,7 @@ public List<Prediction> predict(List<Instance> instances) {
183182 throw new IllegalArgumentException ("instanances can't be empty or null" );
184183
185184 List <Prediction > predictionList = new ArrayList <>();
186- for (Instance instance : instances )
185+ for (Instance instance : instances )
187186 predictionList .add (predict (instance ));
188187
189188 return predictionList ;
0 commit comments