use of org.jpmml.converter.Schema 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 = new Schema(new ContinuousLabel(null, DataType.DOUBLE), schema.getFeatures());
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(FieldName.create("gbmValue(" + categoricalLabel.getValue(i) + ")"), OpType.CONTINUOUS, DataType.DOUBLE));
miningModels.add(miningModel);
}
return MiningModelUtil.createClassification(miningModels, RegressionModel.NormalizationMethod.SOFTMAX, true, schema);
}
use of org.jpmml.converter.Schema 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;
}
use of org.jpmml.converter.Schema 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;
}
use of org.jpmml.converter.Schema in project jpmml-r by jpmml.
the class RandomForestConverter method encodeClassification.
private MiningModel encodeClassification(RGenericVector forest, final Schema schema) {
RNumberVector<?> bestvar = (RNumberVector<?>) forest.getValue("bestvar");
RNumberVector<?> treemap = (RNumberVector<?>) forest.getValue("treemap");
RIntegerVector nodepred = (RIntegerVector) forest.getValue("nodepred");
RDoubleVector xbestsplit = (RDoubleVector) forest.getValue("xbestsplit");
RIntegerVector nrnodes = (RIntegerVector) forest.getValue("nrnodes");
RDoubleVector ntree = (RDoubleVector) forest.getValue("ntree");
int rows = nrnodes.asScalar();
int columns = ValueUtil.asInt(ntree.asScalar());
final CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
ScoreEncoder<Integer> scoreEncoder = new ScoreEncoder<Integer>() {
@Override
public String 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.Schema in project jpmml-r by jpmml.
the class RandomForestConverter method encodeRegression.
private MiningModel encodeRegression(RGenericVector forest, final Schema schema) {
RNumberVector<?> leftDaughter = (RNumberVector<?>) forest.getValue("leftDaughter");
RNumberVector<?> rightDaughter = (RNumberVector<?>) forest.getValue("rightDaughter");
RDoubleVector nodepred = (RDoubleVector) forest.getValue("nodepred");
RNumberVector<?> bestvar = (RNumberVector<?>) forest.getValue("bestvar");
RDoubleVector xbestsplit = (RDoubleVector) forest.getValue("xbestsplit");
RIntegerVector nrnodes = (RIntegerVector) forest.getValue("nrnodes");
RDoubleVector ntree = (RDoubleVector) forest.getValue("ntree");
ScoreEncoder<Double> scoreEncoder = new ScoreEncoder<Double>() {
@Override
public String encode(Double value) {
return ValueUtil.formatValue(value);
}
};
int rows = nrnodes.asScalar();
int columns = ValueUtil.asInt(ntree.asScalar());
Schema segmentSchema = schema.toAnonymousSchema();
List<TreeModel> treeModels = new ArrayList<>();
for (int i = 0; i < columns; i++) {
TreeModel treeModel = encodeTreeModel(MiningFunction.REGRESSION, scoreEncoder, FortranMatrixUtil.getColumn(leftDaughter.getValues(), rows, columns, i), FortranMatrixUtil.getColumn(rightDaughter.getValues(), rows, columns, i), 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.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel())).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels));
return miningModel;
}
Aggregations