Search in sources :

Example 21 with CategoricalLabel

use of org.jpmml.converter.CategoricalLabel in project jpmml-r by jpmml.

the class RPartConverter method encodeClassification.

private TreeModel encodeClassification(RGenericVector frame, RIntegerVector rowNames, RVector<?> var, RIntegerVector n, int[][] splitInfo, RNumberVector<?> splits, RIntegerVector csplit, Schema schema) {
    RDoubleVector yval2 = frame.getDoubleElement("yval2");
    CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
    List<?> categories = categoricalLabel.getValues();
    boolean hasScoreDistribution = hasScoreDistribution();
    ScoreEncoder scoreEncoder = new ScoreEncoder() {

        private List<Integer> classes = null;

        private List<List<? extends Number>> recordCounts = null;

        {
            int rows = rowNames.size();
            int columns = 1 + (2 * categories.size()) + 1;
            List<Integer> classes = ValueUtil.asIntegers(FortranMatrixUtil.getColumn(yval2.getValues(), rows, columns, 0));
            this.classes = new ArrayList<>(classes);
            if (hasScoreDistribution) {
                this.recordCounts = new ArrayList<>();
                for (int i = 0; i < categories.size(); i++) {
                    List<? extends Number> recordCounts = FortranMatrixUtil.getColumn(yval2.getValues(), rows, columns, 1 + i);
                    this.recordCounts.add(new ArrayList<>(recordCounts));
                }
            }
        }

        @Override
        public Node encode(Node node, int offset) {
            Object score = categories.get(this.classes.get(offset) - 1);
            Integer recordCount = n.getValue(offset);
            node.setScore(score).setRecordCount(recordCount);
            if (hasScoreDistribution) {
                node = new ClassifierNode(node);
                List<ScoreDistribution> scoreDistributions = node.getScoreDistributions();
                for (int i = 0; i < categories.size(); i++) {
                    List<? extends Number> recordCounts = this.recordCounts.get(i);
                    ScoreDistribution scoreDistribution = new ScoreDistribution().setValue(categories.get(i)).setRecordCount(recordCounts.get(offset));
                    scoreDistributions.add(scoreDistribution);
                }
            }
            return node;
        }
    };
    Node root = encodeNode(True.INSTANCE, 1, rowNames, var, n, splitInfo, splits, csplit, scoreEncoder, schema);
    TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(schema.getLabel()), root);
    if (hasScoreDistribution) {
        treeModel.setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));
    }
    return configureTreeModel(treeModel);
}
Also used : Node(org.dmg.pmml.tree.Node) ClassifierNode(org.dmg.pmml.tree.ClassifierNode) CountingLeafNode(org.dmg.pmml.tree.CountingLeafNode) CountingBranchNode(org.dmg.pmml.tree.CountingBranchNode) ScoreDistribution(org.dmg.pmml.ScoreDistribution) TreeModel(org.dmg.pmml.tree.TreeModel) CategoricalLabel(org.jpmml.converter.CategoricalLabel) ArrayList(java.util.ArrayList) List(java.util.List) ClassifierNode(org.dmg.pmml.tree.ClassifierNode)

Example 22 with CategoricalLabel

use of org.jpmml.converter.CategoricalLabel in project jpmml-r by jpmml.

the class RandomForestConverter method encodeClassification.

private MiningModel encodeClassification(RGenericVector forest, Schema schema) {
    RNumberVector<?> bestvar = forest.getNumericElement("bestvar");
    RNumberVector<?> treemap = forest.getNumericElement("treemap");
    RIntegerVector nodepred = forest.getIntegerElement("nodepred");
    RDoubleVector xbestsplit = forest.getDoubleElement("xbestsplit");
    RIntegerVector nrnodes = forest.getIntegerElement("nrnodes");
    RDoubleVector ntree = forest.getDoubleElement("ntree");
    int rows = nrnodes.asScalar();
    int columns = ValueUtil.asInt(ntree.asScalar());
    CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
    ScoreEncoder<Integer> scoreEncoder = new ScoreEncoder<Integer>() {

        @Override
        public Object encode(Integer value) {
            return categoricalLabel.getValue(value - 1);
        }
    };
    Schema segmentSchema = schema.toAnonymousSchema();
    List<TreeModel> treeModels = new ArrayList<>();
    for (int i = 0; i < columns; i++) {
        List<? extends Number> daughters = FortranMatrixUtil.getColumn(treemap.getValues(), 2 * rows, columns, i);
        TreeModel treeModel = encodeTreeModel(MiningFunction.CLASSIFICATION, scoreEncoder, FortranMatrixUtil.getColumn(daughters, rows, 2, 0), FortranMatrixUtil.getColumn(daughters, rows, 2, 1), FortranMatrixUtil.getColumn(nodepred.getValues(), rows, columns, i), FortranMatrixUtil.getColumn(bestvar.getValues(), rows, columns, i), FortranMatrixUtil.getColumn(xbestsplit.getValues(), rows, columns, i), segmentSchema);
        treeModels.add(treeModel);
    }
    MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel)).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.MAJORITY_VOTE, treeModels)).setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));
    return miningModel;
}
Also used : Schema(org.jpmml.converter.Schema) ArrayList(java.util.ArrayList) TreeModel(org.dmg.pmml.tree.TreeModel) MiningModel(org.dmg.pmml.mining.MiningModel) CategoricalLabel(org.jpmml.converter.CategoricalLabel)

