use of org.dmg.pmml.Model in project drools by kiegroup.
the class ModelUtilsTest method getTargetFieldsWithoutTargetFieldsWithTargets.
@Test
public void getTargetFieldsWithoutTargetFieldsWithTargets() {
final Model model = new RegressionModel();
final DataDictionary dataDictionary = new DataDictionary();
final MiningSchema miningSchema = new MiningSchema();
final Targets targets = new Targets();
IntStream.range(0, 3).forEach(i -> {
final DataField dataField = getRandomDataField();
dataDictionary.addDataFields(dataField);
final MiningField miningField = getMiningField(dataField.getName().getValue(), MiningField.UsageType.ACTIVE);
miningSchema.addMiningFields(miningField);
final Target targetField = getTarget(dataField.getName().getValue(), null);
targets.addTargets(targetField);
});
model.setMiningSchema(miningSchema);
model.setTargets(targets);
Map<String, DATA_TYPE> retrieved = ModelUtils.getTargetFieldsTypeMap(getFieldsFromDataDictionary(dataDictionary), model);
assertNotNull(retrieved);
assertTrue(retrieved.isEmpty());
}
use of org.dmg.pmml.Model in project drools by kiegroup.
the class ModelUtilsTest method getTargetFieldsWithTargetFieldsWithTargetsWithOptType.
@Test
public void getTargetFieldsWithTargetFieldsWithTargetsWithOptType() {
final Model model = new RegressionModel();
final DataDictionary dataDictionary = new DataDictionary();
final MiningSchema miningSchema = new MiningSchema();
final Targets targets = new Targets();
IntStream.range(0, 3).forEach(i -> {
final String fieldName = "fieldName-" + i;
final DataField dataField = getDataField(fieldName, OpType.CATEGORICAL, DataType.STRING);
dataDictionary.addDataFields(dataField);
final MiningField miningField = getMiningField(fieldName, MiningField.UsageType.PREDICTED);
miningField.setOpType(OpType.CONTINUOUS);
miningSchema.addMiningFields(miningField);
final Target targetField = getTarget(fieldName, OpType.CATEGORICAL);
targets.addTargets(targetField);
});
model.setMiningSchema(miningSchema);
model.setTargets(targets);
List<KiePMMLNameOpType> retrieved = ModelUtils.getTargetFields(getFieldsFromDataDictionary(dataDictionary), model);
assertNotNull(retrieved);
assertEquals(miningSchema.getMiningFields().size(), retrieved.size());
retrieved.forEach(kiePMMLNameOpType -> {
Optional<Target> optionalTarget = targets.getTargets().stream().filter(fld -> kiePMMLNameOpType.getName().equals(fld.getField().getValue())).findFirst();
assertTrue(optionalTarget.isPresent());
Target target = optionalTarget.get();
OP_TYPE expected = OP_TYPE.byName(target.getOpType().value());
assertEquals(expected, kiePMMLNameOpType.getOpType());
});
}
use of org.dmg.pmml.Model in project drools by kiegroup.
the class ModelUtilsTest method getOpTypeByTargets.
@Test
public void getOpTypeByTargets() {
final Model model = new RegressionModel();
final DataDictionary dataDictionary = new DataDictionary();
final MiningSchema miningSchema = new MiningSchema();
final Targets targets = new Targets();
IntStream.range(0, 3).forEach(i -> {
final DataField dataField = getRandomDataField();
dataDictionary.addDataFields(dataField);
final MiningField miningField = getRandomMiningField();
miningField.setName(dataField.getName());
miningSchema.addMiningFields(miningField);
final Target targetField = getRandomTarget();
targetField.setField(dataField.getName());
targets.addTargets(targetField);
});
model.setMiningSchema(miningSchema);
model.setTargets(targets);
getFieldsFromDataDictionary(dataDictionary);
targets.getTargets().forEach(target -> {
OP_TYPE retrieved = ModelUtils.getOpType(getFieldsFromDataDictionary(dataDictionary), model, target.getField().getValue());
assertNotNull(retrieved);
OP_TYPE expected = OP_TYPE.byName(target.getOpType().value());
assertEquals(expected, retrieved);
});
}
use of org.dmg.pmml.Model in project drools by kiegroup.
the class PMMLInfo method from.
public static PMMLInfo<PMMLModelInfo> from(InputStream is) throws SAXException, JAXBException {
PMML pmml = org.jpmml.model.PMMLUtil.unmarshal(is);
List<PMMLModelInfo> models = new ArrayList<>();
for (Model pm : pmml.getModels()) {
models.add(pmmlToModelInfo(pm));
}
PMMLInfo<PMMLModelInfo> info = new PMMLInfo<>(models, pmmlToHeaderInfo(pmml, pmml.getHeader()));
return info;
}
use of org.dmg.pmml.Model in project drools by kiegroup.
the class PMMLInfo method pmmlToModelInfo.
public static PMMLModelInfo pmmlToModelInfo(Model pm) {
MiningSchema miningSchema = pm.getMiningSchema();
Collection<String> inputFields = new ArrayList<>();
miningSchema.getMiningFields().stream().filter(mf -> mf.getUsageType() == UsageType.ACTIVE).forEach(fn -> inputFields.add(fn.getName().getValue()));
Collection<String> targetFields = new ArrayList<>();
miningSchema.getMiningFields().stream().filter(mf -> mf.getUsageType() == UsageType.PREDICTED).forEach(fn -> targetFields.add(fn.getName().getValue()));
Collection<String> outputFields = new ArrayList<>();
if (pm.getOutput() != null && pm.getOutput().getOutputFields() != null) {
pm.getOutput().getOutputFields().forEach(of -> outputFields.add(of.getName().getValue()));
}
return new PMMLModelInfo(pm.getModelName(), pm.getClass().getSimpleName(), inputFields, targetFields, outputFields);
}
Aggregations