RemoveDiscardedFeatures implements Function<String[], String[]> { @Override public String[] call(String[] tokens) { int size = newToOldIndicesList.size() + 1; if (responseIndex == -1) { size = newToOldIndicesList.size(); } String[] features = new String[size]; for (int i = 0; i < tokens.length; i++) { int newIndex = newToOldIndicesList.indexOf(i); if (newIndex != -1) { features[newIndex] = tokens[i]; } else if (i == responseIndex) { features[features.length - 1] = tokens[i]; } else { continue; } } return features; } private  RemoveDiscardedFeatures(Builder builder); @Override String[] call(String[] tokens);  }
@Test public void testDiscardedFeatureRemoval() throws MLModelBuilderException { List<Integer> newToOldIndicesList = new ArrayList<Integer>(); newToOldIndicesList.add(2); newToOldIndicesList.add(3); newToOldIndicesList.add(5); newToOldIndicesList.add(7); newToOldIndicesList.add(8); int responseIndex = 9; RemoveDiscardedFeatures removeDiscardedFeatures = new RemoveDiscardedFeatures.Builder() .indices(newToOldIndicesList).responseIndex(responseIndex).build(); String[] result = removeDiscardedFeatures.call(new String[] { "2.2", "3.0", "4.2", "1.1", "2.6", "0.7", "1.44", "4.5", "7.7", "0" }); Assert.assertEquals(result.length, 6); Assert.assertEquals(result, new String[] { "4.2", "1.1", "0.7", "4.5", "7.7", "0" }); }
@Test public void testDiscardedFeatureRemovalWhenResponseVariableIsInMiddle() throws MLModelBuilderException { List<Integer> newToOldIndicesList = new ArrayList<Integer>(); newToOldIndicesList.add(2); newToOldIndicesList.add(3); newToOldIndicesList.add(5); newToOldIndicesList.add(7); newToOldIndicesList.add(8); int responseIndex = 4; RemoveDiscardedFeatures removeDiscardedFeatures = new RemoveDiscardedFeatures.Builder() .indices(newToOldIndicesList).responseIndex(responseIndex).build(); String[] result = removeDiscardedFeatures.call(new String[] { "2.2", "3.0", "4.2", "1.1", "0", "0.7", "1.44", "4.5", "7.7" }); Assert.assertEquals(result.length, 6); Assert.assertEquals(result, new String[] { "4.2", "1.1", "0.7", "4.5", "7.7", "0" }); }
@Test public void testDiscardedFeatureRemovalWhenResponseVariableIsAbsent() throws MLModelBuilderException { List<Integer> newToOldIndicesList = new ArrayList<Integer>(); newToOldIndicesList.add(2); newToOldIndicesList.add(3); newToOldIndicesList.add(5); newToOldIndicesList.add(7); newToOldIndicesList.add(8); int responseIndex = -1; RemoveDiscardedFeatures removeDiscardedFeatures = new RemoveDiscardedFeatures.Builder() .indices(newToOldIndicesList).responseIndex(responseIndex).build(); String[] result = removeDiscardedFeatures.call(new String[] { "2.2", "3.0", "4.2", "1.1", "0.5", "0.7", "1.44", "4.5", "7.7", "9.2" }); Assert.assertEquals(result.length, 5); Assert.assertEquals(result, new String[] { "4.2", "1.1", "0.7", "4.5", "7.7" }); }