org.dmg.pmml.MapValues Java Examples

The following examples show how to use org.dmg.pmml.MapValues. 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: FormulaUtil.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private MapValues createMapValues(FieldName name, Map<String, String> mapping, List<String> categories){
	Set<String> inputs = new LinkedHashSet<>(mapping.keySet());
	Set<String> outputs = new LinkedHashSet<>(mapping.values());

	for(String category : categories){

		// Assume disjoint input and output value spaces
		if(outputs.contains(category)){
			continue;
		}

		mapping.put(category, category);
	}

	return PMMLUtil.createMapValues(name, mapping);
}
 
Example #2
Source File: ExpressionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public FieldValue evaluateMapValues(MapValues mapValues, EvaluationContext context){
	Map<String, FieldValue> values = new LinkedHashMap<>();

	List<FieldColumnPair> fieldColumnPairs = mapValues.getFieldColumnPairs();
	for(FieldColumnPair fieldColumnPair : fieldColumnPairs){
		FieldName name = fieldColumnPair.getField();
		if(name == null){
			throw new MissingAttributeException(fieldColumnPair, PMMLAttributes.FIELDCOLUMNPAIR_FIELD);
		}

		String column = fieldColumnPair.getColumn();
		if(column == null){
			throw new MissingAttributeException(fieldColumnPair, PMMLAttributes.FIELDCOLUMNPAIR_COLUMN);
		}

		FieldValue value = context.evaluate(name);
		if(FieldValueUtil.isMissing(value)){
			return FieldValueUtil.create(mapValues.getDataType(DataType.STRING), OpType.CATEGORICAL, mapValues.getMapMissingTo());
		}

		values.put(column, value);
	}

	return DiscretizationUtil.mapValue(mapValues, values);
}
 
Example #3
Source File: DiscretizationUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public FieldValue mapValue(MapValues mapValues, Map<String, FieldValue> values){
	String outputColumn = mapValues.getOutputColumn();
	if(outputColumn == null){
		throw new MissingAttributeException(mapValues, PMMLAttributes.MAPVALUES_OUTPUTCOLUMN);
	}

	DataType dataType = mapValues.getDataType(DataType.STRING);

	InlineTable inlineTable = InlineTableUtil.getInlineTable(mapValues);
	if(inlineTable != null){
		Map<String, Object> row = match(inlineTable, values);

		if(row != null){
			Object result = row.get(outputColumn);

			if(result == null){
				throw new InvalidElementException(inlineTable);
			}

			return FieldValueUtil.create(dataType, OpType.CATEGORICAL, result);
		}
	}

	return FieldValueUtil.create(dataType, OpType.CATEGORICAL, mapValues.getDefaultValue());
}
 
Example #4
Source File: ExpressionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void evaluateMapValues(){
	FieldName name = FieldName.create("x");

	List<List<String>> rows = Arrays.asList(
		Arrays.asList("0", "zero"),
		Arrays.asList("1", "one")
	);

	MapValues mapValues = new MapValues("data:output", null, createInlineTable(rows, Arrays.asList("data:input", "data:output")))
		.addFieldColumnPairs(new FieldColumnPair(name, "data:input"));

	assertEquals("zero", evaluate(mapValues, name, "0"));
	assertEquals("one", evaluate(mapValues, name, "1"));
	assertEquals(null, evaluate(mapValues, name, "3"));

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

	mapValues.setMapMissingTo("Missing");

	assertEquals("Missing", evaluate(mapValues, name, null));

	mapValues.setDefaultValue("Default");

	assertEquals("Default", evaluate(mapValues, name, "3"));
}
 
Example #5
Source File: EncoderUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public Feature encodeIndexFeature(Feature feature, List<?> categories, List<? extends Number> indexCategories, Number mapMissingTo, Number defaultValue, DataType dataType, SkLearnEncoder encoder){
	ClassDictUtil.checkSize(categories, indexCategories);

	encoder.toCategorical(feature.getName(), categories);

	Supplier<MapValues> mapValuesSupplier = () -> {
		MapValues mapValues = PMMLUtil.createMapValues(feature.getName(), categories, indexCategories)
			.setMapMissingTo(mapMissingTo)
			.setDefaultValue(defaultValue);

		return mapValues;
	};

	DerivedField derivedField = encoder.ensureDerivedField(FeatureUtil.createName("encoder", feature), OpType.CATEGORICAL, dataType, mapValuesSupplier);

	Feature encodedFeature = new IndexFeature(encoder, derivedField, indexCategories);

	Feature result = new CategoricalFeature(encoder, feature, categories){

		@Override
		public ContinuousFeature toContinuousFeature(){
			return encodedFeature.toContinuousFeature();
		}
	};

	return result;
}
 
