Search in sources :

Example 1 with PMMLEncoder

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

the class TermFeature method toContinuousFeature.

@Override
public ContinuousFeature toContinuousFeature() {
    PMMLEncoder encoder = ensureEncoder();
    DerivedField derivedField = encoder.getDerivedField(getName());
    if (derivedField == null) {
        Apply apply = createApply();
        derivedField = encoder.createDerivedField(getName(), OpType.CONTINUOUS, getDataType(), apply);
    }
    return new ContinuousFeature(encoder, derivedField);
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Apply(org.dmg.pmml.Apply) PMMLEncoder(org.jpmml.converter.PMMLEncoder) DerivedField(org.dmg.pmml.DerivedField)

Example 2 with PMMLEncoder

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

the class TermFeature method toWeightedTermFeature.

public WeightedTermFeature toWeightedTermFeature(double weight) {
    PMMLEncoder encoder = ensureEncoder();
    DefineFunction defineFunction = getDefineFunction();
    String name = (defineFunction.getName()).replace("tf@", "tf-idf@");
    DefineFunction weightedDefineFunction = encoder.getDefineFunction(name);
    if (weightedDefineFunction == null) {
        ParameterField weightField = new ParameterField(FieldName.create("weight"));
        List<ParameterField> parameterFields = new ArrayList<>(defineFunction.getParameterFields());
        parameterFields.add(weightField);
        Apply apply = PMMLUtil.createApply("*", defineFunction.getExpression(), new FieldRef(weightField.getName()));
        weightedDefineFunction = new DefineFunction(name, OpType.CONTINUOUS, parameterFields).setDataType(DataType.DOUBLE).setExpression(apply);
        encoder.addDefineFunction(weightedDefineFunction);
    }
    return new WeightedTermFeature(encoder, weightedDefineFunction, getFeature(), getValue(), weight);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Apply(org.dmg.pmml.Apply) PMMLEncoder(org.jpmml.converter.PMMLEncoder) ArrayList(java.util.ArrayList) DefineFunction(org.dmg.pmml.DefineFunction) ParameterField(org.dmg.pmml.ParameterField)

Example 3 with PMMLEncoder

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

the class ClassificationModelConverter method registerOutputFields.

@Override
public List<OutputField> registerOutputFields(Label label, SparkMLEncoder encoder) {
    T model = getTransformer();
    CategoricalLabel categoricalLabel = (CategoricalLabel) label;
    List<OutputField> result = new ArrayList<>();
    String predictionCol = model.getPredictionCol();
    OutputField pmmlPredictedField = ModelUtil.createPredictedField(FieldName.create("pmml(" + predictionCol + ")"), categoricalLabel.getDataType(), OpType.CATEGORICAL);
    result.add(pmmlPredictedField);
    List<String> categories = new ArrayList<>();
    DocumentBuilder documentBuilder = DOMUtil.createDocumentBuilder();
    InlineTable inlineTable = new InlineTable();
    List<String> columns = Arrays.asList("input", "output");
    for (int i = 0; i < categoricalLabel.size(); i++) {
        String value = categoricalLabel.getValue(i);
        String category = String.valueOf(i);
        categories.add(category);
        Row row = DOMUtil.createRow(documentBuilder, columns, Arrays.asList(value, category));
        inlineTable.addRows(row);
    }
    MapValues mapValues = new MapValues().addFieldColumnPairs(new FieldColumnPair(pmmlPredictedField.getName(), columns.get(0))).setOutputColumn(columns.get(1)).setInlineTable(inlineTable);
    final OutputField predictedField = new OutputField(FieldName.create(predictionCol), DataType.DOUBLE).setOpType(OpType.CATEGORICAL).setResultFeature(ResultFeature.TRANSFORMED_VALUE).setExpression(mapValues);
    result.add(predictedField);
    Feature feature = new CategoricalFeature(encoder, predictedField.getName(), predictedField.getDataType(), categories) {

        @Override
        public ContinuousFeature toContinuousFeature() {
            PMMLEncoder encoder = ensureEncoder();
            return new ContinuousFeature(encoder, getName(), getDataType());
        }
    };
    encoder.putOnlyFeature(predictionCol, feature);
    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++) {
            String value = categoricalLabel.getValue(i);
            OutputField probabilityField = ModelUtil.createProbabilityField(FieldName.create(probabilityCol + "(" + value + ")"), DataType.DOUBLE, value);
            result.add(probabilityField);
            features.add(new ContinuousFeature(encoder, probabilityField.getName(), probabilityField.getDataType()));
        }
        encoder.putFeatures(probabilityCol, features);
    }
    return result;
}
Also used : InlineTable(org.dmg.pmml.InlineTable) HasProbabilityCol(org.apache.spark.ml.param.shared.HasProbabilityCol) PMMLEncoder(org.jpmml.converter.PMMLEncoder) ArrayList(java.util.ArrayList) FieldColumnPair(org.dmg.pmml.FieldColumnPair) ResultFeature(org.dmg.pmml.ResultFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DocumentBuilder(javax.xml.parsers.DocumentBuilder) MapValues(org.dmg.pmml.MapValues) CategoricalLabel(org.jpmml.converter.CategoricalLabel) OutputField(org.dmg.pmml.OutputField) Row(org.dmg.pmml.Row)

Aggregations

PMMLEncoder (org.jpmml.converter.PMMLEncoder)3 ArrayList (java.util.ArrayList)2 Apply (org.dmg.pmml.Apply)2 ContinuousFeature (org.jpmml.converter.ContinuousFeature)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 HasProbabilityCol (org.apache.spark.ml.param.shared.HasProbabilityCol)1 DefineFunction (org.dmg.pmml.DefineFunction)1 DerivedField (org.dmg.pmml.DerivedField)1 FieldColumnPair (org.dmg.pmml.FieldColumnPair)1 FieldRef (org.dmg.pmml.FieldRef)1 InlineTable (org.dmg.pmml.InlineTable)1 MapValues (org.dmg.pmml.MapValues)1 OutputField (org.dmg.pmml.OutputField)1 ParameterField (org.dmg.pmml.ParameterField)1 ResultFeature (org.dmg.pmml.ResultFeature)1 Row (org.dmg.pmml.Row)1 CategoricalFeature (org.jpmml.converter.CategoricalFeature)1 CategoricalLabel (org.jpmml.converter.CategoricalLabel)1 Feature (org.jpmml.converter.Feature)1