org.dmg.pmml.mining.Segmentation Java Examples

The following examples show how to use org.dmg.pmml.mining.Segmentation. 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: GBTClassificationModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #2
Source File: AdaConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #3
Source File: RangerConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
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 #4
Source File: BoostingConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	RGenericVector boosting = getObject();

	RGenericVector trees = boosting.getGenericElement("trees");
	RDoubleVector weights = boosting.getDoubleElement("weights");

	CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel();

	List<TreeModel> treeModels = encodeTreeModels(trees);

	MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.WEIGHTED_MAJORITY_VOTE, treeModels, weights.getValues()))
		.setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));

	return miningModel;
}
 
Example #5
Source File: BaggingClassifier.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 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 #6
Source File: PMMLConverter.java    From pyramid with Apache License 2.0 6 votes vote down vote up
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 #7
Source File: MiningModelUtil.java    From pyramid with Apache License 2.0 6 votes vote down vote up
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 #8
Source File: PMMLConverter.java    From pyramid with Apache License 2.0 6 votes vote down vote up
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 #9
Source File: VotingRegressor.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #10
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
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 #11
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #12
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
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 #13
Source File: MiningModelUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public SegmentResult asSegmentResult(Segmentation.MultipleModelMethod multipleModelMethod, Map<FieldName, ?> predictions){

	switch(multipleModelMethod){
		case SELECT_FIRST:
		case SELECT_ALL:
		case MODEL_CHAIN:
			{
				if(predictions instanceof SegmentResult){
					SegmentResult segmentResult = (SegmentResult)predictions;

					return segmentResult;
				}
			}
			break;
		default:
			break;
	}

	return null;
}
 
Example #14
Source File: FieldResolver.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private List<Output> getEarlierOutputs(Segmentation segmentation, Segment targetSegment){
	List<Output> result = new ArrayList<>();

	Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();
	switch(multipleModelMethod){
		case MODEL_CHAIN:
			break;
		default:
			return Collections.emptyList();
	}

	List<Segment> segments = segmentation.getSegments();
	for(Segment segment : segments){
		Model model = segment.getModel();

		if(targetSegment != null && (targetSegment).equals(segment)){
			break;
		}

		Output output = model.getOutput();
		if(output != null){
			result.add(output);
		}
	}

	return result;
}
 
Example #15
Source File: MiningModelUtil.java    From pyramid with Apache License 2.0 5 votes vote down vote up
static
public Segmentation createSegmentation(Segmentation.MultipleModelMethod multipleModelMethod, List<? extends Model> models, List<? extends Number> weights){

    if((weights != null) && (models.size() != weights.size())){
        throw new IllegalArgumentException();
    }

    List<Segment> segments = new ArrayList<>();

    for(int i = 0; i < models.size(); i++){
        Model model = models.get(i);
        Number weight = (weights != null ? weights.get(i) : null);

        Segment segment = new Segment()
                .setId(String.valueOf(i + 1))
                .setPredicate(new True())
                .setModel(model);

        if(weight != null && !ValueUtil.isOne(weight)){
            segment.setWeight(ValueUtil.asDouble(weight));
        }

        segments.add(segment);
    }

    return new Segmentation(multipleModelMethod, segments);
}
 
Example #16
Source File: UnsupportedMarkupInspector.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public VisitorAction visit(Segmentation segmentation){
	LocalTransformations localTransformations = segmentation.getLocalTransformations();

	if(localTransformations != null){
		report(new UnsupportedElementException(localTransformations));
	}

	return super.visit(segmentation);
}
 
Example #17
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private Map<FieldName, ?> getSegmentationResult(Set<Segmentation.MultipleModelMethod> multipleModelMethods, List<SegmentResult> segmentResults){
	MiningModel miningModel = getModel();

	Segmentation segmentation = miningModel.getSegmentation();

	Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();
	switch(multipleModelMethod){
		case SELECT_FIRST:
			if(!segmentResults.isEmpty()){
				return segmentResults.get(0);
			}
			break;
		case SELECT_ALL:
			return selectAll(segmentResults);
		case MODEL_CHAIN:
			if(!segmentResults.isEmpty()){
				return segmentResults.get(segmentResults.size() - 1);
			}
			break;
		default:
			if(!(multipleModelMethods).contains(multipleModelMethod)){
				throw new UnsupportedAttributeException(segmentation, multipleModelMethod);
			}
			break;
	}

	// "If no segments have predicates that evaluate to true, then the result is a missing value"
	if(segmentResults.isEmpty()){
		return Collections.singletonMap(getTargetName(), null);
	}

	return null;
}
 
Example #18
Source File: GBMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private 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;
}
 
Example #19
Source File: VotingClassifier.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Segmentation.MultipleModelMethod parseVoting(String voting, boolean weighted){

	switch(voting){
		case "hard":
			return (weighted ? Segmentation.MultipleModelMethod.WEIGHTED_MAJORITY_VOTE : Segmentation.MultipleModelMethod.MAJORITY_VOTE);
		case "soft":
			return (weighted ? Segmentation.MultipleModelMethod.WEIGHTED_AVERAGE : Segmentation.MultipleModelMethod.AVERAGE);
		default:
			throw new IllegalArgumentException(voting);
	}
}
 
Example #20
Source File: SelectFirstUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #21
Source File: GradientBoostingUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #22
Source File: GBTRegressionModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeModel(Schema schema){
	GBTRegressionModel model = getTransformer();

	List<TreeModel> treeModels = TreeModelUtil.encodeDecisionTreeEnsemble(this, schema);

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.WEIGHTED_SUM, treeModels, Doubles.asList(model.treeWeights())));

	return miningModel;
}
 