Example 23 with CategoricalLabel

use of org.jpmml.converter.CategoricalLabel in project jpmml-r by jpmml.

the class NNetConverter method encodeModel.

@Override
public Model encodeModel(Schema schema) {
    RGenericVector nnet = getObject();
    RDoubleVector n = nnet.getDoubleElement("n");
    RBooleanVector linout = nnet.getBooleanElement("linout", false);
    RBooleanVector softmax = nnet.getBooleanElement("softmax", false);
    RBooleanVector censored = nnet.getBooleanElement("censored", false);
    RDoubleVector wts = nnet.getDoubleElement("wts");
    RStringVector lev = nnet.getStringElement("lev", false);
    if (n.size() != 3) {
        throw new IllegalArgumentException();
    }
    Label label = schema.getLabel();
    List<? extends Feature> features = schema.getFeatures();
    MiningFunction miningFunction;
    if (lev == null) {
        if (linout != null && !linout.asScalar()) {
            throw new IllegalArgumentException();
        }
        miningFunction = MiningFunction.REGRESSION;
    } else {
        miningFunction = MiningFunction.CLASSIFICATION;
    }
    int nInput = ValueUtil.asInt(n.getValue(0));
    SchemaUtil.checkSize(nInput, features);
    NeuralInputs neuralInputs = NeuralNetworkUtil.createNeuralInputs(features, DataType.DOUBLE);
    int offset = 0;
    List<NeuralLayer> neuralLayers = new ArrayList<>();
    List<? extends NeuralEntity> entities = neuralInputs.getNeuralInputs();
    int nHidden = ValueUtil.asInt(n.getValue(1));
    if (nHidden > 0) {
        NeuralLayer neuralLayer = encodeNeuralLayer("hidden", nHidden, entities, wts, offset).setActivationFunction(NeuralNetwork.ActivationFunction.LOGISTIC);
        offset += (nHidden * (entities.size() + 1));
        neuralLayers.add(neuralLayer);
        entities = neuralLayer.getNeurons();
    }
    int nOutput = ValueUtil.asInt(n.getValue(2));
    if (nOutput == 1) {
        NeuralLayer neuralLayer = encodeNeuralLayer("output", nOutput, entities, wts, offset);
        offset += (nOutput * (entities.size() + 1));
        neuralLayers.add(neuralLayer);
        entities = neuralLayer.getNeurons();
        switch(miningFunction) {
            case REGRESSION:
                break;
            case CLASSIFICATION:
                {
                    List<NeuralLayer> transformationNeuralLayers = NeuralNetworkUtil.createBinaryLogisticTransformation(Iterables.getOnlyElement(entities));
                    neuralLayers.addAll(transformationNeuralLayers);
                    neuralLayer = Iterables.getLast(transformationNeuralLayers);
                    entities = neuralLayer.getNeurons();
                }
                break;
        }
    } else if (nOutput > 1) {
        NeuralLayer neuralLayer = encodeNeuralLayer("output", nOutput, entities, wts, offset);
        if (softmax != null && softmax.asScalar()) {
            if (censored != null && censored.asScalar()) {
                throw new IllegalArgumentException();
            }
            neuralLayer.setNormalizationMethod(NeuralNetwork.NormalizationMethod.SOFTMAX);
        }
        offset += (nOutput * (entities.size() + 1));
        neuralLayers.add(neuralLayer);
        entities = neuralLayer.getNeurons();
    } else {
        throw new IllegalArgumentException();
    }
    NeuralNetwork neuralNetwork = new NeuralNetwork(miningFunction, NeuralNetwork.ActivationFunction.IDENTITY, ModelUtil.createMiningSchema(label), neuralInputs, neuralLayers);
    switch(miningFunction) {
        case REGRESSION:
            neuralNetwork.setNeuralOutputs(NeuralNetworkUtil.createRegressionNeuralOutputs(entities, (ContinuousLabel) label));
            break;
        case CLASSIFICATION:
            neuralNetwork.setNeuralOutputs(NeuralNetworkUtil.createClassificationNeuralOutputs(entities, (CategoricalLabel) label)).setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, (CategoricalLabel) label));
            break;
    }
    return neuralNetwork;
}
Also used : NeuralInputs(org.dmg.pmml.neural_network.NeuralInputs) CategoricalLabel(org.jpmml.converter.CategoricalLabel) ContinuousLabel(org.jpmml.converter.ContinuousLabel) Label(org.jpmml.converter.Label) ArrayList(java.util.ArrayList) NeuralLayer(org.dmg.pmml.neural_network.NeuralLayer) NeuralNetwork(org.dmg.pmml.neural_network.NeuralNetwork) CategoricalLabel(org.jpmml.converter.CategoricalLabel) ArrayList(java.util.ArrayList) List(java.util.List) MiningFunction(org.dmg.pmml.MiningFunction) ContinuousLabel(org.jpmml.converter.ContinuousLabel)

