| 125 | } |
| 126 | |
| 127 | public HashBag<Integer> getVotesBag(Neighbor[] kNN, int k, int[] unknown, List<Integer> Y) { |
| 128 | HashBag<Integer> votes = new HashBag<>(); |
| 129 | for (int i = 0; i<k && i<kNN.length; i++) { |
| 130 | votes.add(Y.get(kNN[i].corpusVectorIndex)); |
| 131 | } |
| 132 | if ( dumpVotes && kNN.length>0 ) { |
| 133 | System.out.print(Trainer.featureNameHeader(FEATURES)); |
| 134 | InputDocument firstDoc = corpus.documentsPerExemplar.get(kNN[0].corpusVectorIndex); // pick any neighbor to get parser |
| 135 | System.out.println(Trainer._toString(FEATURES, firstDoc, unknown)+"->"+votes); |
| 136 | kNN = Arrays.copyOfRange(kNN, 0, Math.min(k, kNN.length)); |
| 137 | StringBuilder buf = new StringBuilder(); |
| 138 | for (Neighbor n : kNN) { |
| 139 | buf.append(n.toString(FEATURES, Y)); |
| 140 | buf.append("\n"); |
| 141 | } |
| 142 | System.out.println(buf); |
| 143 | } |
| 144 | return votes; |
| 145 | } |
| 146 | |
| 147 | // get category similarity (1.0-distance) so we can weight votes. Just add up similarity. |
| 148 | // I.e., weight votes with similarity 1 (distances of 0) more than votes with lower similarity |