Example #6
Source File: VectorIndexerModelConverter.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){
	VectorIndexerModel transformer = getTransformer();

	int numFeatures = transformer.numFeatures();

	List<Feature> features = encoder.getFeatures(transformer.getInputCol());

	SchemaUtil.checkSize(numFeatures, features);

	Map<Integer, Map<Double, Integer>> categoryMaps = transformer.javaCategoryMaps();

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

	for(int i = 0, length = numFeatures; i < length; i++){
		Feature feature = features.get(i);

		Map<Double, Integer> categoryMap = categoryMaps.get(i);
		if(categoryMap != null){
			List<Double> categories = new ArrayList<>();
			List<Integer> values = new ArrayList<>();

			List<Map.Entry<Double, Integer>> entries = new ArrayList<>(categoryMap.entrySet());
			Collections.sort(entries, VectorIndexerModelConverter.COMPARATOR);

			for(Map.Entry<Double, Integer> entry : entries){
				Double category = entry.getKey();
				Integer value = entry.getValue();

				categories.add(category);
				values.add(value);
			}

			encoder.toCategorical(feature.getName(), categories);

			MapValues mapValues = PMMLUtil.createMapValues(feature.getName(), categories, values)
				.setDataType(DataType.INTEGER);

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

			result.add(new CategoricalFeature(encoder, derivedField, values));
		} else

		{
			result.add((ContinuousFeature)feature);
		}
	}

	return result;
}
 
Example #7
Source File: RegressionTableUtil.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private MapValues createMapValues(FieldName name, Object identifier, List<Feature> features, List<Double> coefficients){
	ListIterator<Feature> featureIt = features.listIterator();
	ListIterator<Double> coefficientIt = coefficients.listIterator();

	PMMLEncoder encoder = null;

	List<Object> inputValues = new ArrayList<>();
	List<Double> outputValues = new ArrayList<>();

	while(featureIt.hasNext()){
		Feature feature = featureIt.next();
		Double coefficient = coefficientIt.next();

		if(!(feature instanceof BinaryFeature)){
			continue;
		}

		BinaryFeature binaryFeature = (BinaryFeature)feature;
		if(!(name).equals(binaryFeature.getName())){
			continue;
		}

		featureIt.remove();
		coefficientIt.remove();

		if(encoder == null){
			encoder = binaryFeature.getEncoder();
		}

		inputValues.add(binaryFeature.getValue());
		outputValues.add(coefficient);
	}

	MapValues mapValues = PMMLUtil.createMapValues(name, inputValues, outputValues)
		.setDefaultValue(0d)
		.setDataType(DataType.DOUBLE);

	DerivedField derivedField = encoder.createDerivedField(FieldName.create("lookup(" + name.getValue() + (identifier != null ? (", " + identifier) : "") + ")"), OpType.CONTINUOUS, DataType.DOUBLE, mapValues);

	featureIt.add(new ContinuousFeature(encoder, derivedField));
	coefficientIt.add(1d);

	return mapValues;
}
 
Example #8
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(MapValues mapValues){
	parseExpressionValues(mapValues);

	return super.visit(mapValues);
}
 
Example #9
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 #10
Source File: ClassificationModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 2 votes vote down vote up
@Override
public List<OutputField> registerOutputFields(Label label, Model pmmlModel, SparkMLEncoder encoder){
	T model = getTransformer();

	CategoricalLabel categoricalLabel = (CategoricalLabel)label;

	List<Integer> categories = LabelUtil.createTargetCategories(categoricalLabel.size());

	String predictionCol = model.getPredictionCol();

	Boolean keepPredictionCol = (Boolean)getOption(HasPredictionModelOptions.OPTION_KEEP_PREDICTIONCOL, Boolean.TRUE);

	OutputField pmmlPredictedOutputField = ModelUtil.createPredictedField(FieldName.create("pmml(" + predictionCol + ")"), OpType.CATEGORICAL, categoricalLabel.getDataType())
		.setFinalResult(false);

	DerivedOutputField pmmlPredictedField = encoder.createDerivedField(pmmlModel, pmmlPredictedOutputField, keepPredictionCol);

	MapValues mapValues = PMMLUtil.createMapValues(pmmlPredictedField.getName(), categoricalLabel.getValues(), categories)
		.setDataType(DataType.DOUBLE);

	OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CONTINUOUS, DataType.DOUBLE)
		.setResultFeature(ResultFeature.TRANSFORMED_VALUE)
		.setExpression(mapValues);

	DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, keepPredictionCol);

	encoder.putOnlyFeature(predictionCol, new IndexFeature(encoder, predictedField, categories));

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

	if(model instanceof HasProbabilityCol){
		HasProbabilityCol hasProbabilityCol = (HasProbabilityCol)model;

		String probabilityCol = hasProbabilityCol.getProbabilityCol();

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

		for(int i = 0; i < categoricalLabel.size(); i++){
			Object value = categoricalLabel.getValue(i);

			OutputField probabilityField = ModelUtil.createProbabilityField(FieldName.create(probabilityCol + "(" + value + ")"), DataType.DOUBLE, value);

			result.add(probabilityField);

			features.add(new ContinuousFeature(encoder, probabilityField));
		}

		// XXX
		encoder.putFeatures(probabilityCol, features);
	}

	return result;
}