Java Code Examples for org.jpmml.converter.PMMLUtil#createApply()

The following examples show how to use org.jpmml.converter.PMMLUtil#createApply() . 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: RegexTokenizerConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder){
	RegexTokenizer transformer = getTransformer();

	if(!transformer.getGaps()){
		throw new IllegalArgumentException("Expected splitter mode, got token matching mode");
	} // End if

	if(transformer.getMinTokenLength() != 1){
		throw new IllegalArgumentException("Expected 1 as minimum token length, got " + transformer.getMinTokenLength() + " as minimum token length");
	}

	Feature feature = encoder.getOnlyFeature(transformer.getInputCol());

	Field<?> field = feature.getField();

	if(transformer.getToLowercase()){
		Apply apply = PMMLUtil.createApply(PMMLFunctions.LOWERCASE, feature.ref());

		field = encoder.createDerivedField(FeatureUtil.createName("lowercase", feature), OpType.CATEGORICAL, DataType.STRING, apply);
	}

	return Collections.singletonList(new DocumentFeature(encoder, field, transformer.getPattern()));
}
 
Example 2
Source File: TfidfVectorizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public DefineFunction encodeDefineFunction(){
	TfidfTransformer transformer = getTransformer();

	DefineFunction defineFunction = super.encodeDefineFunction();

	Expression expression = defineFunction.getExpression();

	Boolean sublinearTf = transformer.getSublinearTf();
	if(sublinearTf){
		expression = PMMLUtil.createApply(PMMLFunctions.ADD, PMMLUtil.createApply(PMMLFunctions.LN, expression), PMMLUtil.createConstant(1d));
	} // End if

	Boolean useIdf = transformer.getUseIdf();
	if(useIdf){
		ParameterField weight = new ParameterField(FieldName.create("weight"));

		defineFunction.addParameterFields(weight);

		expression = PMMLUtil.createApply(PMMLFunctions.MULTIPLY, expression, new FieldRef(weight.getName()));
	}

	defineFunction.setExpression(expression);

	return defineFunction;
}
 
Example 3
Source File: EarthConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private Apply createHingeFunction(int dir, Feature feature, double cut){
	Expression expression;

	switch(dir){
		case -1:
			expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, PMMLUtil.createConstant(cut), feature.ref());
			break;
		case 1:
			expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, feature.ref(), PMMLUtil.createConstant(cut));
			break;
		default:
			throw new IllegalArgumentException();
	}

	return PMMLUtil.createApply(PMMLFunctions.MAX, expression, PMMLUtil.createConstant(0d));
}
 
Example 4
Source File: ImputerUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
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 5
Source File: StringNormalizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@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 6
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 7
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 8
Source File: OneClassSVM.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public SupportVectorMachineModel encodeModel(Schema schema){
	Transformation outlier = new OutlierTransformation(){

		@Override
		public Expression createExpression(FieldRef fieldRef){
			return PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, fieldRef, PMMLUtil.createConstant(0d));
		}
	};

	SupportVectorMachineModel supportVectorMachineModel = super.encodeModel(schema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE, outlier));

	Output output = supportVectorMachineModel.getOutput();

	List<OutputField> outputFields = output.getOutputFields();
	if(outputFields.size() != 2){
		throw new IllegalArgumentException();
	}

	OutputField decisionFunctionOutputField = outputFields.get(0);

	if(!decisionFunctionOutputField.isFinalResult()){
		decisionFunctionOutputField.setFinalResult(true);
	}

	return supportVectorMachineModel;
}
 
Example 9
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 10
Source File: Binarizer.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){
	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 11
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 12
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 13
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 14
Source File: CountVectorizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
public Apply encodeApply(String function, Feature feature, int index, String term){
	Constant constant = PMMLUtil.createConstant(term, DataType.STRING);

	return PMMLUtil.createApply(function, feature.ref(), constant);
}
 
Example 15
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 16
Source File: SVMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public SupportVectorMachineModel encodeModel(Schema schema){
	RGenericVector svm = getObject();

	RDoubleVector type = svm.getDoubleElement("type");
	RDoubleVector kernel = svm.getDoubleElement("kernel");
	RDoubleVector degree = svm.getDoubleElement("degree");
	RDoubleVector gamma = svm.getDoubleElement("gamma");
	RDoubleVector coef0 = svm.getDoubleElement("coef0");
	RGenericVector yScale = svm.getGenericElement("y.scale");
	RIntegerVector nSv = svm.getIntegerElement("nSV");
	RDoubleVector sv = svm.getDoubleElement("SV");
	RDoubleVector rho = svm.getDoubleElement("rho");
	RDoubleVector coefs = svm.getDoubleElement("coefs");

	Type svmType = Type.values()[ValueUtil.asInt(type.asScalar())];
	Kernel svmKernel = Kernel.values()[ValueUtil.asInt(kernel.asScalar())];

	org.dmg.pmml.support_vector_machine.Kernel pmmlKernel = svmKernel.createKernel(degree.asScalar(), gamma.asScalar(), coef0.asScalar());

	SupportVectorMachineModel supportVectorMachineModel;

	switch(svmType){
		case C_CLASSIFICATION:
		case NU_CLASSIFICATION:
			{
				supportVectorMachineModel = encodeClassification(pmmlKernel, sv, nSv, rho, coefs, schema);
			}
			break;
		case ONE_CLASSIFICATION:
			{
				Transformation outlier = new OutlierTransformation(){

					@Override
					public Expression createExpression(FieldRef fieldRef){
						return PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, fieldRef, PMMLUtil.createConstant(0d));
					}
				};

				supportVectorMachineModel = encodeRegression(pmmlKernel, sv, rho, coefs, schema)
					.setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE, outlier));

				if(yScale != null && yScale.size() > 0){
					throw new IllegalArgumentException();
				}
			}
			break;
		case EPS_REGRESSION:
		case NU_REGRESSION:
			{
				supportVectorMachineModel = encodeRegression(pmmlKernel, sv, rho, coefs, schema);

				if(yScale != null && yScale.size() > 0){
					RDoubleVector yScaledCenter = yScale.getDoubleElement("scaled:center");
					RDoubleVector yScaledScale = yScale.getDoubleElement("scaled:scale");

					supportVectorMachineModel.setTargets(ModelUtil.createRescaleTargets(-1d * yScaledScale.asScalar(), yScaledCenter.asScalar(), (ContinuousLabel)schema.getLabel()));
				}
			}
			break;
		default:
			throw new IllegalArgumentException();
	}

	return supportVectorMachineModel;
}
 
Example 17
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 18
Source File: TokenizerConverter.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){
	Tokenizer transformer = getTransformer();

	Feature feature = encoder.getOnlyFeature(transformer.getInputCol());

	Apply apply = PMMLUtil.createApply(PMMLFunctions.LOWERCASE, feature.ref());

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

	return Collections.singletonList(new DocumentFeature(encoder, derivedField, "\\s+"));
}
 
Example 19
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;
}
 
Example 20
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;
}