use of org.dmg.pmml.MiningField in project drools by kiegroup.
the class KiePMMLMiningFieldInstanceFactoryTest method getKiePMMLMiningField.
@Test
public void getKiePMMLMiningField() {
DataField dataField = getRandomDataField();
MiningField toConvert = getRandomMiningField(dataField);
KiePMMLMiningField toVerify = KiePMMLMiningFieldInstanceFactory.getKiePMMLMiningField(toConvert, dataField);
commonVerifyKiePMMLMiningField(toVerify, toConvert, dataField);
}
use of org.dmg.pmml.MiningField in project drools by kiegroup.
the class KiePMMLUtil method populateMissingMiningTargetField.
/**
* Method to populate <code>MiningSchema</code> with a n ad-hoc created target <code>MiningField</code>.
* It also populate the given <code>List<DataField></code> with the relative <code>DataField</code>.
* This method has to be called <b>after</b> the model name has been set
* @param model
* @param dataFields
*/
static void populateMissingMiningTargetField(final Model model, final List<DataField> dataFields) {
List<MiningField> miningTargetFields = getMiningTargetFields(model.getMiningSchema().getMiningFields());
if (miningTargetFields.isEmpty()) {
Optional<DataField> targetDataField = getTargetDataField(model);
targetDataField.ifPresent(dataField -> {
dataFields.add(dataField);
MiningField targetMiningField = getTargetMiningField(dataField);
model.getMiningSchema().addMiningFields(targetMiningField);
correctTargetFields(targetMiningField, model.getTargets());
});
}
}
use of org.dmg.pmml.MiningField in project jpmml-sparkml by jpmml.
the class ConverterUtil method toPMML.
public static PMML toPMML(StructType schema, PipelineModel pipelineModel) {
checkVersion();
SparkMLEncoder encoder = new SparkMLEncoder(schema);
List<org.dmg.pmml.Model> models = new ArrayList<>();
Iterable<Transformer> transformers = getTransformers(pipelineModel);
for (Transformer transformer : transformers) {
TransformerConverter<?> converter = ConverterUtil.createConverter(transformer);
if (converter instanceof FeatureConverter) {
FeatureConverter<?> featureConverter = (FeatureConverter<?>) converter;
featureConverter.registerFeatures(encoder);
} else if (converter instanceof ModelConverter) {
ModelConverter<?> modelConverter = (ModelConverter<?>) converter;
org.dmg.pmml.Model model = modelConverter.registerModel(encoder);
models.add(model);
} else {
throw new IllegalArgumentException("Expected a " + FeatureConverter.class.getName() + " or " + ModelConverter.class.getName() + " instance, got " + converter);
}
}
org.dmg.pmml.Model rootModel;
if (models.size() == 1) {
rootModel = Iterables.getOnlyElement(models);
} else if (models.size() > 1) {
List<MiningField> targetMiningFields = new ArrayList<>();
for (org.dmg.pmml.Model model : models) {
MiningSchema miningSchema = model.getMiningSchema();
List<MiningField> miningFields = miningSchema.getMiningFields();
for (MiningField miningField : miningFields) {
MiningField.UsageType usageType = miningField.getUsageType();
switch(usageType) {
case PREDICTED:
case TARGET:
targetMiningFields.add(miningField);
break;
default:
break;
}
}
}
MiningSchema miningSchema = new MiningSchema(targetMiningFields);
MiningModel miningModel = MiningModelUtil.createModelChain(models, new Schema(null, Collections.<Feature>emptyList())).setMiningSchema(miningSchema);
rootModel = miningModel;
} else {
throw new IllegalArgumentException("Expected a pipeline with one or more models, got a pipeline with zero models");
}
PMML pmml = encoder.encodePMML(rootModel);
return pmml;
}
use of org.dmg.pmml.MiningField in project drools by kiegroup.
the class KiePMMLUtil method populateMissingOutputFieldDataType.
/**
* Method to populate the <b>dataType</b> property of <code>OutputField</code>s.
* Such property was optional until 4.4.1 spec
* @param toPopulate
* @param miningFields
* @param dataFields
*/
static void populateMissingOutputFieldDataType(List<OutputField> toPopulate, List<MiningField> miningFields, List<DataField> dataFields) {
// partial implementation to fix missing "dataType" inside OutputField; "dataType" became mandatory only in 4.4.1 version
List<MiningField> targetFields = getMiningTargetFields(miningFields);
toPopulate.stream().filter(outputField -> outputField.getDataType() == null).forEach(outputField -> {
MiningField referencedField = null;
if (outputField.getTargetField() != null) {
referencedField = targetFields.stream().filter(targetField -> outputField.getTargetField().equals(targetField.getName())).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a target field for OutputField " + outputField.getName().getValue()));
}
if (referencedField == null && (outputField.getResultFeature() == null || outputField.getResultFeature().equals(ResultFeature.PREDICTED_VALUE))) {
// default predictedValue
referencedField = targetFields.stream().findFirst().orElse(// It is allowed to not have any "target" field inside MiningSchema
null);
}
if (referencedField == null && ResultFeature.PROBABILITY.equals(outputField.getResultFeature())) {
// we set the "dataType" to "double" because outputField is a "probability", we may return
outputField.setDataType(DataType.DOUBLE);
return;
}
if (referencedField != null) {
FieldName targetFieldName = referencedField.getName();
DataField dataField = dataFields.stream().filter(df -> df.getName().equals(targetFieldName)).findFirst().orElseThrow(() -> new KiePMMLException("Failed to find a DataField field for " + "MiningField " + targetFieldName.toString()));
outputField.setDataType(dataField.getDataType());
}
});
}
use of org.dmg.pmml.MiningField in project drools by kiegroup.
the class KiePMMLUtil method getTargetMiningField.
/**
* Returns a model-specific <b>target</b> <code>MiningField</code>
* @param dataField
* @return
*/
static MiningField getTargetMiningField(final DataField dataField) {
MiningField toReturn = new MiningField();
toReturn.setName(dataField.getName());
toReturn.setUsageType(MiningField.UsageType.TARGET);
return toReturn;
}
Aggregations