org.dmg.pmml.mining.MiningModel Java Examples
The following examples show how to use
org.dmg.pmml.mining.MiningModel.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TargetCategoryParser.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
@Override public void pushParent(PMMLObject parent){ super.pushParent(parent); if(parent instanceof MiningModel){ MiningModel miningModel = (MiningModel)parent; processMiningModel(miningModel); } else if(parent instanceof Model){ Model model = (Model)parent; processModel(model); } }
Example #2
Source File: PMMLConverter.java From pyramid with Apache License 2.0 | 6 votes |
static protected MiningModel createMiningModel(List<RegressionTree> regTrees, float base_score, Schema schema){ ContinuousLabel continuousLabel = (ContinuousLabel)schema.getLabel(); Schema segmentSchema = schema.toAnonymousSchema(); List<TreeModel> treeModels = new ArrayList<>(); for(RegressionTree regTree : regTrees){ TreeModel treeModel = regTree.encodeTreeModel(segmentSchema); treeModels.add(treeModel); } MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel)) .setMathContext(MathContext.FLOAT) .setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.SUM, treeModels)) .setTargets(ModelUtil.createRescaleTargets(null, ValueUtil.floatToDouble(base_score), continuousLabel)); return miningModel; }
Example #3
Source File: MiningModelUtil.java From pyramid with Apache License 2.0 | 6 votes |
static public MiningModel createModelChain(List<? extends Model> models, Schema schema){ if(models.size() < 1){ throw new IllegalArgumentException(); } Segmentation segmentation = createSegmentation(Segmentation.MultipleModelMethod.MODEL_CHAIN, models); Model lastModel = Iterables.getLast(models); MiningModel miningModel = new MiningModel(lastModel.getMiningFunction(), ModelUtil.createMiningSchema(schema.getLabel())) .setMathContext(ModelUtil.simplifyMathContext(lastModel.getMathContext())) .setSegmentation(segmentation); return miningModel; }
Example #4
Source File: MultinomialLogisticRegression.java From jpmml-lightgbm with GNU Affero General Public License v3.0 | 6 votes |
@Override public MiningModel encodeMiningModel(List<Tree> trees, Integer numIteration, Schema schema){ Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.DOUBLE); List<MiningModel> miningModels = new ArrayList<>(); CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); for(int i = 0, rows = categoricalLabel.size(), columns = (trees.size() / rows); i < rows; i++){ MiningModel miningModel = createMiningModel(FortranMatrixUtil.getRow(trees, rows, columns, i), numIteration, segmentSchema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("lgbmValue(" + categoricalLabel.getValue(i) + ")"), OpType.CONTINUOUS, DataType.DOUBLE)); miningModels.add(miningModel); } return MiningModelUtil.createClassification(miningModels, RegressionModel.NormalizationMethod.SOFTMAX, true, schema); }
Example #5
Source File: PMMLConverter.java From pyramid with Apache License 2.0 | 6 votes |
static protected MiningModel createMiningModel(List<RegressionTree> regTrees, float base_score, Schema schema){ ContinuousLabel continuousLabel = (ContinuousLabel)schema.getLabel(); Schema segmentSchema = schema.toAnonymousSchema(); List<TreeModel> treeModels = new ArrayList<>(); for(RegressionTree regTree : regTrees){ TreeModel treeModel = regTree.encodeTreeModel(segmentSchema); treeModels.add(treeModel); } MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel)) .setMathContext(MathContext.FLOAT) .setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.SUM, treeModels)) .setTargets(ModelUtil.createRescaleTargets(null, ValueUtil.floatToDouble(base_score), continuousLabel)); return miningModel; }
Example #6
Source File: RandomForestConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
@Override public MiningModel encodeModel(Schema schema){ RGenericVector randomForest = getObject(); RStringVector type = randomForest.getStringElement("type"); RGenericVector forest = randomForest.getGenericElement("forest"); switch(type.asScalar()){ case "regression": return encodeRegression(forest, schema); case "classification": return encodeClassification(forest, schema); default: throw new IllegalArgumentException(); } }
Example #7
Source File: TargetCategoryParser.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
private void processMiningModel(MiningModel miningModel){ Segmentation segmentation = miningModel.getSegmentation(); if(segmentation != null){ Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod(); switch(multipleModelMethod){ case SELECT_FIRST: case SELECT_ALL: case MODEL_CHAIN: { this.targetDataTypes.push(Collections.singletonMap(Evaluator.DEFAULT_TARGET_NAME, null)); this.dataType = null; return; } default: break; } } processModel(miningModel); }
Example #8
Source File: GBTClassificationModelConverter.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 6 votes |
@Override public MiningModel encodeModel(Schema schema){ GBTClassificationModel model = getTransformer(); String lossType = model.getLossType(); switch(lossType){ case "logistic": break; default: throw new IllegalArgumentException("Loss function " + lossType + " is not supported"); } Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.DOUBLE); List<TreeModel> treeModels = TreeModelUtil.encodeDecisionTreeEnsemble(this, segmentSchema); MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(segmentSchema.getLabel())) .setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.WEIGHTED_SUM, treeModels, Doubles.asList(model.treeWeights()))) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("gbtValue"), OpType.CONTINUOUS, DataType.DOUBLE)); return MiningModelUtil.createBinaryLogisticClassification(miningModel, 2d, 0d, RegressionModel.NormalizationMethod.LOGIT, false, schema); }
Example #9
Source File: RangerConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
private MiningModel encodeRegression(RGenericVector ranger, Schema schema){ RGenericVector forest = ranger.getGenericElement("forest"); 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; }
Example #10
Source File: RangerConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
@Override public MiningModel encodeModel(Schema schema){ RGenericVector ranger = getObject(); RStringVector treetype = ranger.getStringElement("treetype"); switch(treetype.asScalar()){ case "Regression": return encodeRegression(ranger, schema); case "Classification": return encodeClassification(ranger, schema); case "Probability estimation": return encodeProbabilityForest(ranger, schema); default: throw new IllegalArgumentException(); } }
Example #11
Source File: GBDTLRClassifier.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
@Override public Model encodeModel(Schema schema){ Classifier gbdt = getGBDT(); MultiOneHotEncoder ohe = getOHE(); LinearClassifier lr = getLR(); CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); SchemaUtil.checkSize(2, categoricalLabel); List<? extends Number> coef = lr.getCoef(); List<? extends Number> intercept = lr.getIntercept(); Schema segmentSchema = schema.toAnonymousSchema(); MiningModel miningModel = GBDTUtil.encodeModel(gbdt, ohe, coef, Iterables.getOnlyElement(intercept), segmentSchema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE)); return MiningModelUtil.createBinaryLogisticClassification(miningModel, 1d, 0d, RegressionModel.NormalizationMethod.LOGIT, lr.hasProbabilityDistribution(), schema); }
Example #12
Source File: MultinomialLogisticRegression.java From jpmml-xgboost with GNU Affero General Public License v3.0 | 6 votes |
@Override public MiningModel encodeMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){ Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.FLOAT); List<MiningModel> miningModels = new ArrayList<>(); CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel(); for(int i = 0, columns = categoricalLabel.size(), rows = (trees.size() / columns); i < columns; i++){ MiningModel miningModel = createMiningModel(CMatrixUtil.getColumn(trees, rows, columns, i), (weights != null) ? CMatrixUtil.getColumn(weights, rows, columns, i) : null, base_score, ntreeLimit, segmentSchema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("xgbValue(" + categoricalLabel.getValue(i) + ")"), OpType.CONTINUOUS, DataType.FLOAT)); miningModels.add(miningModel); } return MiningModelUtil.createClassification(miningModels, RegressionModel.NormalizationMethod.SOFTMAX, true, schema); }
Example #13
Source File: MiningModelEvaluator.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
private List<OutputField> createNestedOutputFields(){ MiningModel miningModel = getModel(); Segmentation segmentation = miningModel.getSegmentation(); List<Segment> segments = segmentation.getSegments(); Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod(); switch(multipleModelMethod){ case SELECT_FIRST: return createNestedOutputFields(getActiveHead(segments)); case SELECT_ALL: // Ignored break; case MODEL_CHAIN: return createNestedOutputFields(getActiveTail(segments)); default: break; } return Collections.emptyList(); }
Example #14
Source File: AdaBoostRegressor.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
@Override public MiningModel encodeModel(Schema schema){ List<? extends Regressor> estimators = getEstimators(); List<? extends Number> estimatorWeights = getEstimatorWeights(); Schema segmentSchema = schema.toAnonymousSchema(); List<Model> models = new ArrayList<>(); for(Regressor estimator : estimators){ Model model = estimator.encodeModel(segmentSchema); models.add(model); } MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel())) .setSegmentation(MiningModelUtil.createSegmentation(MultipleModelMethod.WEIGHTED_MEDIAN, models, estimatorWeights)); return miningModel; }
Example #15
Source File: BaggingClassifier.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
@Override public MiningModel encodeModel(Schema schema){ List<? extends Classifier> estimators = getEstimators(); List<List<Integer>> estimatorsFeatures = getEstimatorsFeatures(); Segmentation.MultipleModelMethod multipleModelMethod = Segmentation.MultipleModelMethod.AVERAGE; for(Classifier estimator : estimators){ if(!estimator.hasProbabilityDistribution()){ multipleModelMethod = Segmentation.MultipleModelMethod.MAJORITY_VOTE; break; } } MiningModel miningModel = BaggingUtil.encodeBagging(estimators, estimatorsFeatures, multipleModelMethod, MiningFunction.CLASSIFICATION, schema) .setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, (CategoricalLabel)schema.getLabel())); return miningModel; }
Example #16
Source File: MiningModelEvaluator.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
@Override public DataField getDefaultDataField(){ MiningModel miningModel = getModel(); Segmentation segmentation = miningModel.getSegmentation(); Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod(); switch(multipleModelMethod){ case SELECT_FIRST: case SELECT_ALL: case MODEL_CHAIN: return null; default: return super.getDefaultDataField(); } }
Example #17
Source File: HingeClassification.java From jpmml-xgboost with GNU Affero General Public License v3.0 | 6 votes |
@Override public MiningModel encodeMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){ Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.FLOAT); Transformation transformation = new FunctionTransformation(PMMLFunctions.THRESHOLD){ @Override public FieldName getName(FieldName name){ return FieldName.create("hinge(" + name + ")"); } @Override public Expression createExpression(FieldRef fieldRef){ Apply apply = (Apply)super.createExpression(fieldRef); apply.addExpressions(PMMLUtil.createConstant(0f)); return apply; } }; MiningModel miningModel = createMiningModel(trees, weights, base_score, ntreeLimit, segmentSchema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("xgbValue"), OpType.CONTINUOUS, DataType.FLOAT, transformation)); return MiningModelUtil.createBinaryLogisticClassification(miningModel, 1d, 0d, RegressionModel.NormalizationMethod.NONE, true, schema); }
Example #18
Source File: VotingRegressor.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 6 votes |
@Override public Model encodeModel(Schema schema){ List<? extends Regressor> estimators = getEstimators(); List<? extends Number> weights = getWeights(); List<Model> models = new ArrayList<>(); for(Regressor estimator : estimators){ Model model = estimator.encodeModel(schema); models.add(model); } Segmentation.MultipleModelMethod multipleModelMethod = (weights != null && weights.size() > 0 ? Segmentation.MultipleModelMethod.WEIGHTED_AVERAGE : Segmentation.MultipleModelMethod.AVERAGE); MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel())) .setSegmentation(MiningModelUtil.createSegmentation(multipleModelMethod, models, weights)); return miningModel; }
Example #19
Source File: AdaConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 6 votes |
@Override public Model encodeModel(Schema schema){ RGenericVector ada = getObject(); RGenericVector model = ada.getGenericElement("model"); RGenericVector trees = model.getGenericElement("trees"); RDoubleVector alpha = model.getDoubleElement("alpha"); List<TreeModel> treeModels = encodeTreeModels(trees); MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(null)) .setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.WEIGHTED_SUM, treeModels, alpha.getValues())) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("adaValue"), OpType.CONTINUOUS, DataType.DOUBLE)); return MiningModelUtil.createBinaryLogisticClassification(miningModel, 2d, 0d, RegressionModel.NormalizationMethod.LOGIT, true, schema); }
Example #20
Source File: GradientBoostingRegressor.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
@Override public MiningModel encodeModel(Schema schema){ HasDefaultValue init = getInit(); Number learningRate = getLearningRate(); return GradientBoostingUtil.encodeGradientBoosting(this, init.getDefaultValue(), learningRate, schema); }
Example #21
Source File: HistGradientBoostingRegressor.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
@Override public MiningModel encodeModel(Schema schema){ List<List<TreePredictor>> predictors = getPredictors(); Number baselinePrediction = getBaselinePrediction(); return HistGradientBoostingUtil.encodeHistGradientBoosting(predictors, Collections.singletonList(baselinePrediction), 0, schema); }
Example #22
Source File: HistGradientBoostingUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
static public MiningModel encodeHistGradientBoosting(List<List<TreePredictor>> predictors, List<? extends Number> baselinePredictions, int column, Schema schema){ List<TreePredictor> treePredictors = predictors.stream() .map(predictor -> predictor.get(column)) .collect(Collectors.toList()); Number baselinePrediction = baselinePredictions.get(column); return encodeHistGradientBoosting(treePredictors, baselinePrediction, schema); }
Example #23
Source File: GBMConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
private MiningModel encodeBinaryClassification(List<TreeModel> treeModels, Double initF, double coefficient, Schema schema){ Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.DOUBLE); MiningModel miningModel = createMiningModel(treeModels, initF, segmentSchema) .setOutput(ModelUtil.createPredictedOutput(FieldName.create("gbmValue"), OpType.CONTINUOUS, DataType.DOUBLE)); return MiningModelUtil.createBinaryLogisticClassification(miningModel, -coefficient, 0d, RegressionModel.NormalizationMethod.LOGIT, true, schema); }
Example #24
Source File: GBMConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 5 votes |
private MiningModel encodeMiningModel(RStringVector distributionName, List<TreeModel> treeModels, Double initF, Schema schema){ switch(distributionName.asScalar()){ case "gaussian": return encodeRegression(treeModels, initF, schema); case "adaboost": return encodeBinaryClassification(treeModels, initF, -2d, schema); case "bernoulli": return encodeBinaryClassification(treeModels, initF, -1d, schema); case "multinomial": return encodeMultinomialClassification(treeModels, initF, schema); default: throw new IllegalArgumentException(); } }
Example #25
Source File: XGBoostRegressionModelConverter.java From jpmml-sparkml-xgboost with GNU Affero General Public License v3.0 | 5 votes |
@Override public MiningModel encodeModel(Schema schema){ XGBoostRegressionModel model = getTransformer(); Booster booster = model.nativeBooster(); return BoosterUtil.encodeBooster(this, booster, schema); }
Example #26
Source File: SelectFirstUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
static private MiningModel encodeModel(MiningFunction miningFunction, List<Object[]> steps, Schema schema){ if(steps.size() < 1){ throw new IllegalArgumentException(); } Label label = schema.getLabel(); List<? extends Feature> features = schema.getFeatures(); Segmentation segmentation = new Segmentation(Segmentation.MultipleModelMethod.SELECT_FIRST, null); Scope scope = new DataFrameScope(FieldName.create("X"), features); for(Object[] step : steps){ String name = TupleUtil.extractElement(step, 0, String.class); Estimator estimator = TupleUtil.extractElement(step, 1, Estimator.class); String predicate = TupleUtil.extractElement(step, 2, String.class); if(!(miningFunction).equals(estimator.getMiningFunction())){ throw new IllegalArgumentException(); } Predicate pmmlPredicate = PredicateTranslator.translate(predicate, scope); Model model = estimator.encodeModel(schema); Segment segment = new Segment(pmmlPredicate, model) .setId(name); segmentation.addSegments(segment); } MiningModel miningModel = new MiningModel(miningFunction, ModelUtil.createMiningSchema(label)) .setSegmentation(segmentation); return miningModel; }
Example #27
Source File: GradientBoostingUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
static public <E extends Estimator & HasEstimatorEnsemble<TreeRegressor> & HasTreeOptions> MiningModel encodeGradientBoosting(E estimator, Number initialPrediction, Number learningRate, Schema schema){ ContinuousLabel continuousLabel = (ContinuousLabel)schema.getLabel(); List<TreeModel> treeModels = TreeUtil.encodeTreeModelEnsemble(estimator, MiningFunction.REGRESSION, schema); MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel)) .setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.SUM, treeModels)) .setTargets(ModelUtil.createRescaleTargets(learningRate, initialPrediction, continuousLabel)); return TreeUtil.transform(estimator, miningModel); }
Example #28
Source File: PMMLConverter.java From pyramid with Apache License 2.0 | 5 votes |
public static PMML encodePMML(FieldName targetField, List<String> targetCategories, FeatureList featureList, List<List<RegressionTree>> regTrees, float base_score, int numClasses){ LKBoostEncoder encoder = new LKBoostEncoder(); if(targetField == null){ targetField = FieldName.create("_target"); } Label label = encodeLabel(targetField, targetCategories, encoder, numClasses); //todo List<Feature> features = new ArrayList<>(); for (int i=0;i<featureList.size();i++){ FieldName fieldName = new FieldName("feature_"+i); DataField dataField = encoder.createDataField(fieldName, OpType.CONTINUOUS, DataType.FLOAT); Feature feature = new ContinuousFeature(encoder, dataField); features.add(feature); } Schema schema = new Schema(label, features); MiningModel miningModel = encodeMiningModel(regTrees, base_score, schema); PMML pmml = encoder.encodePMML(miningModel); return pmml; }
Example #29
Source File: ForestUtil.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
static public <E extends Estimator & HasEstimatorEnsemble<T> & HasTreeOptions, T extends Estimator & HasTree> MiningModel encodeBaseForest(E estimator, Segmentation.MultipleModelMethod multipleModelMethod, MiningFunction miningFunction, Schema schema){ List<TreeModel> treeModels = TreeUtil.encodeTreeModelEnsemble(estimator, miningFunction, schema); MiningModel miningModel = new MiningModel(miningFunction, ModelUtil.createMiningSchema(schema.getLabel())) .setSegmentation(MiningModelUtil.createSegmentation(multipleModelMethod, treeModels)); return TreeUtil.transform(estimator, miningModel); }
Example #30
Source File: PMMLConverter.java From pyramid with Apache License 2.0 | 5 votes |
public static PMML encodePMML(FieldName targetField, List<String> targetCategories, FeatureList featureList, List<RegressionTree> regTrees, float base_score){ LSBoostEncoder encoder = new LSBoostEncoder(); if(targetField == null){ targetField = FieldName.create("_target"); } Label label = encodeLabel(targetField, targetCategories, encoder); //todo List<Feature> features = new ArrayList<>(); for (int i=0;i<featureList.size();i++){ FieldName fieldName = new FieldName("feature_"+i); DataField dataField = encoder.createDataField(fieldName, OpType.CONTINUOUS, DataType.FLOAT); Feature feature = new ContinuousFeature(encoder, dataField); features.add(feature); } Schema schema = new Schema(label, features); MiningModel miningModel = encodeMiningModel(regTrees, base_score, schema); PMML pmml = encoder.encodePMML(miningModel); return pmml; }