org.jpmml.converter.Transformation Java Examples

The following examples show how to use org.jpmml.converter.Transformation. 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: LinearSVCModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public MiningModel encodeModel(Schema schema){
	LinearSVCModel model = getTransformer();

	Transformation transformation = new AbstractTransformation(){

		@Override
		public Expression createExpression(FieldRef fieldRef){
			return PMMLUtil.createApply(PMMLFunctions.THRESHOLD)
				.addExpressions(fieldRef, PMMLUtil.createConstant(model.getThreshold()));
		}
	};

	Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.DOUBLE);

	Model linearModel = LinearModelUtil.createRegression(this, model.coefficients(), model.intercept(), segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("margin"), OpType.CONTINUOUS, DataType.DOUBLE, transformation));

	return MiningModelUtil.createBinaryLogisticClassification(linearModel, 1d, 0d, RegressionModel.NormalizationMethod.NONE, false, schema);
}
 
Example #2
Source File: HingeClassification.java    From jpmml-xgboost with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public MiningModel encodeMiningModel(List<RegTree> trees, List<Float> weights, float base_score, Integer ntreeLimit, Schema schema){
	Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.FLOAT);

	Transformation transformation = new FunctionTransformation(PMMLFunctions.THRESHOLD){

		@Override
		public FieldName getName(FieldName name){
			return FieldName.create("hinge(" + name + ")");
		}

		@Override
		public Expression createExpression(FieldRef fieldRef){
			Apply apply = (Apply)super.createExpression(fieldRef);

			apply.addExpressions(PMMLUtil.createConstant(0f));

			return apply;
		}
	};

	MiningModel miningModel = createMiningModel(trees, weights, base_score, ntreeLimit, segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("xgbValue"), OpType.CONTINUOUS, DataType.FLOAT, transformation));

	return MiningModelUtil.createBinaryLogisticClassification(miningModel, 1d, 0d, RegressionModel.NormalizationMethod.NONE, true, schema);
}
 
Example #3
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 #4
Source File: LossFunction.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
abstract
public Transformation createTransformation();
 
Example #5
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;
}