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);
}
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;
}
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;
}
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);
}
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;
}
Aggregations