org.dmg.pmml.MathContext Java Examples

The following examples show how to use org.dmg.pmml.MathContext. 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: TargetUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private TreeModelEvaluator createTreeModelEvaluator(MiningFunction miningFunction, MathContext mathContext, Target target){
	Node root = new LeafNode(null, False.INSTANCE);

	Targets targets = new Targets()
		.addTargets(target);

	TreeModel treeModel = new TreeModel(miningFunction, new MiningSchema(), root)
		.setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT)
		.setMathContext(mathContext)
		.setTargets(targets);

	PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), new DataDictionary())
		.addModels(treeModel);

	ModelEvaluatorBuilder modelEvaluatorBuilder = new ModelEvaluatorBuilder(pmml);

	return (TreeModelEvaluator)modelEvaluatorBuilder.build();
}
 
Example #2
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * @return A synthetic {@link DataField} element describing the default target field.
 */
public DataField getDefaultDataField(){

	if(this.defaultDataField != null){
		return this.defaultDataField;
	}

	MiningFunction miningFunction = getMiningFunction();
	switch(miningFunction){
		case REGRESSION:
			MathContext mathContext = getMathContext();

			switch(mathContext){
				case FLOAT:
					return ModelManager.DEFAULT_TARGET_CONTINUOUS_FLOAT;
				default:
					return ModelManager.DEFAULT_TARGET_CONTINUOUS_DOUBLE;
			}
		case CLASSIFICATION:
		case CLUSTERING:
			return ModelManager.DEFAULT_TARGET_CATEGORICAL_STRING;
		default:
			return null;
	}
}
 
Example #3
Source File: RegTree.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
public TreeModel encodeTreeModel(PredicateManager predicateManager, Schema schema){
	org.dmg.pmml.tree.Node root = encodeNode(True.INSTANCE, predicateManager, 0, schema);

	TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), root)
		.setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT)
		.setMissingValueStrategy(TreeModel.MissingValueStrategy.DEFAULT_CHILD)
		.setMathContext(MathContext.FLOAT);

	return treeModel;
}
 
Example #4
Source File: ReportingValueFactoryFactory.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public ReportingValueFactory<?> newValueFactory(MathContext mathContext){
	ReportFactory reportFactory = getReportFactory();

	switch(mathContext){
		case FLOAT:
			return new ReportingFloatValueFactory(reportFactory);
		case DOUBLE:
			return new ReportingDoubleValueFactory(reportFactory);
		default:
			throw new IllegalArgumentException();
	}
}
 
Example #5
Source File: RegressionTree.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public TreeModel encodeTreeModel(Schema schema){
    org.dmg.pmml.tree.Node root = new org.dmg.pmml.tree.Node()
            .setPredicate(new True());

    encodeNode(root, 0, schema);

    TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), root)
            .setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT)
            .setMissingValueStrategy(TreeModel.MissingValueStrategy.NONE)
            .setMathContext(MathContext.FLOAT);

    return treeModel;
}
 
Example #6
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected ModelEvaluator(PMML pmml, M model){
	super(pmml, model);

	MathContext mathContext = model.getMathContext();
	switch(mathContext){
		case FLOAT:
		case DOUBLE:
			break;
		default:
			throw new UnsupportedAttributeException(model, mathContext);
	}
}
 
Example #7
Source File: JavaModel.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public MathContext getMathContext(){

	if(this.mathContext == null){
		return MathContext.DOUBLE;
	}

	return this.mathContext;
}
 
Example #8
Source File: SupportVectorMachineModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Object toArray(SupportVectorMachineModel supportVectorMachineModel, List<? extends Number> values){
	MathContext mathContext = supportVectorMachineModel.getMathContext();

	switch(mathContext){
		case FLOAT:
			return Floats.toArray(values);
		case DOUBLE:
			return Doubles.toArray(values);
		default:
			throw new UnsupportedAttributeException(supportVectorMachineModel, mathContext);
	}
}
 
