} else if (inputSchema.isCategorical(featureIndex)) { int encoding = valueEncodings.getValueEncodingMap(featureIndex).get(dataAtIndex); feature = CategoricalFeature.forEncoding(encoding); } else { feature = null;
@Test public void testFeature() { CategoricalFeature f = CategoricalFeature.forEncoding(1); assertEquals(FeatureType.CATEGORICAL, f.getFeatureType()); assertEquals(1, f.getEncoding()); assertEquals(f, CategoricalFeature.forEncoding(1)); // Not necessary for correctness to assert this, but fairly important for performance assertSame(f, CategoricalFeature.forEncoding(1)); }
@Test public void testToString() { CategoricalFeature f = CategoricalFeature.forEncoding(1); assertEquals(":1", f.toString()); }
@Test public void testDecision() { BitSet activeCategories = new BitSet(10); activeCategories.set(2); activeCategories.set(5); Decision decision = new CategoricalDecision(0, activeCategories, true); for (int i = 0; i < 10; i++) { assertEquals(activeCategories.get(i), decision.isPositive(new Example(null, CategoricalFeature.forEncoding(i)))); } assertTrue(decision.isPositive(new Example(null, new Feature[] {null}))); }
CategoricalFeature feature1 = CategoricalFeature.forEncoding( encoding.getValueEncodingMap(1).get(f1 == 1 ? "A" : "B")); for (int f2 = 0; f2 <= 1; f2++) { CategoricalFeature feature2 = CategoricalFeature.forEncoding( encoding.getValueEncodingMap(2).get(f2 == 1 ? "A" : "B")); for (int f3 = 0; f3 <= 1; f3++) { CategoricalFeature feature3 = CategoricalFeature.forEncoding( encoding.getValueEncodingMap(3).get(f3 == 1 ? "A" : "B")); Example toPredict = new Example(null, null, feature1, feature2, feature3);
@Test public void testToExample() { Map<String,Object> overlayConfig = new HashMap<>(); overlayConfig.put("oryx.input-schema.num-features", 5); overlayConfig.put("oryx.input-schema.categorical-features", "[\"4\"]"); overlayConfig.put("oryx.input-schema.id-features", "[\"0\"]"); overlayConfig.put("oryx.input-schema.target-feature", "\"4\""); Config config = ConfigUtils.overlayOn(overlayConfig, ConfigUtils.getDefault()); InputSchema schema = new InputSchema(config); CategoricalValueEncodings encodings = new CategoricalValueEncodings(Collections.singletonMap(4, Arrays.asList("A", "B", "C"))); Example example = ExampleUtils.dataToExample(new String[] {"foo", "1", "2.5", "-3.2", "B"}, schema, encodings); assertEquals(CategoricalFeature.forEncoding(1), example.getTarget()); assertNull(example.getFeature(0)); assertEquals(NumericFeature.forValue(1.0), example.getFeature(1)); assertEquals(NumericFeature.forValue(2.5), example.getFeature(2)); assertEquals(NumericFeature.forValue(-3.2), example.getFeature(3)); assertNull(example.getFeature(4)); assertTrue(example.toString().contains("2.5")); }
@Test public void testUpdate() { int[] counts = { 0, 1, 3, 0, 4, 0 }; CategoricalPrediction prediction = new CategoricalPrediction(counts); Example example = new Example(CategoricalFeature.forEncoding(2)); // Yes, called twice prediction.update(example); prediction.update(example); assertEquals(2, prediction.getMostProbableCategoryEncoding()); counts[2] += 2; assertArrayEquals(toDoubles(counts), prediction.getCategoryCounts()); assertArrayEquals(new double[] {0.0, 0.1, 0.5, 0.0, 0.4, 0.0}, prediction.getCategoryProbabilities()); }
} else if (inputSchema.isCategorical(featureIndex)) { int encoding = valueEncodings.getValueEncodingMap(featureIndex).get(dataAtIndex); feature = CategoricalFeature.forEncoding(encoding); } else { feature = null;