Java Code Examples for org.jpmml.converter.CategoricalFeature#getValues()

The following examples show how to use org.jpmml.converter.CategoricalFeature#getValues() . 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: OneHotEncoderModelConverter.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){
	OneHotEncoderModel transformer = getTransformer();

	boolean dropLast = transformer.getDropLast();

	InOutMode inputMode = getInputMode();

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

	String[] inputCols = inputMode.getInputCols(transformer);
	for(String inputCol : inputCols){
		CategoricalFeature categoricalFeature = (CategoricalFeature)encoder.getOnlyFeature(inputCol);

		List<?> values = categoricalFeature.getValues();

		List<BinaryFeature> binaryFeatures = OneHotEncoderModelConverter.encodeFeature(encoder, categoricalFeature, values, dropLast);

		result.add(new BinarizedCategoricalFeature(encoder, categoricalFeature.getName(), categoricalFeature.getDataType(), binaryFeatures));
	}

	return result;
}
 
Example 2
Source File: MultiOneHotEncoder.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	List<List<?>> categories = getCategories();

	ClassDictUtil.checkSize(categories, features);

	Object drop = getDrop();
	List<Integer> dropIdx = (drop != null ? getDropIdx() : null);

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

	for(int i = 0; i < features.size(); i++){
		Feature feature = features.get(i);
		List<?> featureCategories = categories.get(i);

		if(feature instanceof CategoricalFeature){
			CategoricalFeature categoricalFeature = (CategoricalFeature)feature;

			ClassDictUtil.checkSize(featureCategories, categoricalFeature.getValues());

			featureCategories = categoricalFeature.getValues();
		} else

		if(feature instanceof ObjectFeature){
			ObjectFeature objectFeature = (ObjectFeature)feature;
		} else

		if(feature instanceof WildcardFeature){
			WildcardFeature wildcardFeature = (WildcardFeature)feature;

			feature = wildcardFeature.toCategoricalFeature(featureCategories);
		} else

		{
			throw new IllegalArgumentException();
		} // End if

		if(dropIdx != null){
			// Unbox to primitive value in order to ensure correct List#remove(int) vs. List#remove(Object) method resolution
			int index = dropIdx.get(i);

			featureCategories = new ArrayList<>(featureCategories);
			featureCategories.remove(index);
		}

		for(int j = 0; j < featureCategories.size(); j++){
			Object featureCategory = featureCategories.get(j);

			result.add(new BinaryFeature(encoder, feature, featureCategory));
		}
	}

	return result;
}
 
Example 3
Source File: RPartConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
private List<Predicate> encodePredicates(Feature feature, int splitOffset, RNumberVector<?> splits, RIntegerVector csplit){
	Predicate leftPredicate;
	Predicate rightPredicate;

	RIntegerVector splitsDim = splits.dim();

	int splitRows = splitsDim.getValue(0);
	int splitColumns = splitsDim.getValue(1);

	List<? extends Number> ncat = FortranMatrixUtil.getColumn(splits.getValues(), splitRows, splitColumns, 1);
	List<? extends Number> index = FortranMatrixUtil.getColumn(splits.getValues(), splitRows, splitColumns, 3);

	int splitType = ValueUtil.asInt(ncat.get(splitOffset));

	Number splitValue = index.get(splitOffset);

	if(Math.abs(splitType) == 1){
		SimplePredicate.Operator leftOperator;
		SimplePredicate.Operator rightOperator;

		if(splitType == -1){
			leftOperator = SimplePredicate.Operator.LESS_THAN;
			rightOperator = SimplePredicate.Operator.GREATER_OR_EQUAL;
		} else

		{
			leftOperator = SimplePredicate.Operator.GREATER_OR_EQUAL;
			rightOperator = SimplePredicate.Operator.LESS_THAN;
		}

		leftPredicate = createSimplePredicate(feature, leftOperator, splitValue);
		rightPredicate = createSimplePredicate(feature, rightOperator, splitValue);
	} else

	{
		CategoricalFeature categoricalFeature = (CategoricalFeature)feature;

		RIntegerVector csplitDim = csplit.dim();

		int csplitRows = csplitDim.getValue(0);
		int csplitColumns = csplitDim.getValue(1);

		List<Integer> csplitRow = FortranMatrixUtil.getRow(csplit.getValues(), csplitRows, csplitColumns, ValueUtil.asInt(splitValue) - 1);

		List<?> values = categoricalFeature.getValues();

		leftPredicate = createSimpleSetPredicate(categoricalFeature, selectValues(values, csplitRow, 1));
		rightPredicate = createSimpleSetPredicate(categoricalFeature, selectValues(values, csplitRow, 3));
	}

	return Arrays.asList(leftPredicate, rightPredicate);
}