Search in sources :

Example 6 with MiningModel

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

the class GBMConverter method createMiningModel.

private static MiningModel createMiningModel(List<TreeModel> treeModels, Double initF, Schema schema) {
    ContinuousLabel continuousLabel = (ContinuousLabel) schema.getLabel();
    MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel)).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.SUM, treeModels)).setTargets(ModelUtil.createRescaleTargets(null, initF, continuousLabel));
    return miningModel;
}
Also used : MiningModel(org.dmg.pmml.mining.MiningModel) ContinuousLabel(org.jpmml.converter.ContinuousLabel)

Example 7 with MiningModel

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

the class IForestConverter method encodeModel.

@Override
public Model encodeModel(Schema schema) {
    RGenericVector iForest = getObject();
    RGenericVector trees = (RGenericVector) iForest.getValue("trees");
    RDoubleVector ntree = (RDoubleVector) iForest.getValue("ntree");
    if (trees == null) {
        throw new IllegalArgumentException();
    }
    final RIntegerVector xrow = (RIntegerVector) trees.getValue("xrow");
    Schema segmentSchema = schema.toAnonymousSchema();
    List<TreeModel> treeModels = new ArrayList<>();
    for (int i = 0; i < ValueUtil.asInt(ntree.asScalar()); i++) {
        TreeModel treeModel = encodeTreeModel(trees, i, segmentSchema);
        treeModels.add(treeModel);
    }
    // "rawPathLength / avgPathLength(xrow)"
    Transformation normalizedPathLength = new AbstractTransformation() {

        @Override
        public FieldName getName(FieldName name) {
            return FieldName.create("normalizedPathLength");
        }

        @Override
        public Expression createExpression(FieldRef fieldRef) {
            return PMMLUtil.createApply("/", fieldRef, PMMLUtil.createConstant(avgPathLength(xrow.asScalar())));
        }
    };
    // "2 ^ (-1 * normalizedPathLength)"
    Transformation anomalyScore = new AbstractTransformation() {

        @Override
        public FieldName getName(FieldName name) {
            return FieldName.create("anomalyScore");
        }

        @Override
        public boolean isFinalResult() {
            return true;
        }

        @Override
        public Expression createExpression(FieldRef fieldRef) {
            return PMMLUtil.createApply("pow", PMMLUtil.createConstant(2d), PMMLUtil.createApply("*", PMMLUtil.createConstant(-1d), fieldRef));
        }
    };
    MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel())).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels)).setOutput(ModelUtil.createPredictedOutput(FieldName.create("rawPathLength"), OpType.CONTINUOUS, DataType.DOUBLE, normalizedPathLength, anomalyScore));
    return miningModel;
}
Also used : Transformation(org.jpmml.converter.Transformation) AbstractTransformation(org.jpmml.converter.AbstractTransformation) FieldRef(org.dmg.pmml.FieldRef) Schema(org.jpmml.converter.Schema) ArrayList(java.util.ArrayList) TreeModel(org.dmg.pmml.tree.TreeModel) MiningModel(org.dmg.pmml.mining.MiningModel) AbstractTransformation(org.jpmml.converter.AbstractTransformation) FieldName(org.dmg.pmml.FieldName)

Example 8 with MiningModel

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

the class RangerConverter method encodeRegression.

private MiningModel encodeRegression(RGenericVector ranger, Schema schema) {
    RGenericVector forest = (RGenericVector) ranger.getValue("forest");
    ScoreEncoder scoreEncoder = new ScoreEncoder() {

        @Override
        public void encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount) {
            node.setScore(ValueUtil.formatValue(splitValue));
        }
    };
    List<TreeModel> treeModels = encodeForest(forest, MiningFunction.REGRESSION, scoreEncoder, schema);
    MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel())).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels));
    return miningModel;
}
Also used : TreeModel(org.dmg.pmml.tree.TreeModel) MiningModel(org.dmg.pmml.mining.MiningModel) Node(org.dmg.pmml.tree.Node)