Example 24 with CategoricalLabel

use of org.jpmml.converter.CategoricalLabel in project jpmml-r by jpmml.

the class RExpEncoder method setLabel.

public void setLabel(DataField dataField) {
    Label label;
    OpType opType = dataField.requireOpType();
    switch(opType) {
        case CATEGORICAL:
            label = new CategoricalLabel(dataField);
            break;
        case CONTINUOUS:
            label = new ContinuousLabel(dataField);
            break;
        default:
            throw new IllegalArgumentException();
    }
    setLabel(label);
}
Also used : CategoricalLabel(org.jpmml.converter.CategoricalLabel) CategoricalLabel(org.jpmml.converter.CategoricalLabel) ContinuousLabel(org.jpmml.converter.ContinuousLabel) Label(org.jpmml.converter.Label) OpType(org.dmg.pmml.OpType) ContinuousLabel(org.jpmml.converter.ContinuousLabel)

Example 25 with CategoricalLabel

use of org.jpmml.converter.CategoricalLabel in project jpmml-r by jpmml.

the class RangerConverter method encodeProbabilityForest.

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

        @Override
        public Node encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount) {
            if (splitValue.doubleValue() != 0d || (terminalClassCount == null || terminalClassCount.size() != levels.size())) {
                throw new IllegalArgumentException();
            }
            node = new ClassifierNode(node);
            List<ScoreDistribution> scoreDistributions = node.getScoreDistributions();
            Number maxProbability = null;
            for (int i = 0; i < terminalClassCount.size(); i++) {
                String value = levels.getValue(i);
                Number probability = terminalClassCount.getValue(i);
                if (maxProbability == null || ((Comparable) maxProbability).compareTo(probability) < 0) {
                    node.setScore(value);
                    maxProbability = probability;
                }
                ScoreDistribution scoreDistribution = new ScoreDistribution(value, probability);
                scoreDistributions.add(scoreDistribution);
            }
            return node;
        }
    };
    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) ClassifierNode(org.dmg.pmml.tree.ClassifierNode) BranchNode(org.dmg.pmml.tree.BranchNode) LeafNode(org.dmg.pmml.tree.LeafNode) ScoreDistribution(org.dmg.pmml.ScoreDistribution) TreeModel(org.dmg.pmml.tree.TreeModel) MiningModel(org.dmg.pmml.mining.MiningModel) CategoricalLabel(org.jpmml.converter.CategoricalLabel) ClassifierNode(org.dmg.pmml.tree.ClassifierNode)

Aggregations

CategoricalLabel (org.jpmml.converter.CategoricalLabel)28 ArrayList (java.util.ArrayList)12 List (java.util.List)9 TreeModel (org.dmg.pmml.tree.TreeModel)8 MiningFunction (org.dmg.pmml.MiningFunction)7 MiningModel (org.dmg.pmml.mining.MiningModel)7 Feature (org.jpmml.converter.Feature)7 Label (org.jpmml.converter.Label)7 ContinuousFeature (org.jpmml.converter.ContinuousFeature)6 OutputField (org.dmg.pmml.OutputField)5 ScoreDistribution (org.dmg.pmml.ScoreDistribution)5 ClassifierNode (org.dmg.pmml.tree.ClassifierNode)5 Node (org.dmg.pmml.tree.Node)5 CategoricalFeature (org.jpmml.converter.CategoricalFeature)5 RegressionModel (org.dmg.pmml.regression.RegressionModel)4 RegressionTable (org.dmg.pmml.regression.RegressionTable)4 ContinuousLabel (org.jpmml.converter.ContinuousLabel)4 Schema (org.jpmml.converter.Schema)4 Model (org.dmg.pmml.Model)3 MultilayerPerceptronClassificationModel (org.apache.spark.ml.classification.MultilayerPerceptronClassificationModel)2