Search in sources :

Example 1 with DerivedOutputField

use of org.jpmml.converter.DerivedOutputField in project jpmml-sparkml by jpmml.

the class RegressionModelConverter method registerOutputFields.

@Override
public List<OutputField> registerOutputFields(Label label, Model pmmlModel, SparkMLEncoder encoder) {
    T model = getTransformer();
    String predictionCol = model.getPredictionCol();
    Boolean keepPredictionCol = (Boolean) getOption(HasPredictionModelOptions.OPTION_KEEP_PREDICTIONCOL, Boolean.TRUE);
    OutputField predictedOutputField = ModelUtil.createPredictedField(FieldName.create(predictionCol), OpType.CONTINUOUS, label.getDataType());
    DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, keepPredictionCol);
    encoder.putOnlyFeature(predictionCol, new ContinuousFeature(encoder, predictedField));
    return Collections.emptyList();
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) DerivedOutputField(org.jpmml.converter.DerivedOutputField) OutputField(org.dmg.pmml.OutputField) DerivedOutputField(org.jpmml.converter.DerivedOutputField)

Example 2 with DerivedOutputField

use of org.jpmml.converter.DerivedOutputField in project jpmml-sparkml by jpmml.

the class ClusteringModelConverter method registerOutputFields.

@Override
public List<OutputField> registerOutputFields(Label label, org.dmg.pmml.Model pmmlModel, SparkMLEncoder encoder) {
    T model = getTransformer();
    List<Integer> clusters = LabelUtil.createTargetCategories(getNumberOfClusters());
    String predictionCol = model.getPredictionCol();
    OutputField pmmlPredictedOutputField = ModelUtil.createPredictedField(FieldNameUtil.create("pmml", predictionCol), OpType.CATEGORICAL, DataType.STRING).setFinalResult(false);
    DerivedOutputField pmmlPredictedField = encoder.createDerivedField(pmmlModel, pmmlPredictedOutputField, true);
    OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CATEGORICAL, DataType.INTEGER).setResultFeature(ResultFeature.TRANSFORMED_VALUE).setExpression(new FieldRef(pmmlPredictedField.getName()));
    DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, true);
    encoder.putOnlyFeature(predictionCol, new IndexFeature(encoder, predictedField, clusters));
    return Collections.emptyList();
}
Also used : IndexFeature(org.jpmml.converter.IndexFeature) FieldRef(org.dmg.pmml.FieldRef) DerivedOutputField(org.jpmml.converter.DerivedOutputField) OutputField(org.dmg.pmml.OutputField) DerivedOutputField(org.jpmml.converter.DerivedOutputField)

Example 3 with DerivedOutputField

use of org.jpmml.converter.DerivedOutputField 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

OutputField (org.dmg.pmml.OutputField)3 DerivedOutputField (org.jpmml.converter.DerivedOutputField)3 ContinuousFeature (org.jpmml.converter.ContinuousFeature)2 IndexFeature (org.jpmml.converter.IndexFeature)2 ArrayList (java.util.ArrayList)1 HasProbabilityCol (org.apache.spark.ml.param.shared.HasProbabilityCol)1 FieldRef (org.dmg.pmml.FieldRef)1 MapValues (org.dmg.pmml.MapValues)1 ResultFeature (org.dmg.pmml.ResultFeature)1 CategoricalLabel (org.jpmml.converter.CategoricalLabel)1 Feature (org.jpmml.converter.Feature)1