Java Code Examples for org.dmg.pmml.DerivedField

The following examples show how to use org.dmg.pmml.DerivedField. These examples are extracted from open source projects. 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
static
public Feature encodeIndicatorFeature(Feature feature, Object missingValue, SkLearnEncoder encoder){
	Expression expression = feature.ref();

	if(missingValue != null){
		expression = PMMLUtil.createApply(PMMLFunctions.EQUAL, expression, PMMLUtil.createConstant(missingValue, feature.getDataType()));
	} else

	{
		expression = PMMLUtil.createApply(PMMLFunctions.ISMISSING, expression);
	}

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("missing_indicator", feature), OpType.CATEGORICAL, DataType.BOOLEAN, expression);

	return new BooleanFeature(encoder, derivedField);
}
 
Example 2
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	UFunc func = getFunc();

	if(func == null){
		return features;
	}

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

	for(int i = 0; i < features.size(); i++){
		ContinuousFeature continuousFeature = (features.get(i)).toContinuousFeature();

		DerivedField derivedField = encoder.ensureDerivedField(FeatureUtil.createName(func.getName(), continuousFeature), OpType.CONTINUOUS, DataType.DOUBLE, () -> UFuncUtil.encodeUFunc(func, Collections.singletonList(continuousFeature.ref())));

		result.add(new ContinuousFeature(encoder, derivedField));
	}

	return result;
}
 
Example 3
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String function = getFunction();

	if(features.size() <= 1){
		return features;
	}

	Apply apply = PMMLUtil.createApply(translateFunction(function));

	for(Feature feature : features){
		apply.addExpressions(feature.ref());
	}

	FieldName name = FeatureUtil.createName(function, features);

	DerivedField derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply);

	return Collections.singletonList(new ContinuousFeature(encoder, derivedField));
}
 
Example 4
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String pattern = getPattern();
	String replacement = getReplacement();

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);
	if(!(DataType.STRING).equals(feature.getDataType())){
		throw new IllegalArgumentException();
	}

	Apply apply = PMMLUtil.createApply(PMMLFunctions.REPLACE)
		.addExpressions(feature.ref())
		.addExpressions(PMMLUtil.createConstant(pattern, DataType.STRING), PMMLUtil.createConstant(replacement, DataType.STRING));

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("replace", feature), OpType.CATEGORICAL, DataType.STRING, apply);

	return Collections.singletonList(new StringFeature(encoder, derivedField));
}
 
Example 5
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	Integer begin = getBegin();
	Integer end = getEnd();

	if((begin < 0) || (end < begin)){
		throw new IllegalArgumentException();
	}

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);
	if(!(DataType.STRING).equals(feature.getDataType())){
		throw new IllegalArgumentException();
	}

	Apply apply = PMMLUtil.createApply(PMMLFunctions.SUBSTRING)
		.addExpressions(feature.ref())
		.addExpressions(PMMLUtil.createConstant(begin + 1, DataType.INTEGER), PMMLUtil.createConstant((end - begin), DataType.INTEGER));

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("substring", feature), OpType.CATEGORICAL, DataType.STRING, apply);

	return Collections.singletonList(new StringFeature(encoder, derivedField));
}
 
Example 6
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String pattern = getPattern();

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);
	if(!(DataType.STRING).equals(feature.getDataType())){
		throw new IllegalArgumentException();
	}

	Apply apply = PMMLUtil.createApply(PMMLFunctions.MATCHES)
		.addExpressions(feature.ref())
		.addExpressions(PMMLUtil.createConstant(pattern, DataType.STRING));

	DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("matches", feature), OpType.CATEGORICAL, DataType.BOOLEAN, apply);

	return Collections.singletonList(new BooleanFeature(encoder, derivedField));
}
 
Example 7
Source Project: jpmml-r   Source File: FormulaUtil.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
static
private FieldName prepareInputField(FunctionExpression.Argument argument, OpType opType, DataType dataType, RExpEncoder encoder){
	Expression expression = argument.getExpression();

	if(expression instanceof FieldRef){
		FieldRef fieldRef = (FieldRef)expression;

		return fieldRef.getField();
	} else

	if(expression instanceof Apply){
		Apply apply = (Apply)expression;

		DerivedField derivedField = encoder.createDerivedField(FieldName.create((argument.formatExpression()).trim()), opType, dataType, apply);

		return derivedField.getName();
	} else

	{
		throw new IllegalArgumentException();
	}
}
 
