Java Code Examples for org.jpmml.converter.Schema#toAnonymousSchema()

The following examples show how to use org.jpmml.converter.Schema#toAnonymousSchema() . 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: TreeModelUtil.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public <C extends ModelConverter<? extends M> & HasTreeOptions, M extends Model<M> & TreeEnsembleModel<T>, T extends Model<T> & DecisionTreeModel> List<TreeModel> encodeDecisionTreeEnsemble(C converter, PredicateManager predicateManager, Schema schema){
	M model = converter.getTransformer();

	Schema segmentSchema = schema.toAnonymousSchema();

	List<TreeModel> treeModels = new ArrayList<>();

	T[] trees = model.trees();
	for(T tree : trees){
		TreeModel treeModel = encodeDecisionTree(converter, tree, predicateManager, segmentSchema);

		treeModels.add(treeModel);
	}

	return treeModels;
}
 
Example 2
Source File: AdaBoostRegressor.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 3
Source File: GBDTLRClassifier.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 4
Source File: RangerConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
private List<TreeModel> encodeForest(RGenericVector forest, MiningFunction miningFunction, ScoreEncoder scoreEncoder, Schema schema){
	RNumberVector<?> numTrees = forest.getNumericElement("num.trees");
	RGenericVector childNodeIDs = forest.getGenericElement("child.nodeIDs");
	RGenericVector splitVarIDs = forest.getGenericElement("split.varIDs");
	RGenericVector splitValues = forest.getGenericElement("split.values");
	RGenericVector terminalClassCounts = forest.getGenericElement("terminal.class.counts", false);

	Schema segmentSchema = schema.toAnonymousSchema();

	List<TreeModel> treeModels = new ArrayList<>();

	for(int i = 0; i < ValueUtil.asInt(numTrees.asScalar()); i++){
		TreeModel treeModel = encodeTreeModel(miningFunction, scoreEncoder, (RGenericVector)childNodeIDs.getValue(i), (RNumberVector<?>)splitVarIDs.getValue(i), (RNumberVector<?>)splitValues.getValue(i), (terminalClassCounts != null ? (RGenericVector)terminalClassCounts.getValue(i) : null), segmentSchema);

		treeModels.add(treeModel);
	}

	return treeModels;
}
 
Example 5
Source File: RPartEnsembleConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
public List<TreeModel> encodeTreeModels(RGenericVector trees){
	List<TreeModel> result = new ArrayList<>();

	if(trees.size() != this.schemas.size()){
		throw new IllegalArgumentException();
	}

	for(int i = 0; i < trees.size(); i++){
		RGenericVector tree = (RGenericVector)trees.getValue(i);
		Schema schema = this.schemas.get(i);

		RPartConverter converter = this.converters.get(tree);
		if(converter == null){
			throw new IllegalArgumentException();
		}

		Schema segmentSchema = schema.toAnonymousSchema();

		TreeModel treeModel = converter.encodeModel(segmentSchema);

		result.add(treeModel);
	}

	return result;
}
 
Example 6
Source File: PoissonRegression.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeMiningModel(List<Tree> trees, Integer numIteration, Schema schema){
	Schema segmentSchema = schema.toAnonymousSchema();

	MiningModel miningModel = super.encodeMiningModel(trees, numIteration, segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("lgbmValue"), OpType.CONTINUOUS, DataType.DOUBLE));

	return MiningModelUtil.createRegression(miningModel, RegressionModel.NormalizationMethod.EXP, schema);
}
 
Example 7
Source File: LogisticRegression.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){
	Schema segmentSchema = schema.toAnonymousSchema();

	MiningModel miningModel = createMiningModel(trees, weights, base_score, ntreeLimit, segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("xgbValue"), OpType.CONTINUOUS, DataType.FLOAT));

	return MiningModelUtil.createRegression(miningModel, RegressionModel.NormalizationMethod.LOGIT, schema);
}
 
Example 8
Source File: GeneralizedLinearRegression.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){
	Schema segmentSchema = schema.toAnonymousSchema();

	MiningModel miningModel = createMiningModel(trees, weights, base_score, ntreeLimit, segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("xgbValue"), OpType.CONTINUOUS, DataType.FLOAT));

	return MiningModelUtil.createRegression(miningModel, RegressionModel.NormalizationMethod.EXP, schema);
}
 
