Search in sources :

Example 1 with DataField

use of org.dmg.pmml.DataField in project jpmml-r by jpmml.

the class GBMConverter method encodeSchema.

@Override
public void encodeSchema(RExpEncoder encoder) {
    RGenericVector gbm = getObject();
    RGenericVector distribution = (RGenericVector) gbm.getValue("distribution");
    RStringVector response_name = (RStringVector) gbm.getValue("response.name", true);
    RGenericVector var_levels = (RGenericVector) gbm.getValue("var.levels");
    RStringVector var_names = (RStringVector) gbm.getValue("var.names");
    RNumberVector<?> var_type = (RNumberVector<?>) gbm.getValue("var.type");
    RStringVector classes = (RStringVector) gbm.getValue("classes", true);
    // Dependent variable
    {
        FieldName responseName;
        if (response_name != null) {
            responseName = FieldName.create(response_name.asScalar());
        } else {
            responseName = FieldName.create("y");
        }
        DataField dataField;
        RStringVector distributionName = (RStringVector) distribution.getValue("name");
        switch(distributionName.asScalar()) {
            case "gaussian":
                dataField = encoder.createDataField(responseName, OpType.CONTINUOUS, DataType.DOUBLE);
                break;
            case "adaboost":
            case "bernoulli":
                dataField = encoder.createDataField(responseName, OpType.CATEGORICAL, DataType.STRING, GBMConverter.BINARY_CLASSES);
                break;
            case "multinomial":
                dataField = encoder.createDataField(responseName, OpType.CATEGORICAL, DataType.STRING, classes.getValues());
                break;
            default:
                throw new IllegalArgumentException();
        }
        encoder.setLabel(dataField);
    }
    // Independent variables
    for (int i = 0; i < var_names.size(); i++) {
        FieldName varName = FieldName.create(var_names.getValue(i));
        DataField dataField;
        boolean categorical = (ValueUtil.asInt(var_type.getValue(i)) > 0);
        if (categorical) {
            RStringVector var_level = (RStringVector) var_levels.getValue(i);
            dataField = encoder.createDataField(varName, OpType.CATEGORICAL, DataType.STRING, var_level.getValues());
        } else {
            dataField = encoder.createDataField(varName, OpType.CONTINUOUS, DataType.DOUBLE);
        }
        encoder.addFeature(dataField);
    }
}
Also used : DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName)

Example 2 with DataField

use of org.dmg.pmml.DataField in project jpmml-r by jpmml.

the class IForestConverter method encodeSchema.

@Override
public void encodeSchema(RExpEncoder encoder) {
    RGenericVector iForest = getObject();
    RStringVector xcols = (RStringVector) iForest.getValue("xcols");
    RBooleanVector colisfactor = (RBooleanVector) iForest.getValue("colisfactor");
    if (xcols.size() != colisfactor.size()) {
        throw new IllegalArgumentException();
    }
    boolean hasFactors = false;
    for (int i = 0; i < colisfactor.size(); i++) {
        hasFactors |= colisfactor.getValue(i);
    }
    if (hasFactors) {
        throw new IllegalArgumentException();
    }
    // Dependent variable
    {
        DataField dataField = encoder.createDataField(FieldName.create("pathLength"), OpType.CONTINUOUS, DataType.DOUBLE);
        encoder.setLabel(dataField);
    }
    // Independent variables
    for (int i = 0; i < xcols.size(); i++) {
        String xcol = xcols.getValue(i);
        DataField dataField = encoder.createDataField(FieldName.create(xcol), OpType.CONTINUOUS, DataType.DOUBLE);
        encoder.addFeature(dataField);
    }
}
Also used : DataField(org.dmg.pmml.DataField)

Example 3 with DataField

use of org.dmg.pmml.DataField in project jpmml-r by jpmml.

the class LMConverter method encodeSchema.

