Search in sources :

Example 56 with MiningField

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

the class TreeModelMiningSchemaCreator method build.

@Override
public MiningSchema build(BasicML basicML) {
    MiningSchema miningSchema = new MiningSchema();
    for (ColumnConfig columnConfig : columnConfigList) {
        if (columnConfig.isFinalSelect() || columnConfig.isTarget()) {
            MiningField miningField = new MiningField();
            // TODO, how to support segment variable in tree model, here should be changed
            miningField.setName(FieldName.create(NormalUtils.getSimpleColumnName(columnConfig.getColumnName())));
            miningField.setOpType(getOptype(columnConfig));
            if (columnConfig.isNumerical()) {
                miningField.setMissingValueReplacement(String.valueOf(columnConfig.getColumnStats().getMean()));
            } else {
                miningField.setMissingValueReplacement("");
            }
            if (columnConfig.isFinalSelect()) {
                miningField.setUsageType(UsageType.ACTIVE);
            } else if (columnConfig.isTarget()) {
                miningField.setUsageType(UsageType.TARGET);
            }
            miningSchema.addMiningFields(miningField);
        }
    }
    return miningSchema;
}
Also used : MiningField(org.dmg.pmml.MiningField) MiningSchema(org.dmg.pmml.MiningSchema) ColumnConfig(ml.shifu.shifu.container.obj.ColumnConfig)

Example 57 with MiningField

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

the class MiningSchemaCreator method build.

@Override
public MiningSchema build(BasicML basicML) {
    MiningSchema miningSchema = new MiningSchema();
    boolean isSegExpansionMode = columnConfigList.size() > datasetHeaders.length;
    int segSize = segmentExpansions.size();
    if (basicML != null && basicML instanceof BasicFloatNetwork) {
        BasicFloatNetwork bfn = (BasicFloatNetwork) basicML;
        Set<Integer> featureSet = bfn.getFeatureSet();
        for (ColumnConfig columnConfig : columnConfigList) {
            if (columnConfig.getColumnNum() >= datasetHeaders.length) {
                // in order
                break;
            }
            if (isActiveColumn(featureSet, columnConfig)) {
                if (columnConfig.isTarget()) {
                    List<MiningField> miningFields = createTargetMingFields(columnConfig);
                    miningSchema.addMiningFields(miningFields.toArray(new MiningField[miningFields.size()]));
                } else {
                    miningSchema.addMiningFields(createActiveMingFields(columnConfig));
                }
            } else if (isSegExpansionMode) {
                // even current column not selected, if segment column selected, we should keep raw column
                for (int i = 0; i < segSize; i++) {
                    int newIndex = datasetHeaders.length * (i + 1) + columnConfig.getColumnNum();
                    ColumnConfig cc = columnConfigList.get(newIndex);
                    if (cc.isFinalSelect()) {
                        // if one segment feature is selected, we should put raw column in
                        if (columnConfig.isTarget()) {
                            List<MiningField> miningFields = createTargetMingFields(columnConfig);
                            miningSchema.addMiningFields(miningFields.toArray(new MiningField[miningFields.size()]));
                        } else {
                            miningSchema.addMiningFields(createActiveMingFields(columnConfig));
                        }
                        break;
                    }
                }
            }
        }
    } else {
        for (ColumnConfig columnConfig : columnConfigList) {
            if (columnConfig.getColumnNum() >= datasetHeaders.length) {
                // in order
                break;
            }
            // FIXME, if no variable is selected
            if (columnConfig.isFinalSelect() || columnConfig.isTarget()) {
                if (columnConfig.isTarget()) {
                    List<MiningField> miningFields = createTargetMingFields(columnConfig);
                    miningSchema.addMiningFields(miningFields.toArray(new MiningField[miningFields.size()]));
                } else {
                    miningSchema.addMiningFields(createActiveMingFields(columnConfig));
                }
            } else if (isSegExpansionMode) {
                // even current column not selected, if segment column selected, we should keep raw column
                for (int i = 0; i < segSize; i++) {
                    int newIndex = datasetHeaders.length * (i + 1) + columnConfig.getColumnNum();
                    ColumnConfig cc = columnConfigList.get(newIndex);
                    if (cc.isFinalSelect()) {
                        // if one segment feature is selected, we should put raw column in
                        if (columnConfig.isTarget()) {
                            List<MiningField> miningFields = createTargetMingFields(columnConfig);
                            miningSchema.addMiningFields(miningFields.toArray(new MiningField[miningFields.size()]));
                        } else {
                            miningSchema.addMiningFields(createActiveMingFields(columnConfig));
                        }
                        break;
                    }
                }
            }
        }
    }
    return miningSchema;
}
Also used : MiningField(org.dmg.pmml.MiningField) MiningSchema(org.dmg.pmml.MiningSchema) ColumnConfig(ml.shifu.shifu.container.obj.ColumnConfig) ArrayList(java.util.ArrayList) List(java.util.List) BasicFloatNetwork(ml.shifu.shifu.core.dtrain.dataset.BasicFloatNetwork)

Example 58 with MiningField

use of org.dmg.pmml.MiningField 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)

Example 59 with MiningField

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

the class NeuralNetworkModelIntegrator method isRootInMiningList.

private boolean isRootInMiningList(FieldName root, List<MiningField> miningList) {
    for (int i = 0; i < miningList.size(); i++) {
        MiningField mField = miningList.get(i);
        if (mField.getUsageType() != MiningField.UsageType.ACTIVE)
            continue;
        FieldName mFieldName = mField.getName();
        if (root.equals(mFieldName)) {
            return true;
        }
    }
    return false;
}
Also used : MiningField(org.dmg.pmml.MiningField) FieldName(org.dmg.pmml.FieldName)

Aggregations

MiningField (org.dmg.pmml.MiningField)59 DataField (org.dmg.pmml.DataField)40 Test (org.junit.Test)39 MiningSchema (org.dmg.pmml.MiningSchema)33 DataDictionary (org.dmg.pmml.DataDictionary)25 RegressionModel (org.dmg.pmml.regression.RegressionModel)24 Model (org.dmg.pmml.Model)22 PMMLModelTestUtils.getRandomDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField)20 PMML (org.dmg.pmml.PMML)18 PMMLModelTestUtils.getRandomMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomMiningField)18 PMMLModelTestUtils.getMiningField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getMiningField)17 PMMLModelTestUtils.getDataField (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getDataField)16 OutputField (org.dmg.pmml.OutputField)15 CommonTestingUtils.getFieldsFromDataDictionary (org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary)15 FieldName (org.dmg.pmml.FieldName)12 Target (org.dmg.pmml.Target)11 Targets (org.dmg.pmml.Targets)11 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)11 HashMap (java.util.HashMap)10 List (java.util.List)10