Example 8
static
private Field<?> resolveField(FieldName name, MiningModelEvaluationContext context){

	while(context != null){
		OutputField outputField = context.getOutputField(name);
		if(outputField != null){
			return outputField;
		}

		DerivedField localDerivedField = context.getLocalDerivedField(name);
		if(localDerivedField != null){
			return localDerivedField;
		}

		context = context.getParent();
	}

	return null;
}
 
Example 9
static
public FieldValue evaluate(DerivedField derivedField, EvaluationContext context){
	FieldName name = derivedField.getName();
	if(name == null){
		throw new MissingAttributeException(derivedField, PMMLAttributes.DERIVEDFIELD_NAME);
	}

	SymbolTable<FieldName> symbolTable = EvaluationContext.DERIVEDFIELD_GUARD_PROVIDER.get();

	if(symbolTable != null){
		symbolTable.lock(name);
	}

	try {
		return evaluateTypedExpressionContainer(derivedField, context);
	} finally {

		if(symbolTable != null){
			symbolTable.release(name);
		}
	}
}
 
Example 10
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 11
@Override
public void addDerivedField(DerivedField derivedField){

	try {
		super.addDerivedField(derivedField);
	} catch(RuntimeException re){
		FieldName name = derivedField.getName();

		String message = "Field " + name.getValue() + " is already defined. " +
			"Please refactor the pipeline so that it would not contain duplicate field declarations, " +
			"or use the " + (Alias.class).getName() + " wrapper class to override the default name with a custom name (eg. " + Alias.formatAliasExample() + ")";

		throw new IllegalArgumentException(message, re);
	}
}
 
Example 12
public void renameFeature(Feature feature, FieldName renamedName){
	FieldName name = feature.getName();

	org.dmg.pmml.Field<?> pmmlField = getField(name);

	if(pmmlField instanceof DataField){
		throw new IllegalArgumentException("User input field " + name.getValue() + " cannot be renamed");
	}

	DerivedField derivedField = removeDerivedField(name);

	try {
		Field field = Feature.class.getDeclaredField("name");

		if(!field.isAccessible()){
			field.setAccessible(true);
		}

		field.set(feature, renamedName);
	} catch(ReflectiveOperationException roe){
		throw new RuntimeException(roe);
	}

	derivedField.setName(renamedName);

	addDerivedField(derivedField);
}
 
Example 13
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	Object dtype = getDType();
	String expr = getExpr();

	Scope scope = new DataFrameScope(FieldName.create("X"), features);

	Expression expression = ExpressionTranslator.translate(expr, scope);

	DataType dataType;

	if(dtype != null){
		dataType = TransformerUtil.getDataType(dtype);
	} else

	{
		if(ExpressionTranslator.isString(expression, scope)){
			dataType = DataType.STRING;
		} else

		{
			dataType = DataType.DOUBLE;
		}
	}

	OpType opType = TransformerUtil.getOpType(dataType);

	DerivedField derivedField = encoder.createDerivedField(FieldName.create("eval(" + expr + ")"), opType, dataType, expression);

	return Collections.singletonList(new ContinuousFeature(encoder, derivedField));
}
 
Example 14
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String function = getFunction();
	Boolean trimBlanks = getTrimBlanks();

	if(function == null && !trimBlanks){
		return features;
	}

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

	for(Feature feature : features){
		Expression expression = feature.ref();

		if(function != null){
			expression = PMMLUtil.createApply(translateFunction(function), expression);
		} // End if

		if(trimBlanks){
			expression = PMMLUtil.createApply(PMMLFunctions.TRIMBLANKS, expression);
		}

		Field<?> field = encoder.toCategorical(feature.getName(), Collections.emptyList());

		// XXX: Should have been set by the previous transformer
		field.setDataType(DataType.STRING);

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("normalize", feature), OpType.CATEGORICAL, DataType.STRING, expression);

		feature = new StringFeature(encoder, derivedField);

		result.add(feature);
	}

	return result;
}
 