Example #9
Source File: ValueFactoryFactory.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public ValueFactory<?> newValueFactory(MathContext mathContext){

		switch(mathContext){
			case FLOAT:
				return FloatValueFactory.INSTANCE;
			case DOUBLE:
				return DoubleValueFactory.INSTANCE;
			default:
				throw new IllegalArgumentException();
		}
	}
 
Example #10
Source File: ReportingValueFactoryTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void newVocalZero(){
	ValueFactory<Float> valueFactory = (ValueFactory)this.valueFactoryFactory.newValueFactory(MathContext.FLOAT);

	Value<Float> value = valueFactory.newValue(0d);

	assertEquals((Float)0f, value.getValue());

	HasReport hasReport = (HasReport)value;

	Report report = hasReport.getReport();

	assertTrue(report.hasEntries());

	Report.Entry entry = report.tailEntry();

	assertEquals("<cn>" + 0f + "</cn>", entry.getExpression());
	assertEquals((Float)0f, entry.getValue());
}
 
Example #11
Source File: MeasureUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluateSimilarity(){
	BitSet flags = createFlags(Arrays.asList(0, 0, 1, 1));
	BitSet referenceFlags = createFlags(Arrays.asList(0, 1, 0, 1));

	ValueFactory<?> valueFactory = MeasureUtilTest.valueFactoryFactory.newValueFactory(MathContext.DOUBLE);

	ComparisonMeasure comparisonMeasure = new ComparisonMeasure(ComparisonMeasure.Kind.SIMILARITY, new SimpleMatching());

	List<ClusteringField> clusteringFields = createClusteringFields("one", "two", "three", "four");

	assertEquals(valueFactory.newValue(2d / 4d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags));

	comparisonMeasure.setMeasure(new Jaccard());

	assertEquals(valueFactory.newValue(1d / 3d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags));

	comparisonMeasure.setMeasure(new Tanimoto());

	assertEquals(valueFactory.newValue(2d / (1d + 2 * 2d + 1d)), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags));

	comparisonMeasure.setMeasure(new BinarySimilarity(0.5d, 0.5d, 0.5d, 0.5d, 1d, 1d, 1d, 1d));

	assertEquals(valueFactory.newValue(2d / 4d), MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, referenceFlags));
}
 
Example #12
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public MathContext getMathContext(){
	M model = getModel();

	return model.getMathContext();
}
 
Example #13
Source File: JavaModel.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public JavaModel setMathContext(MathContext mathContext){
	this.mathContext = mathContext;

	return this;
}
 
Example #14
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public Map<FieldName, ?> evaluateInternal(ModelEvaluationContext context){
	M model = getModel();

	if(!model.isScorable()){
		throw new EvaluationException("Model is not scorable", model);
	}

	ValueFactory<?> valueFactory;

	MathContext mathContext = model.getMathContext();
	switch(mathContext){
		case FLOAT:
		case DOUBLE:
			valueFactory = ensureValueFactory();
			break;
		default:
			throw new UnsupportedAttributeException(model, mathContext);
	}

	Map<FieldName, ?> predictions;

	MiningFunction miningFunction = model.getMiningFunction();
	switch(miningFunction){
		case REGRESSION:
			predictions = evaluateRegression(valueFactory, context);
			break;
		case CLASSIFICATION:
			predictions = evaluateClassification(valueFactory, context);
			break;
		case CLUSTERING:
			predictions = evaluateClustering(valueFactory, context);
			break;
		case ASSOCIATION_RULES:
			predictions = evaluateAssociationRules(valueFactory, context);
			break;
		case SEQUENCES:
			predictions = evaluateSequences(valueFactory, context);
			break;
		case TIME_SERIES:
			predictions = evaluateTimeSeries(valueFactory, context);
			break;
		case MIXED:
			predictions = evaluateMixed(valueFactory, context);
			break;
		default:
			throw new UnsupportedAttributeException(model, miningFunction);
	}

	return OutputUtil.evaluate(predictions, context);
}
 
