Search in sources :

Example 6 with MapValues

use of org.dmg.pmml.MapValues in project drools by kiegroup.

the class KiePMMLMapValuesFactoryTest method setup.

@BeforeClass
public static void setup() throws Exception {
    PMML pmmlModel = KiePMMLUtil.load(getFileInputStream(TRANSFORMATIONS_SAMPLE), TRANSFORMATIONS_SAMPLE);
    DerivedField mapValued = pmmlModel.getTransformationDictionary().getDerivedFields().stream().filter(derivedField -> MAPVALUED.equals(derivedField.getName().getValue())).findFirst().orElseThrow(() -> new RuntimeException("Missing derived field " + MAPVALUED));
    MAPVALUES = ((MapValues) mapValued.getExpression());
}
Also used : KiePMMLMapValues(org.kie.pmml.commons.model.expressions.KiePMMLMapValues) MapValues(org.dmg.pmml.MapValues) PMML(org.dmg.pmml.PMML) DerivedField(org.dmg.pmml.DerivedField) BeforeClass(org.junit.BeforeClass)

Example 7 with MapValues

use of org.dmg.pmml.MapValues in project drools by kiegroup.

the class PMMLModelTestUtils method getRandomMapValues.

public static MapValues getRandomMapValues() {
    MapValues toReturn = new MapValues();
    toReturn.setDataType(getRandomDataType());
    toReturn.setDefaultValue(getRandomObject(toReturn.getDataType()));
    toReturn.setMapMissingTo(RandomStringUtils.random(6, true, false));
    toReturn.setOutputColumn(RandomStringUtils.random(6, true, false));
    toReturn.setInlineTable(getRandomInlineTableWithCells());
    toReturn.setTableLocator(getRandomTableLocator());
    return toReturn;
}
Also used : MapValues(org.dmg.pmml.MapValues)

Example 8 with MapValues

use of org.dmg.pmml.MapValues in project shifu by ShifuML.

the class PMMLLRModelBuilder method adaptMLModelToPMML.

public RegressionModel adaptMLModelToPMML(ml.shifu.shifu.core.LR lr, RegressionModel pmmlModel) {
    pmmlModel.setNormalizationMethod(NormalizationMethod.LOGIT);
    pmmlModel.setMiningFunction(MiningFunction.REGRESSION);
    RegressionTable table = new RegressionTable();
    table.setIntercept(lr.getBias());
    LocalTransformations lt = pmmlModel.getLocalTransformations();
    List<DerivedField> df = lt.getDerivedFields();
    HashMap<FieldName, FieldName> miningTransformMap = new HashMap<FieldName, FieldName>();
    for (DerivedField dField : df) {
        // Apply z-scale normalization on numerical variables
        if (dField.getExpression() instanceof NormContinuous) {
            miningTransformMap.put(((NormContinuous) dField.getExpression()).getField(), dField.getName());
        } else // Apply bin map on categorical variables
        if (dField.getExpression() instanceof MapValues) {
            miningTransformMap.put(((MapValues) dField.getExpression()).getFieldColumnPairs().get(0).getField(), dField.getName());
        } else if (dField.getExpression() instanceof Discretize) {
            miningTransformMap.put(((Discretize) dField.getExpression()).getField(), dField.getName());
        }
    }
    List<MiningField> miningList = pmmlModel.getMiningSchema().getMiningFields();
    int index = 0;
    for (int i = 0; i < miningList.size(); i++) {
        MiningField mField = miningList.get(i);
        if (mField.getUsageType() != UsageType.ACTIVE)
            continue;
        FieldName mFieldName = mField.getName();
        FieldName fName = mFieldName;
        while (miningTransformMap.containsKey(fName)) {
            fName = miningTransformMap.get(fName);
        }
        NumericPredictor np = new NumericPredictor();
        np.setName(fName);
        np.setCoefficient(lr.getWeights()[index++]);
        table.addNumericPredictors(np);
    }
    pmmlModel.addRegressionTables(table);
    return pmmlModel;
}
Also used : NormContinuous(org.dmg.pmml.NormContinuous) MiningField(org.dmg.pmml.MiningField) HashMap(java.util.HashMap) NumericPredictor(org.dmg.pmml.regression.NumericPredictor) RegressionTable(org.dmg.pmml.regression.RegressionTable) LocalTransformations(org.dmg.pmml.LocalTransformations) MapValues(org.dmg.pmml.MapValues) Discretize(org.dmg.pmml.Discretize) DerivedField(org.dmg.pmml.DerivedField) FieldName(org.dmg.pmml.FieldName)

Example 9 with MapValues

use of org.dmg.pmml.MapValues in project shifu by ShifuML.

the class ZscoreLocalTransformCreator method createCategoricalDerivedField.

/**
 * Create DerivedField for categorical variable
 *
 * @param config
 *            - ColumnConfig for categorical variable
 * @param cutoff
 *            - cutoff for normalization
 * @param normType
 *            - the normalization method that is used to generate DerivedField
 * @return DerivedField for variable
 */