Example 15
Source Project: jpmml-r   Source File: MVRConverter.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
private void scaleFeatures(RExpEncoder encoder){
	RGenericVector mvr = getObject();

	RDoubleVector scale = mvr.getDoubleElement("scale", false);
	if(scale == null){
		return;
	}

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

	if(scale.size() != features.size()){
		throw new IllegalArgumentException();
	}

	for(int i = 0; i < features.size(); i++){
		Feature feature = features.get(i);
		Double factor = scale.getValue(i);

		if(ValueUtil.isOne(factor)){
			continue;
		}

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		Apply apply = PMMLUtil.createApply(PMMLFunctions.DIVIDE, continuousFeature.ref(), PMMLUtil.createConstant(factor));

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply);

		features.set(i, new ContinuousFeature(encoder, derivedField));
	}
}
 
Example 16
Source Project: jpmml-r   Source File: Formula.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public void addField(Field<?> field){
	RExpEncoder encoder = getEncoder();

	Feature feature = new ContinuousFeature(encoder, field);

	if(field instanceof DerivedField){
		DerivedField derivedField = (DerivedField)field;

		Expression expression = derivedField.getExpression();
		if(expression instanceof Apply){
			Apply apply = (Apply)expression;

			if(checkApply(apply, PMMLFunctions.POW, FieldRef.class, Constant.class)){
				List<Expression> expressions = apply.getExpressions();

				FieldRef fieldRef = (FieldRef)expressions.get(0);
				Constant constant = (Constant)expressions.get(1);

				try {
					String string = ValueUtil.asString(constant.getValue());

					int power = Integer.parseInt(string);

					feature = new PowerFeature(encoder, fieldRef.getField(), DataType.DOUBLE, power);
				} catch(NumberFormatException nfe){
					// Ignored
				}
			}
		}
	}

	putFeature(field.getName(), feature);

	this.fields.add(field);
}
 
Example 17
private Expression getOutputExpression(NeuralOutput neuralOutput){
	DerivedField derivedField = neuralOutput.getDerivedField();
	if(derivedField == null){
		throw new MissingElementException(neuralOutput, PMMLElements.NEURALOUTPUT_DERIVEDFIELD);
	}

	Expression expression = ExpressionUtil.ensureExpression(derivedField);

	if(expression instanceof FieldRef){
		FieldRef fieldRef = (FieldRef)expression;

		FieldName name = fieldRef.getField();
		if(name == null){
			throw new MissingAttributeException(fieldRef, org.dmg.pmml.PMMLAttributes.FIELDREF_FIELD);
		}

		Field<?> field = resolveField(name);
		if(field == null){
			throw new MissingFieldException(name, fieldRef);
		} // End if

		if(field instanceof DataField){
			return expression;
		} else

		if(field instanceof DerivedField){
			DerivedField targetDerivedField = (DerivedField)field;

			Expression targetExpression = ExpressionUtil.ensureExpression(targetDerivedField);

			return targetExpression;
		} else

		{
			throw new InvalidAttributeException(fieldRef, org.dmg.pmml.PMMLAttributes.FIELDREF_FIELD, name);
		}
	}

	return expression;
}
 
Example 18
@Override
public VisitorAction visit(BayesInputs bayesInputs){

	if(bayesInputs.hasBayesInputs()){
		List<BayesInput> content = bayesInputs.getBayesInputs();

		for(ListIterator<BayesInput> it = content.listIterator(); it.hasNext(); ){
			BayesInput bayesInput = it.next();

			FieldName name = bayesInput.getField();
			if(name == null){
				throw new MissingAttributeException(bayesInput, org.dmg.pmml.naive_bayes.PMMLAttributes.BAYESINPUT_FIELD);
			}

			DataType dataType;

			DerivedField derivedField = bayesInput.getDerivedField();
			if(derivedField != null){
				dataType = derivedField.getDataType();

				if(dataType == null){
					throw new MissingAttributeException(derivedField, PMMLAttributes.DERIVEDFIELD_DATATYPE);
				}
			} else

			{
				dataType = resolveDataType(name);
			} // End if

			if(dataType != null){
				it.set(new RichBayesInput(dataType, bayesInput));
			}
		}
	}

	return super.visit(bayesInputs);
}
 
Example 19
private void processDerivedFields(HasDerivedFields<?> hasDerivedFields){

		if(hasDerivedFields.hasDerivedFields()){
			List<DerivedField> derivedFields = hasDerivedFields.getDerivedFields();

			for(ListIterator<DerivedField> it = derivedFields.listIterator(); it.hasNext(); ){
				DerivedField derivedField = it.next();

				if(derivedField.hasValues()){
					it.set(new RichDerivedField(derivedField));
				}
			}
		}
	}
 
