use of org.dmg.pmml.mining.MiningModel in project jpmml-r by jpmml.
the class GBMConverter method createMiningModel.
private static MiningModel createMiningModel(List<TreeModel> treeModels, Double initF, Schema schema) {
ContinuousLabel continuousLabel = (ContinuousLabel) schema.getLabel();
MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel)).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.SUM, treeModels)).setTargets(ModelUtil.createRescaleTargets(null, initF, continuousLabel));
return miningModel;
}
use of org.dmg.pmml.mining.MiningModel 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.dmg.pmml.mining.MiningModel in project jpmml-r by jpmml.
the class RangerConverter method encodeRegression.
private MiningModel encodeRegression(RGenericVector ranger, Schema schema) {
RGenericVector forest = (RGenericVector) ranger.getValue("forest");
ScoreEncoder scoreEncoder = new ScoreEncoder() {
@Override
public void encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount) {
node.setScore(ValueUtil.formatValue(splitValue));
}
};
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.mining.MiningModel in project jpmml-r by jpmml.
the class RangerConverter method encodeProbabilityForest.
private MiningModel encodeProbabilityForest(RGenericVector ranger, Schema schema) {
RGenericVector forest = (RGenericVector) ranger.getValue("forest");
final RStringVector levels = (RStringVector) forest.getValue("levels");
CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
ScoreEncoder scoreEncoder = new ScoreEncoder() {
@Override
public void encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount) {
if (splitValue.doubleValue() != 0d || (terminalClassCount == null || terminalClassCount.size() != levels.size())) {
throw new IllegalArgumentException();
}
Double maxProbability = null;
for (int i = 0; i < terminalClassCount.size(); i++) {
String value = levels.getValue(i);
Double probability = ValueUtil.asDouble(terminalClassCount.getValue(i));
if (maxProbability == null || (maxProbability).compareTo(probability) < 0) {
node.setScore(value);
maxProbability = probability;
}
ScoreDistribution scoreDisctibution = new ScoreDistribution(value, probability);
node.addScoreDistributions(scoreDisctibution);
}
}
};
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.mining.MiningModel 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;
}
Aggregations