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

The following examples show how to use org.jpmml.converter.ValueUtil#isOne() . 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: MVRConverter.java    From jpmml-r with 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 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: MiningModelUtil.java    From pyramid with Apache License 2.0 5 votes vote down vote up
static
public Segmentation createSegmentation(Segmentation.MultipleModelMethod multipleModelMethod, List<? extends Model> models, List<? extends Number> weights){

    if((weights != null) && (models.size() != weights.size())){
        throw new IllegalArgumentException();
    }

    List<Segment> segments = new ArrayList<>();

    for(int i = 0; i < models.size(); i++){
        Model model = models.get(i);
        Number weight = (weights != null ? weights.get(i) : null);

        Segment segment = new Segment()
                .setId(String.valueOf(i + 1))
                .setPredicate(new True())
                .setModel(model);

        if(weight != null && !ValueUtil.isOne(weight)){
            segment.setWeight(ValueUtil.asDouble(weight));
        }

        segments.add(segment);
    }

    return new Segmentation(multipleModelMethod, segments);
}
 
Example 4
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 5
Source File: ObjFunction.java    From jpmml-xgboost with GNU Affero General Public License v3.0 4 votes vote down vote up
static
protected MiningModel createMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){

	if(weights != null){

		if(trees.size() != weights.size()){
			throw new IllegalArgumentException();
		}
	} // End if

	if(ntreeLimit != null){

		if(ntreeLimit > trees.size()){
			throw new IllegalArgumentException("Tree limit " + ntreeLimit + " is greater than the number of trees");
		}

		trees = trees.subList(0, ntreeLimit);

		if(weights != null){
			weights = weights.subList(0, ntreeLimit);
		}
	} // End if

	if(weights != null){
		weights = new ArrayList<>(weights);
	}

	ContinuousLabel continuousLabel = (ContinuousLabel)schema.getLabel();

	Schema segmentSchema = schema.toAnonymousSchema();

	PredicateManager predicateManager = new PredicateManager();

	List<TreeModel> treeModels = new ArrayList<>();

	boolean equalWeights = true;

	Iterator<RegTree> treeIt = trees.iterator();
	Iterator<Float> weightIt = (weights != null ? weights.iterator() : null);

	while(treeIt.hasNext()){
		RegTree tree = treeIt.next();
		Float weight = (weightIt != null ? weightIt.next() : null);

		if(tree.isEmpty()){
			weightIt.remove();

			continue;
		} // End if

		if(weight != null){
			equalWeights &= ValueUtil.isOne(weight);
		}

		TreeModel treeModel = tree.encodeTreeModel(predicateManager, segmentSchema);

		treeModels.add(treeModel);
	}

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(continuousLabel))
		.setMathContext(MathContext.FLOAT)
		.setSegmentation(MiningModelUtil.createSegmentation(equalWeights ? Segmentation.MultipleModelMethod.SUM : Segmentation.MultipleModelMethod.WEIGHTED_SUM, treeModels, weights))
		.setTargets(ModelUtil.createRescaleTargets(null, base_score, continuousLabel));

	return miningModel;
}
 
Example 6
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 7
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 8
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 9
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 10
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 11
Source File: PCAModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 3 votes vote down vote up
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder){
	PCAModel transformer = getTransformer();

	DenseMatrix pc = transformer.pc();

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

	MatrixUtil.checkRows(features.size(), pc);

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

	for(int i = 0, length = transformer.getK(); i < length; i++){
		Apply apply = new Apply(PMMLFunctions.SUM);

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

			ContinuousFeature continuousFeature = feature.toContinuousFeature();

			Expression expression = continuousFeature.ref();

			Double coefficient = pc.apply(j, i);
			if(!ValueUtil.isOne(coefficient)){
				expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, expression, PMMLUtil.createConstant(coefficient));
			}

			apply.addExpressions(expression);
		}

		DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i, length), OpType.CONTINUOUS, DataType.DOUBLE, apply);

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

	return result;
}
 
Example 12
Source File: MaxAbsScaler.java    From jpmml-sklearn with GNU Affero General Public License v3.0 3 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	List<? extends Number> scale = getScale();

	ClassDictUtil.checkSize(features, scale);

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

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

		Number value = scale.get(i);
		if(ValueUtil.isOne(value)){
			result.add(feature);

			continue;
		}

		ContinuousFeature continuousFeature = feature.toContinuousFeature();

		// "$name / scale"
		Apply apply = PMMLUtil.createApply(PMMLFunctions.DIVIDE, continuousFeature.ref(), PMMLUtil.createConstant(value));

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

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

	return result;
}
 
Example 13
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;
}
 
Example 14
Source File: TruncatedSVD.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();

	ClassDictUtil.checkSize(numberOfFeatures, features);

	String id = "svd@" + String.valueOf(TruncatedSVD.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 componentValue = component.get(j);

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

				continue;
			}

			ContinuousFeature continuousFeature = feature.toContinuousFeature();

			// "$name[i] * component[i]"
			Expression expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, continuousFeature.ref(), PMMLUtil.createConstant(componentValue));

			apply.addExpressions(expression);
		}

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

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

	return result;
}