Example 20
@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 21
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder){
	StandardScalerModel transformer = getTransformer();

	Vector mean = transformer.mean();
	Vector std = transformer.std();

	boolean withMean = transformer.getWithMean();
	boolean withStd = transformer.getWithStd();

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

	if(withMean){
		SchemaUtil.checkSize(mean.size(), features);
	} // End if

	if(withStd){
		SchemaUtil.checkSize(std.size(), features);
	}

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

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

		FieldName name = formatName(transformer, i, length);

		Expression expression = null;

		if(withMean){
			double meanValue = mean.apply(i);

			if(!ValueUtil.isZero(meanValue)){
				ContinuousFeature continuousFeature = feature.toContinuousFeature();

				expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, continuousFeature.ref(), PMMLUtil.createConstant(meanValue));
			}
		} // End if

		if(withStd){
			double stdValue = std.apply(i);

			if(!ValueUtil.isOne(stdValue)){
				Double factor = (1d / stdValue);

				if(expression != null){
					expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, expression, PMMLUtil.createConstant(factor));
				} else

				{
					feature = new ProductFeature(encoder, feature, factor){

						@Override
						public ContinuousFeature toContinuousFeature(){
							Supplier<Apply> applySupplier = () -> {
								Feature feature = getFeature();
								Number factor = getFactor();

								return PMMLUtil.createApply(PMMLFunctions.MULTIPLY, (feature.toContinuousFeature()).ref(), PMMLUtil.createConstant(factor));
							};

							return toContinuousFeature(name, DataType.DOUBLE, applySupplier);
						}
					};
				}
			}
		} // End if

		if(expression != null){
			DerivedField derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, expression);

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

		{
			result.add(feature);
		}
	}

	return result;
}
 
Example 22
@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 23
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 24
static
public Feature encodeFeature(Feature feature, Boolean addIndicator, Object missingValue, Object replacementValue, MissingValueTreatmentMethod missingValueTreatmentMethod, SkLearnEncoder encoder){
	Field<?> field = feature.getField();

	if(field instanceof DataField && !addIndicator){
		DataField dataField = (DataField)field;

		encoder.addDecorator(dataField, new MissingValueDecorator(missingValueTreatmentMethod, replacementValue));

		if(missingValue != null){
			PMMLUtil.addValues(dataField, Collections.singletonList(missingValue), Value.Property.MISSING);
		}

		return feature;
	} // End if

	if((field instanceof DataField) || (field instanceof DerivedField)){
		Expression expression = feature.ref();

		if(missingValue != null){
			expression = PMMLUtil.createApply(PMMLFunctions.EQUAL, expression, PMMLUtil.createConstant(missingValue, feature.getDataType()));
		} else

		{
			expression = PMMLUtil.createApply(PMMLFunctions.ISMISSING, expression);
		}

		expression = PMMLUtil.createApply(PMMLFunctions.IF)
			.addExpressions(expression)
			.addExpressions(PMMLUtil.createConstant(replacementValue, feature.getDataType()), feature.ref());

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("imputer", feature), field.getOpType(), field.getDataType(), expression);

		DataType dataType = derivedField.getDataType();
		switch(dataType){
			case INTEGER:
			case FLOAT:
			case DOUBLE:
				return new ContinuousFeature(encoder, derivedField);
			case STRING:
				return new StringFeature(encoder, derivedField);
			default:
				return new ObjectFeature(encoder, derivedField.getName(), derivedField.getDataType());
		}
	} else

	{
		throw new IllegalArgumentException();
	}
}
 
Example 25
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	Boolean withCentering = getWithCentering();
	Boolean withScaling = getWithScaling();

	List<? extends Number> center = (withCentering ? getCenter() : null);
	List<? extends Number> scale = (withScaling ? getScale() : null);

	if(center == null && scale == null){
		return features;
	}

	ClassDictUtil.checkSize(features, center, scale);

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

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

		Number centerValue = (withCentering ? center.get(i) : 0d);
		Number scaleValue = (withScaling ? scale.get(i) : 1d);

		if(ValueUtil.isZero(centerValue) && ValueUtil.isOne(scaleValue)){
			result.add(feature);

			continue;
		}

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		// "($name - center) / scale"
		Expression expression = continuousFeature.ref();

		if(!ValueUtil.isZero(centerValue)){
			expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(centerValue));
		} // End if

		if(!ValueUtil.isOne(scaleValue)){
			expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(scaleValue));
		}

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("robust_scaler", continuousFeature), expression);

		result.add(new ContinuousFeature(encoder, derivedField));
	}

	return result;
}
 