Example #15
Source File: MiningModelUtil.java    From pyramid with Apache License 2.0 4 votes vote down vote up
static
public MiningModel createClassification(List<? extends Model> models, RegressionModel.NormalizationMethod normalizationMethod, boolean hasProbabilityDistribution, Schema schema){
    CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel();

    // modified here
    if(categoricalLabel.size() != models.size()){
        throw new IllegalArgumentException();
    } // End if

    if(normalizationMethod != null){

        switch(normalizationMethod){
            case NONE:
            case SIMPLEMAX:
            case SOFTMAX:
                break;
            default:
                throw new IllegalArgumentException();
        }
    }

    MathContext mathContext = null;

    List<RegressionTable> regressionTables = new ArrayList<>();

    for(int i = 0; i < categoricalLabel.size(); i++){
        Model model = models.get(i);

        MathContext modelMathContext = model.getMathContext();
        if(modelMathContext == null){
            modelMathContext = MathContext.DOUBLE;
        } // End if

        if(mathContext == null){
            mathContext = modelMathContext;
        } else

        {
            if(!Objects.equals(mathContext, modelMathContext)){
                throw new IllegalArgumentException();
            }
        }

        Feature feature = MODEL_PREDICTION.apply(model);

        RegressionTable regressionTable = RegressionModelUtil.createRegressionTable(Collections.singletonList(feature), Collections.singletonList(1d), null)
                .setTargetCategory(categoricalLabel.getValue(i));

        regressionTables.add(regressionTable);
    }

    RegressionModel regressionModel = new RegressionModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), regressionTables)
            .setNormalizationMethod(normalizationMethod)
            .setMathContext(ModelUtil.simplifyMathContext(mathContext))
            .setOutput(hasProbabilityDistribution ? ModelUtil.createProbabilityOutput(mathContext, categoricalLabel) : null);

    List<Model> segmentationModels = new ArrayList<>(models);
    segmentationModels.add(regressionModel);

    return createModelChain(segmentationModels, schema);
}
 
Example #16
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 #17
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
protected ValueFactory<?> ensureValueFactory(){
	ValueFactory<?> valueFactory = getValueFactory();

	if(valueFactory == null){
		ValueFactoryFactory valueFactoryFactory = ensureValueFactoryFactory();

		MathContext mathContext = getMathContext();

		valueFactory = valueFactoryFactory.newValueFactory(mathContext);

		setValueFactory(valueFactory);
	}

	return valueFactory;
}
 
Example #18
Source File: TargetUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void evaluateRegressionDefault(){
	TargetValue targetValue = new TargetValue()
		.setDefaultValue(Math.PI);

	Target target = new Target()
		.setField(null)
		.addTargetValues(targetValue);

	ModelEvaluator<?> evaluator = createTreeModelEvaluator(MiningFunction.REGRESSION, MathContext.FLOAT, target);

	DataField dataField = evaluator.getDataField(target.getField());

	assertEquals(OpType.CONTINUOUS, dataField.getOpType());
	assertEquals(DataType.FLOAT, dataField.getDataType());

	Map<FieldName, ?> results = evaluator.evaluate(Collections.emptyMap());

	assertEquals((float)Math.PI, results.get(dataField.getName()));

	evaluator = createTreeModelEvaluator(MiningFunction.REGRESSION, MathContext.DOUBLE, target);

	dataField = evaluator.getDefaultDataField();

	assertEquals(OpType.CONTINUOUS, dataField.getOpType());
	assertEquals(DataType.DOUBLE, dataField.getDataType());

	results = evaluator.evaluate(Collections.emptyMap());

	assertEquals(Math.PI, results.get(dataField.getName()));
}
 
