Search in sources :

Example 11 with MapValues

use of org.dmg.pmml.MapValues in project jpmml-sparkml by jpmml.

the class ClassificationModelConverter method registerOutputFields.

@Override
public List<OutputField> registerOutputFields(Label label, Model pmmlModel, SparkMLEncoder encoder) {
    T model = getTransformer();
    CategoricalLabel categoricalLabel = (CategoricalLabel) label;
    List<Integer> categories = LabelUtil.createTargetCategories(categoricalLabel.size());
    String predictionCol = model.getPredictionCol();
    Boolean keepPredictionCol = (Boolean) getOption(HasPredictionModelOptions.OPTION_KEEP_PREDICTIONCOL, Boolean.TRUE);
    OutputField pmmlPredictedOutputField = ModelUtil.createPredictedField(FieldNameUtil.create("pmml", predictionCol), OpType.CATEGORICAL, categoricalLabel.getDataType()).setFinalResult(false);
    DerivedOutputField pmmlPredictedField = encoder.createDerivedField(pmmlModel, pmmlPredictedOutputField, keepPredictionCol);
    MapValues mapValues = PMMLUtil.createMapValues(pmmlPredictedField.getName(), categoricalLabel.getValues(), categories).setDataType(DataType.DOUBLE);
    OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CONTINUOUS, DataType.DOUBLE).setResultFeature(ResultFeature.TRANSFORMED_VALUE).setExpression(mapValues);
    DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, keepPredictionCol);
    encoder.putOnlyFeature(predictionCol, new IndexFeature(encoder, predictedField, categories));
    List<OutputField> result = new ArrayList<>();
    if (model instanceof HasProbabilityCol) {
        HasProbabilityCol hasProbabilityCol = (HasProbabilityCol) model;
        String probabilityCol = hasProbabilityCol.getProbabilityCol();
        List<Feature> features = new ArrayList<>();
        for (int i = 0; i < categoricalLabel.size(); i++) {
            Object value = categoricalLabel.getValue(i);
            OutputField probabilityField = ModelUtil.createProbabilityField(FieldNameUtil.create(probabilityCol, value), DataType.DOUBLE, value);
            result.add(probabilityField);
            features.add(new ContinuousFeature(encoder, probabilityField));
        }
        // XXX
        encoder.putFeatures(probabilityCol, features);
    }
    return result;
}
Also used : IndexFeature(org.jpmml.converter.IndexFeature) HasProbabilityCol(org.apache.spark.ml.param.shared.HasProbabilityCol) ArrayList(java.util.ArrayList) IndexFeature(org.jpmml.converter.IndexFeature) ResultFeature(org.dmg.pmml.ResultFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DerivedOutputField(org.jpmml.converter.DerivedOutputField) MapValues(org.dmg.pmml.MapValues) CategoricalLabel(org.jpmml.converter.CategoricalLabel) OutputField(org.dmg.pmml.OutputField) DerivedOutputField(org.jpmml.converter.DerivedOutputField)

Aggregations

MapValues (org.dmg.pmml.MapValues)11 ArrayList (java.util.ArrayList)6 DerivedField (org.dmg.pmml.DerivedField)6 ContinuousFeature (org.jpmml.converter.ContinuousFeature)4 Feature (org.jpmml.converter.Feature)4 FieldColumnPair (org.dmg.pmml.FieldColumnPair)3 FieldName (org.dmg.pmml.FieldName)3 InlineTable (org.dmg.pmml.InlineTable)3 Row (org.dmg.pmml.Row)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 HasProbabilityCol (org.apache.spark.ml.param.shared.HasProbabilityCol)2 Discretize (org.dmg.pmml.Discretize)2 MiningField (org.dmg.pmml.MiningField)2 NormContinuous (org.dmg.pmml.NormContinuous)2 OutputField (org.dmg.pmml.OutputField)2 ResultFeature (org.dmg.pmml.ResultFeature)2 CategoricalFeature (org.jpmml.converter.CategoricalFeature)2 CategoricalLabel (org.jpmml.converter.CategoricalLabel)2