Example 26
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	List<? extends Number> min = getMin();
	List<? extends Number> scale = getScale();

	ClassDictUtil.checkSize(features, min, scale);

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

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

		Number minValue = min.get(i);
		Number scaleValue = scale.get(i);

		if(ValueUtil.isOne(scaleValue) && ValueUtil.isZero(minValue)){
			result.add(feature);

			continue;
		}

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		// "($name * scale) + min"
		Expression expression = continuousFeature.ref();

		if(!ValueUtil.isOne(scaleValue)){
			expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, expression, PMMLUtil.createConstant(scaleValue));
		} // End if

		if(!ValueUtil.isZero(minValue)){
			expression = PMMLUtil.createApply(PMMLFunctions.ADD, expression, PMMLUtil.createConstant(minValue));
		}

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("mix_max_scaler", continuousFeature), expression);

		result.add(new ContinuousFeature(encoder, derivedField));
	}

	return result;
}
 
Example 27
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	Boolean withMean = getWithMean();
	Boolean withStd = getWithStd();

	List<? extends Number> mean = (withMean ? getMean() : null);
	List<? extends Number> std = (withStd ? getStd() : null);

	if(mean == null && std == null){
		return features;
	}

	ClassDictUtil.checkSize(features, mean, std);

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

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

		Number meanValue = (withMean ? mean.get(i) : 0d);
		Number stdValue = (withStd ? std.get(i) : 1d);

		if(ValueUtil.isZero(meanValue) && ValueUtil.isOne(stdValue)){
			result.add(feature);

			continue;
		}

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		// "($name - mean) / std"
		Expression expression = continuousFeature.ref();

		if(!ValueUtil.isZero(meanValue)){
			expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(meanValue));
		} // End if

		if(!ValueUtil.isOne(stdValue)){
			expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(stdValue));
		}

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("standard_scaler", continuousFeature), expression);

		result.add(new ContinuousFeature(encoder, derivedField));
	}

	return result;
}
 
Example 28
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	Number threshold = getThreshold();

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

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

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		// "($name <= threshold) ? 0 : 1"
		Apply apply = PMMLUtil.createApply(PMMLFunctions.THRESHOLD, continuousFeature.ref(), PMMLUtil.createConstant(threshold));

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("binarizer", continuousFeature), apply);

		result.add(new ContinuousFeature(encoder, derivedField));
	}

	return result;
}
 
Example 29
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	List<?> classes = getClasses();

	Number negLabel = getNegLabel();
	Number posLabel = getPosLabel();

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);

	List<Object> categories = new ArrayList<>();
	categories.addAll(classes);

	List<Number> labelCategories = new ArrayList<>();
	labelCategories.add(negLabel);
	labelCategories.add(posLabel);

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

	classes = prepareClasses(classes);

	for(int i = 0; i < classes.size(); i++){
		Object value = classes.get(i);

		if(ValueUtil.isZero(negLabel) && ValueUtil.isOne(posLabel)){
			result.add(new BinaryFeature(encoder, feature, value));
		} else

		{
			// "($name == value) ? pos_label : neg_label"
			Apply apply = PMMLUtil.createApply(PMMLFunctions.IF)
				.addExpressions(PMMLUtil.createApply(PMMLFunctions.EQUAL, feature.ref(), PMMLUtil.createConstant(value, feature.getDataType())))
				.addExpressions(PMMLUtil.createConstant(posLabel), PMMLUtil.createConstant(negLabel));

			FieldName name = (classes.size() > 1 ? FeatureUtil.createName("label_binarizer", feature, i) : FeatureUtil.createName("label_binarizer", feature));

			DerivedField derivedField = encoder.createDerivedField(name, apply);

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

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

	return result;
}
 
Example 30
public DerivedField createDerivedField(FieldName name, Expression expression){
	return createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, expression);
}