@Override public void update(Example train) { NumericFeature target = (NumericFeature) train.getTarget(); update(target.getValue(), 1); }
return new Example(target, features); } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { log.warn("Bad input: {}", Arrays.toString(data));
@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")); }
@Override public boolean isPositive(Example example) { CategoricalFeature feature = (CategoricalFeature) example.getFeature(getFeatureNumber()); if (feature == null) { return defaultDecision; } int encoding = feature.getEncoding(); if (encoding >= activeCategoryEncodings.size()) { return defaultDecision; } return activeCategoryEncodings.get(encoding); }
public class MainActivity extends AppCompatActivity { String json = "{\n" + " \"Some1\": {\n" + " \"data\": \"dta\",\n" + " \"title\": \"ttl\"\n" + " },\n" + " \"Some3\": {\n" + " \"data\": \"dta2\",\n" + " \"title\": \"ttl2\"\n" + " }\n" + "}"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Example mExample = new Gson().fromJson(json, Example.class); Toast.makeText(this, mExample.toString(), Toast.LENGTH_SHORT).show(); } }
@Override public boolean isPositive(Example example) { NumericFeature feature = (NumericFeature) example.getFeature(getFeatureNumber()); return feature == null ? defaultDecision : feature.getValue() >= threshold; }
public void save(Example example) { Session session = null; Transaction tx=null; try { log.info( example.toString()); session = this.sessionFactory.openSession(); tx = session.beginTransaction(); session.persist(example); tx.commit(); } catch (Exception e) { e.printStackTrace(); } finally { if (!tx.wasCommitted()) { tx.rollback(); }//not much doing but a good practice session.flush(); //this is where I think things will start working. session.close(); } }
@Override public void update(Example train) { CategoricalFeature target = (CategoricalFeature) train.getTarget(); update(target.getEncoding(), 1); }
@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}))); }
@Override public boolean isPositive(Example example) { CategoricalFeature feature = (CategoricalFeature) example.getFeature(getFeatureNumber()); if (feature == null) { return defaultDecision; } int encoding = feature.getEncoding(); if (encoding >= activeCategoryEncodings.size()) { return defaultDecision; } return activeCategoryEncodings.get(encoding); }
static double rmse(DecisionForest forest, JavaRDD<Example> examples) { double mse = examples.mapToDouble(example -> { NumericPrediction prediction = (NumericPrediction) forest.predict(example); NumericFeature target = (NumericFeature) example.getTarget(); double diff = prediction.getPrediction() - target.getValue(); return diff * diff; }).mean(); return Math.sqrt(mse); }
@Test public void testDecision() { Decision decision = new NumericDecision(0, -3.1, true); assertFalse(decision.isPositive(new Example(null, NumericFeature.forValue(-3.5)))); assertTrue(decision.isPositive(new Example(null, NumericFeature.forValue(-3.1)))); assertTrue(decision.isPositive(new Example(null, NumericFeature.forValue(-3.0)))); assertTrue(decision.isPositive(new Example(null, NumericFeature.forValue(3.1)))); assertTrue(decision.isPositive(new Example(null, new Feature[] {null}))); }
@Override public boolean isPositive(Example example) { NumericFeature feature = (NumericFeature) example.getFeature(getFeatureNumber()); return feature == null ? defaultDecision : feature.getValue() >= threshold; }
static double accuracy(DecisionForest forest, JavaRDD<Example> examples) { long total = examples.count(); if (total == 0) { return 0.0; } long correct = examples.filter(example -> { CategoricalPrediction prediction = (CategoricalPrediction) forest.predict(example); CategoricalFeature target = (CategoricalFeature) example.getTarget(); return prediction.getMostProbableCategoryEncoding() == target.getEncoding(); }).count(); return (double) correct / total; }
CategoricalFeature feature3 = CategoricalFeature.forEncoding( encoding.getValueEncodingMap(3).get(f3 == 1 ? "A" : "B")); Example toPredict = new Example(null, null, feature1, feature2, feature3); double prediction = ((NumericPrediction) forest.predict(toPredict)).getPrediction(); assertEquals("Incorrect prediction " + prediction + " for " + toPredict,
JavaPairRDD<Pair<Integer,String>,Iterable<Feature>> targetsByTreeAndID = examplesRDD.flatMapToPair(example -> { Feature target = example.getTarget(); DecisionTree[] trees = forest.getTrees(); List<Tuple2<Pair<Integer,String>,Feature>> results = new ArrayList<>(trees.length);
for (int f3 : zeroOne) { CategoricalPrediction prediction = (CategoricalPrediction) forest.predict(new Example(null, null, NumericFeature.forValue(f1),
@Override public void update(Example train) { NumericFeature target = (NumericFeature) train.getTarget(); update(target.getValue(), 1); }
@Test public void testPredict() { DecisionForest forest = buildTestForest(); NumericPrediction prediction = (NumericPrediction) forest.predict(new Example(null, NumericFeature.forValue(0.5))); assertEquals(1.0, prediction.getPrediction()); }
@Override public void update(Example train) { CategoricalFeature target = (CategoricalFeature) train.getTarget(); update(target.getEncoding(), 1); }