Example #19
Source File: ReportingValueFactoryTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void newSilentZero(){
	ValueFactory<Float> valueFactory = (ValueFactory)this.valueFactoryFactory.newValueFactory(MathContext.FLOAT);

	Value<Float> value = valueFactory.newValue();

	assertEquals((Float)0f, value.getValue());

	HasReport hasReport = (HasReport)value;

	Report report = hasReport.getReport();

	assertFalse(report.hasEntries());

	try {
		report.tailEntry();

		fail();
	} catch(IllegalStateException ise){
		// Ignored
	}

	value.add(Numbers.FLOAT_ZERO);

	assertEquals((Float)0f, value.getValue());

	assertFalse(report.hasEntries());

	value.add(Numbers.FLOAT_ONE);

	assertEquals((Float)1f, value.getValue());

	assertTrue(report.hasEntries());

	Report.Entry entry = report.tailEntry();

	assertEquals("<cn>" + 1f + "</cn>", entry.getExpression());
	assertEquals((Float)1f, entry.getValue());
}
 
Example #20
Source File: NodeScoreParserTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 2 votes vote down vote up
@Test
public void parseAndIntern(){
	Node node1a = new BranchNode("1", True.INSTANCE);

	Node node2a = new LeafNode("2", False.INSTANCE);
	Node node2b = new BranchNode("2.0", False.INSTANCE);
	Node node2c = new LeafNode(2.0f, True.INSTANCE);

	node1a.addNodes(node2a, node2b, node2c);

	Node node3a = new LeafNode("error", False.INSTANCE);

	node2b.addNodes(node3a);

	TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, new MiningSchema(), node1a)
		.setMathContext(MathContext.FLOAT);

	VisitorBattery visitorBattery = new VisitorBattery();
	visitorBattery.add(NodeScoreParser.class);
	visitorBattery.add(FloatInterner.class);

	visitorBattery.applyTo(treeModel);

	assertEquals("1", node1a.getScore());

	assertEquals("2", node2a.getScore());
	assertEquals("2.0", node2b.getScore());
	assertEquals(2.0f, node2c.getScore());

	assertEquals("error", node3a.getScore());

	treeModel.setMiningFunction(MiningFunction.REGRESSION);

	visitorBattery.applyTo(treeModel);

	assertEquals(1.0f, node1a.getScore());

	assertEquals(2.0f, node2a.getScore());
	assertEquals(2.0f, node2b.getScore());
	assertEquals(2.0f, node2c.getScore());

	assertSame(node2a.getScore(), node2b.getScore());
	assertSame(node2a.getScore(), node2c.getScore());

	assertEquals("error", node3a.getScore());
}
 
Example #21
Source File: TargetUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 2 votes vote down vote up
@Test
public void evaluateClassificationDefault(){
	TargetValue yesValue = new TargetValue()
		.setValue("yes")
		.setPriorProbability(1d / 7d);

	TargetValue noValue = new TargetValue()
		.setValue("no")
		.setPriorProbability(1d - NumberUtil.asDouble(yesValue.getPriorProbability()));

	Target target = new Target()
		.setField(null)
		.addTargetValues(yesValue, noValue);

	ModelEvaluator<?> evaluator = createTreeModelEvaluator(MiningFunction.CLASSIFICATION, MathContext.FLOAT, target);

	DataField dataField = evaluator.getDataField(target.getField());

	assertEquals(OpType.CATEGORICAL, dataField.getOpType());
	assertEquals(DataType.STRING, dataField.getDataType());

	Map<FieldName, ?> results = evaluator.evaluate(Collections.emptyMap());

	HasProbability hasProbability = (HasProbability)results.get(dataField.getName());

	assertEquals((Double)((double)(float)(1d / 7d)), hasProbability.getProbability("yes"));
	assertEquals((Double)((double)(float)(1d - (1d / 7d))), hasProbability.getProbability("no"));

	evaluator = createTreeModelEvaluator(MiningFunction.CLASSIFICATION, MathContext.DOUBLE, target);

	dataField = evaluator.getDataField(target.getField());

	assertEquals(OpType.CATEGORICAL, dataField.getOpType());
	assertEquals(DataType.STRING, dataField.getDataType());

	results = evaluator.evaluate(Collections.emptyMap());

	hasProbability = (HasProbability)results.get(dataField.getName());

	assertEquals(yesValue.getPriorProbability(), hasProbability.getProbability("yes"));
	assertEquals(noValue.getPriorProbability(), hasProbability.getProbability("no"));
}