protected List<DerivedField> createCategoricalDerivedField(ColumnConfig config, double cutoff, ModelNormalizeConf.NormType normType) {
    Document document = null;
    try {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        LOG.error("Fail to create document node.", e);
        throw new RuntimeException("Fail to create document node.", e);
    }
    String defaultValue = Normalizer.normalize(config, "doesn't exist at all...by paypal", cutoff, normType).get(0).toString();
    String missingValue = Normalizer.normalize(config, null, cutoff, normType).get(0).toString();
    InlineTable inlineTable = new InlineTable();
    for (int i = 0; i < config.getBinCategory().size(); i++) {
        List<String> catVals = CommonUtils.flattenCatValGrp(config.getBinCategory().get(i));
        for (String cval : catVals) {
            String dval = Normalizer.normalize(config, cval, cutoff, normType).get(0).toString();
            Element out = document.createElementNS(NAME_SPACE_URI, ELEMENT_OUT);
            out.setTextContent(dval);
            Element origin = document.createElementNS(NAME_SPACE_URI, ELEMENT_ORIGIN);
            origin.setTextContent(cval);
            inlineTable.addRows(new Row().addContent(origin).addContent(out));
        }
    }
    MapValues mapValues = new MapValues("out").setDataType(DataType.DOUBLE).setDefaultValue(defaultValue).addFieldColumnPairs(new FieldColumnPair(new FieldName(NormalUtils.getSimpleColumnName(config, columnConfigList, segmentExpansions, datasetHeaders)), ELEMENT_ORIGIN)).setInlineTable(inlineTable).setMapMissingTo(missingValue);
    List<DerivedField> derivedFields = new ArrayList<DerivedField>();
    derivedFields.add(new DerivedField(OpType.CONTINUOUS, DataType.DOUBLE).setName(FieldName.create(genPmmlColumnName(NormalUtils.getSimpleColumnName(config.getColumnName()), normType))).setExpression(mapValues));
    return derivedFields;
}
Also used : InlineTable(org.dmg.pmml.InlineTable) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) FieldColumnPair(org.dmg.pmml.FieldColumnPair) Document(org.w3c.dom.Document) MapValues(org.dmg.pmml.MapValues) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Row(org.dmg.pmml.Row) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Example 10 with MapValues

use of org.dmg.pmml.MapValues in project shifu by ShifuML.

the class NeuralNetworkModelIntegrator method getNeuralInputs.

private NeuralInputs getNeuralInputs(final NeuralNetwork model) {
    NeuralInputs nnInputs = new NeuralInputs();
    // get HashMap for local transform and MiningSchema fields
    HashMap<FieldName, FieldName> reversMiningTransformMap = new HashMap<FieldName, FieldName>();
    HashMap<FieldName, List<FieldName>> treeMapOfTransform = new HashMap<FieldName, List<FieldName>>();
    for (DerivedField dField : model.getLocalTransformations().getDerivedFields()) {
        // Apply z-scale normalization on numerical variables
        FieldName parentField = null;
        if (dField.getExpression() instanceof NormContinuous) {
            parentField = ((NormContinuous) dField.getExpression()).getField();
            reversMiningTransformMap.put(dField.getName(), parentField);
        } else // Apply bin map on categorical variables
        if (dField.getExpression() instanceof MapValues) {
            parentField = ((MapValues) dField.getExpression()).getFieldColumnPairs().get(0).getField();
            reversMiningTransformMap.put(dField.getName(), parentField);
        } else if (dField.getExpression() instanceof Discretize) {
            parentField = ((Discretize) dField.getExpression()).getField();
            reversMiningTransformMap.put(dField.getName(), parentField);
        }
        List<FieldName> fieldNames = treeMapOfTransform.get(parentField);
        if (fieldNames == null) {
            fieldNames = new ArrayList<FieldName>();
        }
        fieldNames.add(dField.getName());
        treeMapOfTransform.put(parentField, fieldNames);
    }
    // comment here
    List<MiningField> miningList = model.getMiningSchema().getMiningFields();
    int index = 0;
    for (DerivedField dField : model.getLocalTransformations().getDerivedFields()) {
        List<FieldName> list = treeMapOfTransform.get(dField.getName());
        boolean isLeaf = (list == null || list.size() == 0);
        FieldName root = getRoot(dField.getName(), reversMiningTransformMap);
        if (isLeaf && isRootInMiningList(root, miningList)) {
            DerivedField field = new DerivedField(OpType.CONTINUOUS, DataType.DOUBLE).setName(dField.getName()).setExpression(new FieldRef(dField.getName()));
            nnInputs.addNeuralInputs(new NeuralInput("0," + (index++), field));
        }
    }
    DerivedField field = new DerivedField(OpType.CONTINUOUS, DataType.DOUBLE).setName(new FieldName(PluginConstants.biasValue)).setExpression(new FieldRef(new FieldName(PluginConstants.biasValue)));
    nnInputs.addNeuralInputs(new NeuralInput(PluginConstants.biasValue, field));
    return nnInputs;
}
Also used : NormContinuous(org.dmg.pmml.NormContinuous) MiningField(org.dmg.pmml.MiningField) FieldRef(org.dmg.pmml.FieldRef) NeuralInputs(org.dmg.pmml.neural_network.NeuralInputs) HashMap(java.util.HashMap) MapValues(org.dmg.pmml.MapValues) Discretize(org.dmg.pmml.Discretize) ArrayList(java.util.ArrayList) List(java.util.List) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField) NeuralInput(org.dmg.pmml.neural_network.NeuralInput)

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