Example 9
Source File: ObjFunction.java    From jpmml-xgboost with GNU Affero General Public License v3.0 4 votes vote down vote up
static
protected MiningModel createMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){

	if(weights != null){

		if(trees.size() != weights.size()){
			throw new IllegalArgumentException();
		}
	} // End if

	if(ntreeLimit != null){

		if(ntreeLimit > trees.size()){
			throw new IllegalArgumentException("Tree limit " + ntreeLimit + " is greater than the number of trees");
		}

		trees = trees.subList(0, ntreeLimit);

		if(weights != null){
			weights = weights.subList(0, ntreeLimit);
		}
	} // End if

	if(weights != null){
		weights = new ArrayList<>(weights);
	}

	ContinuousLabel continuousLabel = (ContinuousLabel)schema.getLabel();

	Schema segmentSchema = schema.toAnonymousSchema();

	PredicateManager predicateManager = new PredicateManager();

	List<TreeModel> treeModels = new ArrayList<>();

	boolean equalWeights = true;

	Iterator<RegTree> treeIt = trees.iterator();
	Iterator<Float> weightIt = (weights != null ? weights.iterator() : null);

	while(treeIt.hasNext()){
		RegTree tree = treeIt.next();
		Float weight = (weightIt != null ? weightIt.next() : null);

		if(tree.isEmpty()){
			weightIt.remove();

			continue;
		} // End if

		if(weight != null){
			equalWeights &= ValueUtil.isOne(weight);
		}

		TreeModel treeModel = tree.encodeTreeModel(predicateManager, segmentSchema);

		treeModels.add(treeModel);
	}

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel))
		.setMathContext(MathContext.FLOAT)
		.setSegmentation(MiningModelUtil.createSegmentation(equalWeights ? Segmentation.MultipleModelMethod.SUM : Segmentation.MultipleModelMethod.WEIGHTED_SUM, treeModels, weights))
		.setTargets(ModelUtil.createRescaleTargets(null, base_score, continuousLabel));

	return miningModel;
}
 
Example 10
Source File: BaggingUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public <E extends Estimator> MiningModel encodeBagging(List<E> estimators, List<List<Integer>> estimatorsFeatures, Segmentation.MultipleModelMethod multipleModelMethod, MiningFunction miningFunction, Schema schema){
	Schema segmentSchema = schema.toAnonymousSchema();

	List<Model> models = new ArrayList<>();

	for(int i = 0; i < estimators.size(); i++){
		E estimator = estimators.get(i);
		List<Integer> estimatorFeatures = estimatorsFeatures.get(i);

		Schema estimatorSchema = segmentSchema.toSubSchema(Ints.toArray(estimatorFeatures));

		Model model = estimator.encodeModel(estimatorSchema);

		models.add(model);
	}

	MiningModel miningModel = new MiningModel(miningFunction, ModelUtil.createMiningSchema(schema.getLabel()))
		.setSegmentation(MiningModelUtil.createSegmentation(multipleModelMethod, models));

	return miningModel;
}
 
Example 11
Source File: RandomForestConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
private MiningModel encodeRegression(RGenericVector forest, Schema schema){
	RNumberVector<?> leftDaughter = forest.getNumericElement("leftDaughter");
	RNumberVector<?> rightDaughter = forest.getNumericElement("rightDaughter");
	RDoubleVector nodepred = forest.getDoubleElement("nodepred");
	RNumberVector<?> bestvar = forest.getNumericElement("bestvar");
	RDoubleVector xbestsplit = forest.getDoubleElement("xbestsplit");
	RIntegerVector nrnodes = forest.getIntegerElement("nrnodes");
	RNumberVector<?> ntree = forest.getNumericElement("ntree");

	ScoreEncoder<Double> scoreEncoder = new ScoreEncoder<Double>(){

		@Override
		public Double encode(Double value){
			return 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;
}
 
Example 12
Source File: RandomForestConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
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;
}
 
Example 13
Source File: ObjectiveFunction.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 3 votes vote down vote up
protected MiningModel createMiningModel(List<Tree> trees, Integer numIteration, Schema schema){
	ContinuousLabel continuousLabel = (ContinuousLabel)schema.getLabel();

	Schema segmentSchema = schema.toAnonymousSchema();

	PredicateManager predicateManager = new PredicateManager();

	List<TreeModel> treeModels = new ArrayList<>();

	if(numIteration != null){

		if(numIteration > trees.size()){
			throw new IllegalArgumentException("Tree limit " + numIteration + " is greater than the number of trees");
		}

		trees = trees.subList(0, numIteration);
	}

	for(Tree tree : trees){
		TreeModel treeModel = tree.encodeTreeModel(predicateManager, segmentSchema);

		treeModels.add(treeModel);
	}

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel))
		.setSegmentation(MiningModelUtil.createSegmentation(this.average_output_ ? Segmentation.MultipleModelMethod.AVERAGE : Segmentation.MultipleModelMethod.SUM, treeModels));

	return miningModel;
}