use of org.dmg.pmml.tree.TreeModel 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);
}
use of org.dmg.pmml.tree.TreeModel in project jpmml-r by jpmml.
the class RPartConverter method encodeRegression.
private TreeModel encodeRegression(RGenericVector frame, RIntegerVector rowNames, RVector<?> var, RIntegerVector n, int[][] splitInfo, RNumberVector<?> splits, RIntegerVector csplit, Schema schema) {
RNumberVector<?> yval = frame.getNumericElement("yval");
ScoreEncoder scoreEncoder = new ScoreEncoder() {
@Override
public Node encode(Node node, int offset) {
Number score = yval.getValue(offset);
Number recordCount = n.getValue(offset);
node.setScore(score).setRecordCount(recordCount);
return node;
}
};
Node root = encodeNode(True.INSTANCE, 1, rowNames, var, n, splitInfo, splits, csplit, scoreEncoder, schema);
TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), root);
return configureTreeModel(treeModel);
}
use of org.dmg.pmml.tree.TreeModel 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;
}
use of org.dmg.pmml.tree.TreeModel in project jpmml-r by jpmml.
the class RangerConverter method encodeRegression.
private MiningModel encodeRegression(RGenericVector forest, Schema schema) {
ScoreEncoder scoreEncoder = new ScoreEncoder() {
@Override
public Node encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount) {
node.setScore(splitValue);
return node;
}
};
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;
}
use of org.dmg.pmml.tree.TreeModel in project jpmml-r by jpmml.
the class GBMConverter method encodeMultinomialClassification.
private MiningModel encodeMultinomialClassification(List<TreeModel> treeModels, Double initF, Schema schema) {
CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.DOUBLE);
List<Model> miningModels = new ArrayList<>();
for (int i = 0, columns = categoricalLabel.size(), rows = (treeModels.size() / columns); i < columns; i++) {
MiningModel miningModel = createMiningModel(CMatrixUtil.getColumn(treeModels, rows, columns, i), initF, segmentSchema).setOutput(ModelUtil.createPredictedOutput(FieldNameUtil.create("gbmValue", categoricalLabel.getValue(i)), OpType.CONTINUOUS, DataType.DOUBLE));
miningModels.add(miningModel);
}
return MiningModelUtil.createClassification(miningModels, RegressionModel.NormalizationMethod.SOFTMAX, true, schema);
}
Aggregations