Example #23
Source File: BaggingRegressor.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeModel(Schema schema){
	List<? extends Regressor> estimators = getEstimators();
	List<List<Integer>> estimatorsFeatures = getEstimatorsFeatures();

	MiningModel miningModel = BaggingUtil.encodeBagging(estimators, estimatorsFeatures, Segmentation.MultipleModelMethod.AVERAGE, MiningFunction.REGRESSION, schema);

	return miningModel;
}
 
Example #24
Source File: RandomForestClassificationModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeModel(Schema schema){
	List<TreeModel> treeModels = TreeModelUtil.encodeDecisionTreeEnsemble(this, schema);

	MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(schema.getLabel()))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels));

	return miningModel;
}
 
Example #25
Source File: RandomForestRegressionModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeModel(Schema schema){
	List<TreeModel> treeModels = TreeModelUtil.encodeDecisionTreeEnsemble(this, schema);

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels));

	return miningModel;
}
 
Example #26
Source File: ForestUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #27
Source File: ForestClassifier.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MiningModel encodeModel(Schema schema){
	MiningModel miningModel = ForestUtil.encodeBaseForest(this, Segmentation.MultipleModelMethod.AVERAGE, MiningFunction.CLASSIFICATION, schema)
		.setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, (CategoricalLabel)schema.getLabel()));

	return miningModel;
}
 
Example #28
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private ModelEvaluator<?> createSegmentModelEvaluator(String segmentId, Model model){
	MiningModel miningModel = getModel();

	MiningFunction miningFunction = miningModel.getMiningFunction();

	Segmentation segmentation = miningModel.getSegmentation();

	Configuration configuration = ensureConfiguration();

	ModelEvaluatorFactory modelEvaluatorFactory = configuration.getModelEvaluatorFactory();

	ModelEvaluator<?> modelEvaluator = modelEvaluatorFactory.newModelEvaluator(getPMML(), model);

	Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();
	switch(multipleModelMethod){
		case SELECT_FIRST:
		case SELECT_ALL:
		case MODEL_CHAIN:
			{
				Set<ResultFeature> resultFeatures = getResultFeatures();

				if(!resultFeatures.isEmpty()){
					modelEvaluator.addResultFeatures(resultFeatures);
				}
			}
			// Falls through
		default:
			{
				Set<ResultFeature> segmentResultFeatures = getSegmentResultFeatures(segmentId);

				if(segmentResultFeatures != null && !segmentResultFeatures.isEmpty()){
					modelEvaluator.addResultFeatures(segmentResultFeatures);
				}
			}
			break;
	}

	MiningFunction segmentMiningFunction = model.getMiningFunction();

	if((MiningFunction.CLASSIFICATION).equals(miningFunction) && (MiningFunction.CLASSIFICATION).equals(segmentMiningFunction)){
		List<TargetField> targetFields = getTargetFields();
		List<TargetField> segmentTargetFields = modelEvaluator.getTargetFields();

		if(targetFields.size() == 1 && segmentTargetFields.size() == 1){
			TargetField targetField = targetFields.get(0);
			TargetField segmentTargetField = segmentTargetFields.get(0);

			if(segmentTargetField instanceof DefaultTargetField){
				DefaultTargetField defaultTargetField = (DefaultTargetField)segmentTargetField;

				modelEvaluator.setDefaultDataField(new DataField(Evaluator.DEFAULT_TARGET_NAME, OpType.CATEGORICAL, targetField.getDataType()));
			}
		}
	}

	modelEvaluator.configure(configuration);

	return modelEvaluator;
}
 
Example #29
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 #30
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected <V extends Number> Map<FieldName, ?> evaluateRegression(ValueFactory<V> valueFactory, EvaluationContext context){
	MiningModel miningModel = getModel();

	List<SegmentResult> segmentResults = evaluateSegmentation((MiningModelEvaluationContext)context);

	Map<FieldName, ?> predictions = getSegmentationResult(REGRESSION_METHODS, segmentResults);
	if(predictions != null){
		return predictions;
	}

	TargetField targetField = getTargetField();

	Segmentation segmentation = miningModel.getSegmentation();

	Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();
	Segmentation.MissingPredictionTreatment missingPredictionTreatment = segmentation.getMissingPredictionTreatment();
	Number missingThreshold = segmentation.getMissingThreshold();
	if(missingThreshold.doubleValue() < 0d || missingThreshold.doubleValue() > 1d){
		throw new InvalidAttributeException(segmentation, PMMLAttributes.SEGMENTATION_MISSINGTHRESHOLD, missingThreshold);
	}

	Value<V> value;

	switch(multipleModelMethod){
		case AVERAGE:
		case WEIGHTED_AVERAGE:
		case MEDIAN:
		case WEIGHTED_MEDIAN:
		case SUM:
		case WEIGHTED_SUM:
			value = MiningModelUtil.aggregateValues(valueFactory, multipleModelMethod, missingPredictionTreatment, missingThreshold, segmentResults);
			if(value == null){
				return TargetUtil.evaluateRegressionDefault(valueFactory, targetField);
			}
			break;
		case MAJORITY_VOTE:
		case WEIGHTED_MAJORITY_VOTE:
		case MAX:
		case SELECT_FIRST:
		case SELECT_ALL:
		case MODEL_CHAIN:
			throw new InvalidAttributeException(segmentation, multipleModelMethod);
		default:
			throw new UnsupportedAttributeException(segmentation, multipleModelMethod);
	}

	value = TargetUtil.evaluateRegressionInternal(targetField, value);

	Regression<V> result = new MiningScore<V>(value){

		@Override
		public Collection<? extends SegmentResult> getSegmentResults(){
			return segmentResults;
		}
	};

	return TargetUtil.evaluateRegression(targetField, result);
}