Java Code Examples for org.jpmml.converter.ValueUtil#isZero()

The following examples show how to use org.jpmml.converter.ValueUtil#isZero() . 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: RegTree.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
public boolean isEmpty(){
	Node node = this.nodes[0];

	if(!node.is_leaf()){
		return false;
	} else

	{
		Float value = node.leaf_value();

		return ValueUtil.isZero(value);
	}
}
 
Example 2
Source File: PreProcessEncoder.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
private Expression encodeExpression(FieldName name, Expression expression){
	List<Double> ranges = this.ranges.get(name);
	if(ranges != null){
		Double min = ranges.get(0);
		Double max = ranges.get(1);

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

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

	Double mean = this.mean.get(name);
	if(mean != null && !ValueUtil.isZero(mean)){
		expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(mean));
	}

	Double std = this.std.get(name);
	if(std != null && !ValueUtil.isOne(std)){
		expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(std));
	}

	Double median = this.median.get(name);
	if(median != null){
		expression = PMMLUtil.createApply(PMMLFunctions.IF)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.ISNOTMISSING, new FieldRef(name)))
			.addExpressions(expression, PMMLUtil.createConstant(median));
	}

	return expression;
}
 
Example 3
Source File: StandardScalerModelConverter.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){
	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 4
Source File: RobustScaler.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){
	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 5
Source File: MinMaxScaler.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<? 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 6
Source File: StandardScaler.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){
	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 7
Source File: LabelBinarizer.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<?> 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 8
Source File: SVMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
private void scaleFeatures(RExpEncoder encoder){
	RGenericVector svm = getObject();

	RDoubleVector sv = svm.getDoubleElement("SV");
	RBooleanVector scaled = svm.getBooleanElement("scaled");
	RGenericVector xScale = svm.getGenericElement("x.scale");

	RStringVector rowNames = sv.dimnames(0);
	RStringVector columnNames = sv.dimnames(1);

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

	if((scaled.size() != columnNames.size()) || (scaled.size() != features.size())){
		throw new IllegalArgumentException();
	}

	RDoubleVector xScaledCenter = xScale.getDoubleElement("scaled:center");
	RDoubleVector xScaledScale = xScale.getDoubleElement("scaled:scale");

	for(int i = 0; i < columnNames.size(); i++){
		String columnName = columnNames.getValue(i);

		if(!scaled.getValue(i)){
			continue;
		}

		Feature feature = features.get(i);

		Double center = xScaledCenter.getElement(columnName);
		Double scale = xScaledScale.getElement(columnName);

		if(ValueUtil.isZero(center) && ValueUtil.isOne(scale)){
			continue;
		}

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		Expression expression = continuousFeature.ref();

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

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

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

		features.set(i, new ContinuousFeature(encoder, derivedField));
	}
}
 
Example 9
Source File: PCA.java    From jpmml-sklearn with GNU Affero General Public License v3.0 2 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	int[] shape = getComponentsShape();

	int numberOfComponents = shape[0];
	int numberOfFeatures = shape[1];

	List<? extends Number> components = getComponents();
	List<? extends Number> mean = getMean();

	ClassDictUtil.checkSize(numberOfFeatures, features, mean);

	Boolean whiten = getWhiten();

	List<? extends Number> explainedVariance = (whiten ? getExplainedVariance() : null);

	ClassDictUtil.checkSize(numberOfComponents, explainedVariance);

	String id = "pca@" + String.valueOf(PCA.SEQUENCE.getAndIncrement());

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

	for(int i = 0; i < numberOfComponents; i++){
		List<? extends Number> component = CMatrixUtil.getRow(components, numberOfComponents, numberOfFeatures, i);

		Apply apply = PMMLUtil.createApply(PMMLFunctions.SUM);

		for(int j = 0; j < numberOfFeatures; j++){
			Feature feature = features.get(j);

			Number meanValue = mean.get(j);
			Number componentValue = component.get(j);

			if(ValueUtil.isZero(meanValue) && ValueUtil.isOne(componentValue)){
				apply.addExpressions(feature.ref());

				continue;
			}

			ContinuousFeature continuousFeature = feature.toContinuousFeature();

			// "($name[i] - mean[i]) * component[i]"
			Expression expression = continuousFeature.ref();

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

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

			apply.addExpressions(expression);
		}

		if(whiten){
			Number explainedVarianceValue = explainedVariance.get(i);

			if(!ValueUtil.isOne(explainedVarianceValue)){
				apply = PMMLUtil.createApply(PMMLFunctions.DIVIDE, apply, PMMLUtil.createConstant(Math.sqrt(ValueUtil.asDouble(explainedVarianceValue))));
			}
		}

		DerivedField derivedField = encoder.createDerivedField(FieldName.create(id + "[" + String.valueOf(i) + "]"), apply);

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

	return result;
}