Example 9 with MiningModel

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

the class RangerConverter method encodeProbabilityForest.

private MiningModel encodeProbabilityForest(RGenericVector ranger, Schema schema) {
    RGenericVector forest = (RGenericVector) ranger.getValue("forest");
    final RStringVector levels = (RStringVector) forest.getValue("levels");
    CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
    ScoreEncoder scoreEncoder = new ScoreEncoder() {

        @Override
        public void encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount) {
            if (splitValue.doubleValue() != 0d || (terminalClassCount == null || terminalClassCount.size() != levels.size())) {
                throw new IllegalArgumentException();
            }
            Double maxProbability = null;
            for (int i = 0; i < terminalClassCount.size(); i++) {
                String value = levels.getValue(i);
                Double probability = ValueUtil.asDouble(terminalClassCount.getValue(i));
                if (maxProbability == null || (maxProbability).compareTo(probability) < 0) {
                    node.setScore(value);
                    maxProbability = probability;
                }
                ScoreDistribution scoreDisctibution = new ScoreDistribution(value, probability);
                node.addScoreDistributions(scoreDisctibution);
            }
        }
    };
    List<TreeModel> treeModels = encodeForest(forest, MiningFunction.CLASSIFICATION, scoreEncoder, schema);
    MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel)).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels)).setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));
    return miningModel;
}
Also used : Node(org.dmg.pmml.tree.Node) ScoreDistribution(org.dmg.pmml.ScoreDistribution) TreeModel(org.dmg.pmml.tree.TreeModel) MiningModel(org.dmg.pmml.mining.MiningModel) CategoricalLabel(org.jpmml.converter.CategoricalLabel)

Example 10 with MiningModel

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

the class XGBoostConverter method encodeModel.

@Override
public MiningModel encodeModel(Schema schema) {
    RGenericVector booster = getObject();
    RNumberVector<?> ntreeLimit = (RNumberVector<?>) booster.getValue("ntreelimit", true);
    RBooleanVector compact = (RBooleanVector) booster.getValue("compact", true);
    Learner learner = ensureLearner();
    Schema xgbSchema = XGBoostUtil.toXGBoostSchema(schema);
    MiningModel miningModel = learner.encodeMiningModel((ntreeLimit != null ? ValueUtil.asInteger(ntreeLimit.asScalar()) : null), (compact != null ? compact.asScalar() : false), xgbSchema);
    return miningModel;
}
Also used : MiningModel(org.dmg.pmml.mining.MiningModel) Schema(org.jpmml.converter.Schema) Learner(org.jpmml.xgboost.Learner)

Aggregations

MiningModel (org.dmg.pmml.mining.MiningModel)17 TreeModel (org.dmg.pmml.tree.TreeModel)12 Schema (org.jpmml.converter.Schema)9 ArrayList (java.util.ArrayList)6 ContinuousLabel (org.jpmml.converter.ContinuousLabel)5 Node (org.dmg.pmml.tree.Node)3 CategoricalLabel (org.jpmml.converter.CategoricalLabel)3 List (java.util.List)1 PipelineModel (org.apache.spark.ml.PipelineModel)1 Transformer (org.apache.spark.ml.Transformer)1 GBTClassificationModel (org.apache.spark.ml.classification.GBTClassificationModel)1 RandomForestClassificationModel (org.apache.spark.ml.classification.RandomForestClassificationModel)1 GBTRegressionModel (org.apache.spark.ml.regression.GBTRegressionModel)1 RandomForestRegressionModel (org.apache.spark.ml.regression.RandomForestRegressionModel)1 CrossValidatorModel (org.apache.spark.ml.tuning.CrossValidatorModel)1 TrainValidationSplitModel (org.apache.spark.ml.tuning.TrainValidationSplitModel)1 FieldName (org.dmg.pmml.FieldName)1 FieldRef (org.dmg.pmml.FieldRef)1 MiningField (org.dmg.pmml.MiningField)1 MiningSchema (org.dmg.pmml.MiningSchema)1