@Test public void testBind() { System.out.println("bind"); Formula formula = Formula.of("revenue", dot(), cross("water", "sowing_density") , mul("humidity", "wind"), delete("wind")); StructType inputSchema = DataTypes.struct( new StructField("revenue", DataTypes.DoubleType, Measure.Currency), new StructField("water", DataTypes.ByteType, new NominalScale("dry", "wet")), new StructField("sowing_density", DataTypes.ByteType, new NominalScale("low", "high")), new StructField("humidity", DataTypes.FloatType, Measure.Percent), new StructField("wind", DataTypes.FloatType) ); assertEquals("revenue ~ . + (water x sowing_density) + (humidity * wind) - wind", formula.toString()); System.out.println(formula.expand(inputSchema)); StructType outputSchema = formula.bind(inputSchema); StructType schema = DataTypes.struct( new StructField("humidity", DataTypes.FloatType, Measure.Percent), new StructField("water", DataTypes.ByteType, new NominalScale("dry", "wet")), new StructField("sowing_density", DataTypes.ByteType, new NominalScale("low", "high")), new StructField("water:sowing_density", DataTypes.IntegerType, new NominalScale("dry:low", "dry:high", "wet:low", "wet:high")), new StructField("humidity * wind", DataTypes.FloatType) ); assertEquals(schema, outputSchema); }
public StructType bind(StructType inputSchema) { if (binding != null && binding.inputSchema == inputSchema) { return binding.xschema; } Formula formula = expand(inputSchema); binding = new Binding(); binding.inputSchema = inputSchema; List<Feature> features = Arrays.stream(formula.predictors) .filter(predictor -> !(predictor instanceof Delete) && !(predictor instanceof Intercept)) .flatMap(predictor -> predictor.bind(inputSchema).stream()) .collect(Collectors.toList()); binding.x = features.toArray(new Feature[features.size()]); binding.xschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); if (response != null) { try { features.addAll(0, response.bind(inputSchema)); binding.yx = features.toArray(new Feature[features.size()]); binding.yxschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); } catch (NullPointerException ex) { logger.debug("The response variable {} doesn't exist in the schema {}", response, inputSchema); } } return binding.xschema; }
Formula implements Serializable { public StructType bind(StructType inputSchema) { if (binding != null && binding.inputSchema == inputSchema) { return binding.xschema; } Formula formula = expand(inputSchema); binding = new Binding(); binding.inputSchema = inputSchema; List<Feature> features = Arrays.stream(formula.predictors) .filter(predictor -> !(predictor instanceof Delete) && !(predictor instanceof Intercept)) .flatMap(predictor -> predictor.bind(inputSchema).stream()) .collect(Collectors.toList()); binding.x = features.toArray(new Feature[features.size()]); binding.xschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); if (response != null) { try { features.addAll(0, response.bind(inputSchema)); binding.yx = features.toArray(new Feature[features.size()]); binding.yxschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); } catch (NullPointerException ex) { logger.debug("The response variable {} doesn't exist in the schema {}", response, inputSchema); } } return binding.xschema; } }
Formula implements Serializable { public StructType bind(StructType inputSchema) { if (binding != null && binding.inputSchema == inputSchema) { return binding.xschema; } Formula formula = expand(inputSchema); binding = new Binding(); binding.inputSchema = inputSchema; List<Feature> features = Arrays.stream(formula.predictors) .filter(predictor -> !(predictor instanceof Delete) && !(predictor instanceof Intercept)) .flatMap(predictor -> predictor.bind(inputSchema).stream()) .collect(Collectors.toList()); binding.x = features.toArray(new Feature[features.size()]); binding.xschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); if (response != null) { try { features.addAll(0, response.bind(inputSchema)); binding.yx = features.toArray(new Feature[features.size()]); binding.yxschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); } catch (NullPointerException ex) { logger.debug("The response variable {} doesn't exist in the schema {}", response, inputSchema); } } return binding.xschema; } Formula(Term response, Term... predictors); }
Formula implements Serializable { public StructType bind(StructType inputSchema) { if (binding != null && binding.inputSchema == inputSchema) { return binding.xschema; } Formula formula = expand(inputSchema); binding = new Binding(); binding.inputSchema = inputSchema; List<Feature> features = Arrays.stream(formula.predictors) .filter(predictor -> !(predictor instanceof Delete) && !(predictor instanceof Intercept)) .flatMap(predictor -> predictor.bind(inputSchema).stream()) .collect(Collectors.toList()); binding.x = features.toArray(new Feature[features.size()]); binding.xschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); if (response != null) { try { features.addAll(0, response.bind(inputSchema)); binding.yx = features.toArray(new Feature[features.size()]); binding.yxschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); } catch (NullPointerException ex) { logger.debug("The response variable {} doesn't exist in the schema {}", response, inputSchema); } } return binding.xschema; } Formula(Term response, Term... predictors); Term[] predictors(); Term response(); @Override String toString(); @Override boolean equals(Object o); static Formula lhs(String lhs); static Formula lhs(Term lhs); static Formula rhs(String... predictors); static Formula rhs(Term... predictors); static Formula of(String response, String... predictors); static Formula of(String response, Term... predictors); static Formula of(Term response, Term... predictors); Formula expand(StructType inputSchema); StructType bind(StructType inputSchema); Tuple apply(Tuple t); Tuple x(Tuple t); DataFrame frame(DataFrame df); DataFrame x(DataFrame df); Matrix matrix(DataFrame df); Matrix matrix(DataFrame df, boolean bias); BaseVector y(DataFrame df); double y(Tuple t); int yint(Tuple t); }
Formula implements Serializable { public StructType bind(StructType inputSchema) { if (binding != null && binding.inputSchema == inputSchema) { return binding.xschema; } Formula formula = expand(inputSchema); binding = new Binding(); binding.inputSchema = inputSchema; List<Feature> features = Arrays.stream(formula.predictors) .filter(predictor -> !(predictor instanceof Delete) && !(predictor instanceof Intercept)) .flatMap(predictor -> predictor.bind(inputSchema).stream()) .collect(Collectors.toList()); binding.x = features.toArray(new Feature[features.size()]); binding.xschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); if (response != null) { try { features.addAll(0, response.bind(inputSchema)); binding.yx = features.toArray(new Feature[features.size()]); binding.yxschema = DataTypes.struct( features.stream() .map(term -> term.field()) .toArray(StructField[]::new) ); } catch (NullPointerException ex) { logger.debug("The response variable {} doesn't exist in the schema {}", response, inputSchema); } } return binding.xschema; } Formula(Term response, Term... predictors); Term[] predictors(); Term response(); @Override String toString(); @Override boolean equals(Object o); static Formula lhs(String lhs); static Formula lhs(Term lhs); static Formula rhs(String... predictors); static Formula rhs(Term... predictors); static Formula of(String response, String... predictors); static Formula of(String response, Term... predictors); static Formula of(Term response, Term... predictors); Formula expand(StructType inputSchema); StructType bind(StructType inputSchema); Tuple apply(Tuple t); Tuple x(Tuple t); DataFrame frame(DataFrame df); DataFrame x(DataFrame df); Matrix matrix(DataFrame df); Matrix matrix(DataFrame df, boolean bias); BaseVector y(DataFrame df); double y(Tuple t); int yint(Tuple t);  }