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

The following examples show how to use org.jpmml.converter.ValueUtil#asInt() . 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: RangerConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
private List<TreeModel> encodeForest(RGenericVector forest, MiningFunction miningFunction, ScoreEncoder scoreEncoder, Schema schema){
	RNumberVector<?> numTrees = forest.getNumericElement("num.trees");
	RGenericVector childNodeIDs = forest.getGenericElement("child.nodeIDs");
	RGenericVector splitVarIDs = forest.getGenericElement("split.varIDs");
	RGenericVector splitValues = forest.getGenericElement("split.values");
	RGenericVector terminalClassCounts = forest.getGenericElement("terminal.class.counts", false);

	Schema segmentSchema = schema.toAnonymousSchema();

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

	for(int i = 0; i < ValueUtil.asInt(numTrees.asScalar()); i++){
		TreeModel treeModel = encodeTreeModel(miningFunction, scoreEncoder, (RGenericVector)childNodeIDs.getValue(i), (RNumberVector<?>)splitVarIDs.getValue(i), (RNumberVector<?>)splitValues.getValue(i), (terminalClassCounts != null ? (RGenericVector)terminalClassCounts.getValue(i) : null), segmentSchema);

		treeModels.add(treeModel);
	}

	return treeModels;
}
 
Example 2
Source File: RPartConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
public RPartConverter(RGenericVector rpart){
	super(rpart);

	RGenericVector control = rpart.getGenericElement("control");

	RNumberVector<?> useSurrogate = control.getNumericElement("usesurrogate");

	this.useSurrogate = ValueUtil.asInt(useSurrogate.asScalar());

	switch(this.useSurrogate){
		case 0:
		case 1:
		case 2:
			break;
		default:
			throw new IllegalArgumentException();
	}
}
 
Example 3
Source File: SVMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
private void encodeFormula(RExpEncoder encoder){
	RGenericVector svm = getObject();

	RDoubleVector type = svm.getDoubleElement("type");
	RDoubleVector sv = svm.getDoubleElement("SV");
	RVector<?> levels = svm.getVectorElement("levels");
	RExp terms = svm.getElement("terms");
	RGenericVector xlevels = DecorationUtil.getGenericElement(svm, "xlevels");

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

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

	FormulaContext context = new XLevelsFormulaContext(xlevels);

	Formula formula = FormulaUtil.createFormula(terms, context, encoder);

	switch(svmType){
		case C_CLASSIFICATION:
		case NU_CLASSIFICATION:
			FormulaUtil.setLabel(formula, terms, levels, encoder);
			break;
		case ONE_CLASSIFICATION:
			encoder.setLabel(new ContinuousLabel(null, DataType.DOUBLE));
			break;
		case EPS_REGRESSION:
		case NU_REGRESSION:
			FormulaUtil.setLabel(formula, terms, null, encoder);
			break;
	}

	FormulaUtil.addFeatures(formula, columnNames, true, encoder);

	scaleFeatures(encoder);
}
 
Example 4
Source File: KNeighborsClassifier.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<?> getY(){
	List<? extends Number> y = getNumberArray("_y");

	Function<Number, Object> function = new Function<Number, Object>(){

		private List<?> classes = getClasses();


		@Override
		public Object apply(Number number){
			int index = ValueUtil.asInt(number);

			return this.classes.get(index);
		}
	};

	return Lists.transform(y, function);
}
 
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;
}
 
Example 6
Source File: RandomForestConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
private MiningModel encodeRegression(RGenericVector forest, Schema schema){
	RNumberVector<?> leftDaughter = forest.getNumericElement("leftDaughter");
	RNumberVector<?> rightDaughter = forest.getNumericElement("rightDaughter");
	RDoubleVector nodepred = forest.getDoubleElement("nodepred");
	RNumberVector<?> bestvar = forest.getNumericElement("bestvar");
	RDoubleVector xbestsplit = forest.getDoubleElement("xbestsplit");
	RIntegerVector nrnodes = forest.getIntegerElement("nrnodes");
	RNumberVector<?> ntree = forest.getNumericElement("ntree");

	ScoreEncoder<Double> scoreEncoder = new ScoreEncoder<Double>(){

		@Override
		public Double encode(Double value){
			return value;
		}
	};

	int rows = nrnodes.asScalar();
	int columns = ValueUtil.asInt(ntree.asScalar());

	Schema segmentSchema = schema.toAnonymousSchema();

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

	for(int i = 0; i < columns; i++){
		TreeModel treeModel = encodeTreeModel(
				MiningFunction.REGRESSION,
				scoreEncoder,
				FortranMatrixUtil.getColumn(leftDaughter.getValues(), rows, columns, i),
				FortranMatrixUtil.getColumn(rightDaughter.getValues(), rows, columns, i),
				FortranMatrixUtil.getColumn(nodepred.getValues(), rows, columns, i),
				FortranMatrixUtil.getColumn(bestvar.getValues(), rows, columns, i),
				FortranMatrixUtil.getColumn(xbestsplit.getValues(), rows, columns, i),
				segmentSchema
			);

		treeModels.add(treeModel);
	}

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels));

	return miningModel;
}
 
