org.dmg.pmml.Discretize Java Examples

The following examples show how to use org.dmg.pmml.Discretize. 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: ValueParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public VisitorAction visit(DiscretizeBin discretizeBin){
	Discretize discretize = (Discretize)getParent();

	DataType dataType = discretize.getDataType();
	if(dataType != null){
		Object binValue = discretizeBin.getBinValue();
		if(binValue != null){
			binValue = parseOrCast(dataType, binValue);

			discretizeBin.setBinValue(binValue);
		}
	}

	return super.visit(discretizeBin);
}
 
Example #2
Source File: DiscretizationUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private RangeMap<Double, Object> parseDiscretize(Discretize discretize){
	RangeMap<Double, Object> result = TreeRangeMap.create();

	List<DiscretizeBin> discretizeBins = discretize.getDiscretizeBins();
	for(DiscretizeBin discretizeBin : discretizeBins){
		Interval interval = discretizeBin.getInterval();
		if(interval == null){
			throw new MissingElementException(discretizeBin, PMMLElements.DISCRETIZEBIN_INTERVAL);
		}

		Range<Double> range = toRange(interval);

		Object binValue = discretizeBin.getBinValue();
		if(binValue == null){
			throw new MissingAttributeException(discretizeBin, PMMLAttributes.DISCRETIZEBIN_BINVALUE);
		}

		result.put(range, binValue);
	}

	return result;
}
 
Example #3
Source File: FormulaUtil.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Discretize createDiscretize(FieldName name, List<String> categories){
	Discretize discretize = new Discretize(name);

	for(String category : categories){
		Interval interval = ExpressionTranslator.translateInterval(category);

		DiscretizeBin discretizeBin = new DiscretizeBin(category, interval);

		discretize.addDiscretizeBins(discretizeBin);
	}

	return discretize;
}
 
Example #4
Source File: ExpressionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public FieldValue evaluateDiscretize(Discretize discretize, EvaluationContext context){
	FieldValue value = context.evaluate(ensureField(discretize));

	if(FieldValueUtil.isMissing(value)){
		return FieldValueUtil.create(discretize.getDataType(DataType.STRING), OpType.CATEGORICAL, discretize.getMapMissingTo());
	}

	return DiscretizationUtil.discretize(discretize, value);
}
 
Example #5
Source File: BucketizerConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder){
	Bucketizer transformer = getTransformer();

	InOutMode inputMode = getInputMode();

	String[] inputCols;
	double[][] splitsArray;

	if((InOutMode.SINGLE).equals(inputMode)){
		inputCols = inputMode.getInputCols(transformer);
		splitsArray = new double[][]{transformer.getSplits()};
	} else

	if((InOutMode.MULTIPLE).equals(inputMode)){
		inputCols = inputMode.getInputCols(transformer);
		splitsArray = transformer.getSplitsArray();
	} else

	{
		throw new IllegalArgumentException();
	}

	List<Feature> result = new ArrayList<>();

	for(int i = 0; i < inputCols.length; i++){
		String inputCol = inputCols[i];
		double[] splits = splitsArray[i];

		Feature feature = encoder.getOnlyFeature(inputCol);

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		Discretize discretize = new Discretize(continuousFeature.getName())
			.setDataType(DataType.INTEGER);

		List<Integer> categories = new ArrayList<>();

		for(int j = 0; j < (splits.length - 1); j++){
			Integer category = j;

			categories.add(category);

			Interval interval = new Interval((j < (splits.length - 2)) ? Interval.Closure.CLOSED_OPEN : Interval.Closure.CLOSED_CLOSED)
				.setLeftMargin(formatMargin(splits[j]))
				.setRightMargin(formatMargin(splits[j + 1]));

			DiscretizeBin discretizeBin = new DiscretizeBin(category, interval);

			discretize.addDiscretizeBins(discretizeBin);
		}

		DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i), OpType.CATEGORICAL, DataType.INTEGER, discretize);

		result.add(new IndexFeature(encoder, derivedField, categories));
	}

	return result;
}
 
Example #6
Source File: FieldReferenceFinder.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public VisitorAction visit(Discretize discretize){
	process(discretize.getField());

	return super.visit(discretize);
}
 
Example #7
Source File: ValueParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public VisitorAction visit(Discretize discretize){
	parseExpressionValues(discretize);

	return super.visit(discretize);
}
 
Example #8
Source File: ExpressionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
FieldValue evaluateExpression(Expression expression, EvaluationContext context){

	if(expression instanceof Constant){
		return evaluateConstant((Constant)expression);
	} else

	if(expression instanceof FieldRef){
		return evaluateFieldRef((FieldRef)expression, context);
	} else

	if(expression instanceof NormContinuous){
		return evaluateNormContinuous((NormContinuous)expression, context);
	} else

	if(expression instanceof NormDiscrete){
		return evaluateNormDiscrete((NormDiscrete)expression, context);
	} else

	if(expression instanceof Discretize){
		return evaluateDiscretize((Discretize)expression, context);
	} else

	if(expression instanceof MapValues){
		return evaluateMapValues((MapValues)expression, context);
	} else

	if(expression instanceof TextIndex){
		return evaluateTextIndex((TextIndex)expression, context);
	} else

	if(expression instanceof Apply){
		return evaluateApply((Apply)expression, context);
	} else

	if(expression instanceof Aggregate){
		return evaluateAggregate((Aggregate)expression, context);
	} // End if

	if(expression instanceof JavaExpression){
		return evaluateJavaExpression((JavaExpression)expression, context);
	}

	throw new UnsupportedElementException(expression);
}
 
Example #9
Source File: DiscretizationUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public RangeMap<Double, Object> load(Discretize discretize){
	return ImmutableRangeMap.copyOf(parseDiscretize(discretize));
}
 
Example #10
Source File: ExpressionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluateDiscretize(){
	FieldName name = FieldName.create("x");

	Discretize discretize = new Discretize(name);

	assertEquals(null, evaluate(discretize, name, null));

	discretize.setMapMissingTo("Missing");

	assertEquals("Missing", evaluate(discretize, name, null));
	assertEquals(null, evaluate(discretize, name, 3));

	discretize.setDefaultValue("Default");

	assertEquals("Default", evaluate(discretize, name, 3));
}