Search in sources :

Example 26 with CategoricalLabel

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

the class LRMConverter method encodeModel.

@Override
public Model encodeModel(Schema schema) {
    RGenericVector lrm = getObject();
    RDoubleVector coefficients = lrm.getDoubleElement("coefficients");
    CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
    SchemaUtil.checkSize(2, categoricalLabel);
    Object targetCategory = categoricalLabel.getValue(1);
    Double intercept = coefficients.getElement(getInterceptName(), false);
    List<? extends Feature> features = schema.getFeatures();
    SchemaUtil.checkSize(coefficients.size() - (intercept != null ? 1 : 0), features);
    List<Double> featureCoefficients = getFeatureCoefficients(features, coefficients);
    GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), null, null, null).setLinkFunction(GeneralRegressionModel.LinkFunction.LOGIT).setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));
    GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, targetCategory);
    return generalRegressionModel;
}
Also used : CategoricalLabel(org.jpmml.converter.CategoricalLabel) GeneralRegressionModel(org.dmg.pmml.general_regression.GeneralRegressionModel)

Example 27 with CategoricalLabel

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

the class BoostingConverter method encodeModel.

@Override
public Model encodeModel(Schema schema) {
    RGenericVector boosting = getObject();
    RGenericVector trees = boosting.getGenericElement("trees");
    RDoubleVector weights = boosting.getDoubleElement("weights");
    CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
    List<TreeModel> treeModels = encodeTreeModels(trees);
    MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel)).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.WEIGHTED_MAJORITY_VOTE, treeModels, weights.getValues())).setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));
    return miningModel;
}
Also used : TreeModel(org.dmg.pmml.tree.TreeModel) MiningModel(org.dmg.pmml.mining.MiningModel) CategoricalLabel(org.jpmml.converter.CategoricalLabel)

Example 28 with CategoricalLabel

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

the class BinaryTreeConverter method encodeClassificationScore.

private static Node encodeClassificationScore(Node node, RDoubleVector probabilities, Schema schema) {
    CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
    SchemaUtil.checkSize(probabilities.size(), categoricalLabel);
    node = new ClassifierNode(node);
    List<ScoreDistribution> scoreDistributions = node.getScoreDistributions();
    Double maxProbability = null;
    for (int i = 0; i < categoricalLabel.size(); i++) {
        Object value = categoricalLabel.getValue(i);
        Double probability = probabilities.getValue(i);
        if (maxProbability == null || (maxProbability).compareTo(probability) < 0) {
            node.setScore(value);
            maxProbability = probability;
        }
        ScoreDistribution scoreDistribution = new ScoreDistribution(value, probability);
        scoreDistributions.add(scoreDistribution);
    }
    return node;
}
Also used : ScoreDistribution(org.dmg.pmml.ScoreDistribution) 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