Example 7
Source File: RandomForestConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
private MiningModel encodeClassification(RGenericVector forest, Schema schema){
	RNumberVector<?> bestvar = forest.getNumericElement("bestvar");
	RNumberVector<?> treemap = forest.getNumericElement("treemap");
	RIntegerVector nodepred = forest.getIntegerElement("nodepred");
	RDoubleVector xbestsplit = forest.getDoubleElement("xbestsplit");
	RIntegerVector nrnodes = forest.getIntegerElement("nrnodes");
	RDoubleVector ntree = forest.getDoubleElement("ntree");

	int rows = nrnodes.asScalar();
	int columns = ValueUtil.asInt(ntree.asScalar());

	CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel();

	ScoreEncoder<Integer> scoreEncoder = new ScoreEncoder<Integer>(){

		@Override
		public Object encode(Integer value){
			return categoricalLabel.getValue(value - 1);
		}
	};

	Schema segmentSchema = schema.toAnonymousSchema();

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

	for(int i = 0; i < columns; i++){
		List<? extends Number> daughters = FortranMatrixUtil.getColumn(treemap.getValues(), 2 * rows, columns, i);

		TreeModel treeModel = encodeTreeModel(
				MiningFunction.CLASSIFICATION,
				scoreEncoder,
				FortranMatrixUtil.getColumn(daughters, rows, 2, 0),
				FortranMatrixUtil.getColumn(daughters, rows, 2, 1),
				FortranMatrixUtil.getColumn(nodepred.getValues(), rows, columns, i),
				FortranMatrixUtil.getColumn(bestvar.getValues(), rows, columns, i),
				FortranMatrixUtil.getColumn(xbestsplit.getValues(), rows, columns, i),
				segmentSchema
			);

		treeModels.add(treeModel);
	}

	MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.MAJORITY_VOTE, treeModels))
		.setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));

	return miningModel;
}
 
Example 8
Source File: RPartConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
private List<Predicate> encodePredicates(Feature feature, int splitOffset, RNumberVector<?> splits, RIntegerVector csplit){
	Predicate leftPredicate;
	Predicate rightPredicate;

	RIntegerVector splitsDim = splits.dim();

	int splitRows = splitsDim.getValue(0);
	int splitColumns = splitsDim.getValue(1);

	List<? extends Number> ncat = FortranMatrixUtil.getColumn(splits.getValues(), splitRows, splitColumns, 1);
	List<? extends Number> index = FortranMatrixUtil.getColumn(splits.getValues(), splitRows, splitColumns, 3);

	int splitType = ValueUtil.asInt(ncat.get(splitOffset));

	Number splitValue = index.get(splitOffset);

	if(Math.abs(splitType) == 1){
		SimplePredicate.Operator leftOperator;
		SimplePredicate.Operator rightOperator;

		if(splitType == -1){
			leftOperator = SimplePredicate.Operator.LESS_THAN;
			rightOperator = SimplePredicate.Operator.GREATER_OR_EQUAL;
		} else

		{
			leftOperator = SimplePredicate.Operator.GREATER_OR_EQUAL;
			rightOperator = SimplePredicate.Operator.LESS_THAN;
		}

		leftPredicate = createSimplePredicate(feature, leftOperator, splitValue);
		rightPredicate = createSimplePredicate(feature, rightOperator, splitValue);
	} else

	{
		CategoricalFeature categoricalFeature = (CategoricalFeature)feature;

		RIntegerVector csplitDim = csplit.dim();

		int csplitRows = csplitDim.getValue(0);
		int csplitColumns = csplitDim.getValue(1);

		List<Integer> csplitRow = FortranMatrixUtil.getRow(csplit.getValues(), csplitRows, csplitColumns, ValueUtil.asInt(splitValue) - 1);

		List<?> values = categoricalFeature.getValues();

		leftPredicate = createSimpleSetPredicate(categoricalFeature, selectValues(values, csplitRow, 1));
		rightPredicate = createSimpleSetPredicate(categoricalFeature, selectValues(values, csplitRow, 3));
	}

	return Arrays.asList(leftPredicate, rightPredicate);
}
 
Example 9
Source File: RangerConverter.java    From jpmml-r with GNU Affero General Public License v3.0 3 votes vote down vote up
private MiningModel encodeClassification(RGenericVector ranger, Schema schema){
	RGenericVector forest = ranger.getGenericElement("forest");

	RStringVector levels = forest.getStringElement("levels");

	ScoreEncoder scoreEncoder = new ScoreEncoder(){

		@Override
		public Node encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount){
			int index = ValueUtil.asInt(splitValue);

			if(terminalClassCount != null){
				throw new IllegalArgumentException();
			}

			node.setScore(levels.getValue(index - 1));

			return node;
		}
	};

	List<TreeModel> treeModels = encodeForest(forest, MiningFunction.CLASSIFICATION, scoreEncoder, schema);

	MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(schema.getLabel()))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.MAJORITY_VOTE, treeModels));

	return miningModel;
}
 
Example 10
Source File: OneHotEncoder.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){
	List<? extends Number> values = getValues();

	ClassDictUtil.checkSize(1, features);

	Feature feature = features.get(0);

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

	if(feature instanceof CategoricalFeature){
		CategoricalFeature categoricalFeature = (CategoricalFeature)feature;

		ClassDictUtil.checkSize(values, categoricalFeature.getValues());

		for(int i = 0; i < values.size(); i++){
			result.add(new BinaryFeature(encoder, categoricalFeature, categoricalFeature.getValue(i)));
		}
	} else

	if(feature instanceof WildcardFeature){
		WildcardFeature wildcardFeature = (WildcardFeature)feature;

		List<Integer> categories = new ArrayList<>();

		for(int i = 0; i < values.size(); i++){
			Number value = values.get(i);

			Integer category = ValueUtil.asInt(value);

			categories.add(category);

			result.add(new BinaryFeature(encoder, wildcardFeature, category));
		}

		wildcardFeature.toCategoricalFeature(categories);
	} else

	{
		throw new IllegalArgumentException();
	}

	return result;
}