org.dmg.pmml.DiscretizeBin Java Examples

The following examples show how to use org.dmg.pmml.DiscretizeBin. 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: 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;
}