public void encodeSchema(FormulaContext context, RExp terms, RExpEncoder encoder) {
    RIntegerVector response = (RIntegerVector) terms.getAttributeValue("response");
    Formula formula = FormulaUtil.createFormula(terms, context, encoder);
    // Dependent variable
    int responseIndex = response.asScalar();
    if (responseIndex != 0) {
        DataField dataField = (DataField) formula.getField(responseIndex - 1);
        encoder.setLabel(dataField);
    } else {
        throw new IllegalArgumentException();
    }
    String interceptName = getInterceptName();
    // Independent variables
    List<String> coefficientNames = getCoefficientNames();
    for (String coefficientName : coefficientNames) {
        if ((interceptName).equals(coefficientName)) {
            continue;
        }
        Feature feature = formula.resolveFeature(coefficientName);
        encoder.addFeature(feature);
    }
    this.formula = formula;
}
Also used : DataField(org.dmg.pmml.DataField) Feature(org.jpmml.converter.Feature)

Example 4 with DataField

use of org.dmg.pmml.DataField in project jpmml-r by jpmml.

the class LRMConverter method encodeSchema.

@Override
public void encodeSchema(RExpEncoder encoder) {
    RGenericVector lrm = getObject();
    RIntegerVector freq = (RIntegerVector) lrm.getValue("freq");
    RStringVector freqNames = freq.dimnames(0);
    super.encodeSchema(encoder);
    Label label = encoder.getLabel();
    DataField dataField = (DataField) encoder.toCategorical(label.getName(), freqNames.getValues());
    encoder.setLabel(dataField);
}
Also used : DataField(org.dmg.pmml.DataField) CategoricalLabel(org.jpmml.converter.CategoricalLabel) Label(org.jpmml.converter.Label)

Example 5 with DataField

use of org.dmg.pmml.DataField in project jpmml-r by jpmml.

the class MVRConverter method encodeSchema.

@Override
public void encodeSchema(RExpEncoder encoder) {
    RGenericVector mvr = getObject();
    RDoubleVector coefficients = (RDoubleVector) mvr.getValue("coefficients");
    RDoubleVector scale = (RDoubleVector) mvr.getValue("scale", true);
    RExp terms = mvr.getValue("terms");
    final RGenericVector model = (RGenericVector) mvr.getValue("model");
    RStringVector rowNames = coefficients.dimnames(0);
    RStringVector columnNames = coefficients.dimnames(1);
    FormulaContext context = new ModelFrameFormulaContext(model);
    Formula formula = FormulaUtil.createFormula(terms, context, encoder);
    // Dependent variable
    {
        FieldName name = FieldName.create(columnNames.asScalar());
        DataField dataField = (DataField) encoder.getField(name);
        encoder.setLabel(dataField);
    }
    // Independent variables
    for (int i = 0; i < rowNames.size(); i++) {
        String rowName = rowNames.getValue(i);
        Feature feature = formula.resolveFeature(rowName);
        if (scale != null) {
            feature = feature.toContinuousFeature();
            Apply apply = PMMLUtil.createApply("/", feature.ref(), PMMLUtil.createConstant(scale.getValue(i)));
            DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply);
            feature = new ContinuousFeature(encoder, derivedField);
        }
        encoder.addFeature(feature);
    }
}
Also used : Apply(org.dmg.pmml.Apply) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Aggregations

DataField (org.dmg.pmml.DataField)101 Test (org.junit.Test)51 DataDictionary (org.dmg.pmml.DataDictionary)42 MiningField (org.dmg.pmml.MiningField)42 MiningSchema (org.dmg.pmml.MiningSchema)30 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)28 RegressionModel (org.dmg.pmml.regression.RegressionModel)27 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)27 FieldName (org.dmg.pmml.FieldName)24 Model (org.dmg.pmml.Model)24 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)22 DataType (org.dmg.pmml.DataType)19 OutputField (org.dmg.pmml.OutputField)19 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)19 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)18 ArrayList (java.util.ArrayList)17 List (java.util.List)17 PMML (org.dmg.pmml.PMML)17 Collectors (java.util.stream.Collectors)16 OpType (org.dmg.pmml.OpType)15