LearnPatternSimilarityClassifier extends ASimplifiedTSClassifier<Integer> { public int findNearestInstanceIndex(final int[][] leafNodeCounts) { double minDistance = Double.MAX_VALUE; int nearestInstIdx = 0; for (int inst = 0; inst < this.trainLeafNodes.length; inst++) { double tmpDist = 0; for (int i = 0; i < this.trainLeafNodes[inst].length; i++) { tmpDist += MathUtil.intManhattanDistance(this.trainLeafNodes[inst][i], leafNodeCounts[i]); } if (tmpDist < minDistance) { minDistance = tmpDist; nearestInstIdx = inst; } } return nearestInstIdx; } LearnPatternSimilarityClassifier(final int seed, final int numTrees, final int maxTreeDepth, final int numSegments); @Override Integer predict(final double[] univInstance); int findNearestInstanceIndex(final int[][] leafNodeCounts); @Override Integer predict(final List<double[]> multivInstance); @Override List<Integer> predict(final TimeSeriesDataset2 dataset); int[][] getSegments(); void setSegments(final int[][] segments); int[][] getSegmentsDifference(); void setSegmentsDifference(final int[][] segmentsDifference); int[] getLengthPerTree(); void setLengthPerTree(final int[] lengthPerTree); int[] getClassAttIndexPerTree(); void setClassAttIndexPerTree(final int[] classAttIndexPerTree); AccessibleRandomTree[] getTrees(); void setTrees(final AccessibleRandomTree[] trees); int[][][] getTrainLeafNodes(); void setTrainLeafNodes(final int[][][] trainLeafNodes); int[] getTrainTargets(); void setTrainTargets(final int[] trainTargets); List<Attribute> getAttributes(); void setAttributes(final List<Attribute> attributes); @Override LearnPatternSimilarityLearningAlgorithm getLearningAlgorithm(final TimeSeriesDataset2 dataset);  }
@Test public void findNearestInstanceIndexTest() { final int[][][] trainLeafNodes = new int[][][] {{{5}, {3}}, {{4}, {5}}}; classifier.setTrainLeafNodes(trainLeafNodes); final int[][] testLeafNodes = new int[][] { { 4 }, { 4 } }; Assert.assertEquals("The nearest neighbor instance test selected the wrong instance.", 1, classifier.findNearestInstanceIndex(testLeafNodes)); }