@ApplicationScoped
public class DecisionTreeRepository {
private DecisionTree model;
private Map<String, Integer> speciesMap = new HashMap<>();
public void setSpeciesMap(Map<String, Integer> map) {
this.speciesMap = map;
}
public String getSpeciesLabel(int code) {
// Reverse lookup (inefficient for large maps, but fine here)
return speciesMap.entrySet()
.stream()
.filter(entry -> entry.getValue() == code)
.map(Map.Entry::getKey)
.findFirst()
.orElse("Unknown");
}
public void train(List<Iris> irisList) {
Formula formula = Formula.lhs("species");
DataFrame data = DataFrame.of(Iris.class, irisList);
// Hyperparameters can be set here
DecisionTree.Options options = new DecisionTree.Options();
model = DecisionTree.fit(formula, data, options);
}
public int[] predict(DataFrame input) {
if (model == null) {
throw new IllegalStateException("Model is not trained yet.");
}
return model.predict(input);
}
}