Java Code Examples for org.dmg.pmml.mining.MiningModel#getSegmentation()

The following examples show how to use org.dmg.pmml.mining.MiningModel#getSegmentation() . 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 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 2
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 3
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 4
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 5
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public MiningModelEvaluator(PMML pmml, MiningModel miningModel){
	super(pmml, miningModel);

	if(miningModel.hasEmbeddedModels()){
		List<EmbeddedModel> embeddedModels = miningModel.getEmbeddedModels();

		EmbeddedModel embeddedModel = Iterables.getFirst(embeddedModels, null);

		throw new UnsupportedElementException(embeddedModel);
	}

	Segmentation segmentation = miningModel.getSegmentation();
	if(segmentation == null){
		throw new MissingElementException(miningModel, PMMLElements.MININGMODEL_SEGMENTATION);
	}

	Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();
	if(multipleModelMethod == null){
		throw new MissingAttributeException(segmentation, PMMLAttributes.SEGMENTATION_MULTIPLEMODELMETHOD);
	} // End if

	if(!segmentation.hasSegments()){
		throw new MissingElementException(segmentation, PMMLElements.SEGMENTATION_SEGMENTS);
	}

	List<Segment> segments = segmentation.getSegments();
	for(Segment segment : segments){
		VariableWeight variableWeight = segment.getVariableWeight();

		if(variableWeight != null){
			throw new UnsupportedElementException(variableWeight);
		}
	}

	LocalTransformations localTransformations = segmentation.getLocalTransformations();
	if(localTransformations != null){
		throw new UnsupportedElementException(localTransformations);
	}

	Output output = miningModel.getOutput();
	if(output != null && output.hasOutputFields()){
		this.segmentResultFeatures = CacheUtil.getValue(output, MiningModelEvaluator.segmentResultFeaturesCache);
	}
}
 
Example 6
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);
}
 
Example 7
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, ?> evaluateClustering(ValueFactory<V> valueFactory, EvaluationContext context){
	MiningModel miningModel = getModel();

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

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

	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);
	}

	MiningVoteDistribution<V> result;

	switch(multipleModelMethod){
		case MAJORITY_VOTE:
		case WEIGHTED_MAJORITY_VOTE:
			{
				ValueMap<Object, V> values = MiningModelUtil.aggregateVotes(valueFactory, multipleModelMethod, missingPredictionTreatment, missingThreshold, segmentResults);
				if(values == null){
					return Collections.singletonMap(getTargetName(), null);
				}

				result = new MiningVoteDistribution<V>(values){

					@Override
					public Collection<? extends SegmentResult> getSegmentResults(){
						return segmentResults;
					}
				};
			}
			break;
		case AVERAGE:
		case WEIGHTED_AVERAGE:
		case MEDIAN:
		case WEIGHTED_MEDIAN:
		case MAX:
		case SUM:
		case WEIGHTED_SUM:
		case SELECT_FIRST:
		case SELECT_ALL:
		case MODEL_CHAIN:
			throw new InvalidAttributeException(segmentation, multipleModelMethod);
		default:
			throw new UnsupportedAttributeException(segmentation, multipleModelMethod);
	}

	result.computeResult(DataType.STRING);

	return Collections.singletonMap(getTargetName(), result);
}
 
Example 8
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 9
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public BiMap<String, Segment> load(MiningModel miningModel){
	Segmentation segmentation = miningModel.getSegmentation();

	return EntityUtil.buildBiMap(segmentation.getSegments());
}
 
Example 10
Source File: MissingPredictionTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluate() throws Exception {
	MiningModelEvaluator evaluator = (MiningModelEvaluator)createModelEvaluator();

	MiningModel miningModel = evaluator.getModel();

	Segmentation segmentation = miningModel.getSegmentation();

	assertEquals(null, evaluate(evaluator, null));

	segmentation.setMissingThreshold(0.25);

	assertEquals(null, evaluate(evaluator, 0d));
	assertEquals(null, evaluate(evaluator, 1d));
	assertEquals(null, evaluate(evaluator, 2d));
	assertEquals((Integer)1, evaluate(evaluator, 3d));
	assertEquals((Integer)1, evaluate(evaluator, 4d));

	segmentation.setMissingThreshold(0.5d);

	assertEquals(null, evaluate(evaluator, 1d));
	assertEquals((Integer)1, evaluate(evaluator, 2d));
	assertEquals((Integer)1, evaluate(evaluator, 3d));

	segmentation.setMissingThreshold(0.75d);

	// Two votes for the missing pseudo-category vs. one vote for the "1" category
	assertEquals(null, evaluate(evaluator, 1d));

	assertEquals((Integer)1, evaluate(evaluator, 2d));
	assertEquals((Integer)1, evaluate(evaluator, 3d));

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

		treeModel.setNoTrueChildStrategy(TreeModel.NoTrueChildStrategy.RETURN_LAST_PREDICTION);
	}

	assertEquals(null, evaluate(evaluator, null));

	assertEquals((Integer)0, evaluate(evaluator, 1d));
	assertEquals((Integer)1, evaluate(evaluator, 2d));
	assertEquals((Integer)1, evaluate(evaluator, 3d));
}