Java Code Examples for org.jpmml.converter.CategoricalLabel#getValue()

The following examples show how to use org.jpmml.converter.CategoricalLabel#getValue() . 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: ScoreDistributionManager.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
public List<ScoreDistribution> createScoreDistribution(CategoricalLabel categoricalLabel, double[] recordCounts){
	List<ScoreDistribution> result = new ArrayList<>();

	for(int i = 0; i < categoricalLabel.size(); i++){
		Object value = categoricalLabel.getValue(i);
		double recordCount = recordCounts[i];

		ScoreDistribution scoreDistribution = new InternableScoreDistribution()
			.setValue(value)
			.setRecordCount(recordCount);

		scoreDistribution = intern(scoreDistribution);

		result.add(scoreDistribution);
	}

	return result;
}
 
Example 2
Source File: GeneralizedLinearRegressionModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public GeneralRegressionModel encodeModel(Schema schema){
	GeneralizedLinearRegressionModel model = getTransformer();

	Object targetCategory = null;

	MiningFunction miningFunction = getMiningFunction();
	switch(miningFunction){
		case CLASSIFICATION:
			CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel();

			SchemaUtil.checkSize(2, categoricalLabel);

			targetCategory = categoricalLabel.getValue(1);
			break;
		default:
			break;
	}

	List<Feature> features = new ArrayList<>(schema.getFeatures());
	List<Double> featureCoefficients = new ArrayList<>(VectorUtil.toList(model.coefficients()));

	RegressionTableUtil.simplify(this, targetCategory, features, featureCoefficients);

	GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, miningFunction, ModelUtil.createMiningSchema(schema.getLabel()), null, null, null)
		.setDistribution(parseFamily(model.getFamily()))
		.setLinkFunction(parseLinkFunction(model.getLink()))
		.setLinkParameter(parseLinkParameter(model.getLink()));

	GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, model.intercept(), targetCategory);

	return generalRegressionModel;
}
 
Example 3
Source File: LinearModelUtil.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public <C extends ModelConverter<?> & HasRegressionTableOptions> Model createBinaryLogisticClassification(C converter, Vector coefficients, double intercept, Schema schema){
	CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel();

	String representation = (String)converter.getOption(HasRegressionTableOptions.OPTION_REPRESENTATION, null);

	List<Feature> features = new ArrayList<>(schema.getFeatures());
	List<Double> featureCoefficients = new ArrayList<>(VectorUtil.toList(coefficients));

	RegressionTableUtil.simplify(converter, null, features, featureCoefficients);

	if(representation != null && (GeneralRegressionModel.class.getSimpleName()).equalsIgnoreCase(representation)){
		Object targetCategory = categoricalLabel.getValue(1);

		GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), null, null, null)
			.setLinkFunction(GeneralRegressionModel.LinkFunction.LOGIT);

		GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, targetCategory);

		return generalRegressionModel;
	}

	return RegressionModelUtil.createBinaryLogisticClassification(features, featureCoefficients, intercept, RegressionModel.NormalizationMethod.LOGIT, true, schema);
}
 
Example 4
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 5
Source File: GLMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	RGenericVector glm = getObject();

	RDoubleVector coefficients = glm.getDoubleElement("coefficients");
	RGenericVector family = glm.getGenericElement("family");

	Double intercept = coefficients.getElement(getInterceptName(), false);

	RStringVector familyFamily = family.getStringElement("family");
	RStringVector familyLink = family.getStringElement("link");

	Label label = schema.getLabel();
	List<? extends Feature> features = schema.getFeatures();

	SchemaUtil.checkSize(coefficients.size() - (intercept != null ? 1 : 0), features);

	List<Double> featureCoefficients = getFeatureCoefficients(features, coefficients);

	MiningFunction miningFunction = getMiningFunction(familyFamily.asScalar());

	Object targetCategory = null;

	switch(miningFunction){
		case CLASSIFICATION:
			{
				CategoricalLabel categoricalLabel = (CategoricalLabel)label;

				SchemaUtil.checkSize(2, categoricalLabel);

				targetCategory = categoricalLabel.getValue(1);
			}
			break;
		default:
			break;
	}

	GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, miningFunction, ModelUtil.createMiningSchema(label), null, null, null)
		.setDistribution(parseFamily(familyFamily.asScalar()))
		.setLinkFunction(parseLinkFunction(familyLink.asScalar()))
		.setLinkParameter(parseLinkParameter(familyLink.asScalar()));

	GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, targetCategory);

	switch(miningFunction){
		case CLASSIFICATION:
			generalRegressionModel.setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, (CategoricalLabel)label));
			break;
		default:
			break;
	}

	return generalRegressionModel;
}
 
Example 6
Source File: LinearModelUtil.java    From jpmml-sparkml with GNU Affero General Public License v3.0 3 votes vote down vote up
static
public <C extends ModelConverter<?> & HasRegressionTableOptions> Model createSoftmaxClassification(C converter, Matrix coefficients, Vector intercepts, Schema schema){
	CategoricalLabel categoricalLabel = (CategoricalLabel)schema.getLabel();

	MatrixUtil.checkRows(categoricalLabel.size(), coefficients);

	List<RegressionTable> regressionTables = new ArrayList<>();

	for(int i = 0; i < categoricalLabel.size(); i++){
		Object targetCategory = categoricalLabel.getValue(i);

		List<Feature> features = new ArrayList<>(schema.getFeatures());
		List<Double> featureCoefficients = new ArrayList<>(MatrixUtil.getRow(coefficients, i));

		RegressionTableUtil.simplify(converter, targetCategory, features, featureCoefficients);

		double intercept = intercepts.apply(i);

		RegressionTable regressionTable = RegressionModelUtil.createRegressionTable(features, featureCoefficients, intercept)
			.setTargetCategory(targetCategory);

		regressionTables.add(regressionTable);
	}

	RegressionModel regressionModel = new RegressionModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), regressionTables)
		.setNormalizationMethod(RegressionModel.NormalizationMethod.SOFTMAX);

	return regressionModel;
}
 
Example 7
Source File: LRMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 3 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	RGenericVector lrm = getObject();

	RDoubleVector coefficients = lrm.getDoubleElement("coefficients");

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

	SchemaUtil.checkSize(2, categoricalLabel);

	Object targetCategory = categoricalLabel.getValue(1);

	Double intercept = coefficients.getElement(getInterceptName(), false);

	List<? extends Feature> features = schema.getFeatures();

	SchemaUtil.checkSize(coefficients.size() - (intercept != null ? 1 : 0), features);

	List<Double> featureCoefficients = getFeatureCoefficients(features, coefficients);

	GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERALIZED_LINEAR, MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), null, null, null)
		.setLinkFunction(GeneralRegressionModel.LinkFunction.LOGIT)
		.setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));

	GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, features, featureCoefficients, intercept, targetCategory);

	return generalRegressionModel;
}
 
Example 8
Source File: ClassificationModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 2 votes vote down vote up
@Override
public List<OutputField> registerOutputFields(Label label, Model pmmlModel, SparkMLEncoder encoder){
	T model = getTransformer();

	CategoricalLabel categoricalLabel = (CategoricalLabel)label;

	List<Integer> categories = LabelUtil.createTargetCategories(categoricalLabel.size());

	String predictionCol = model.getPredictionCol();

	Boolean keepPredictionCol = (Boolean)getOption(HasPredictionModelOptions.OPTION_KEEP_PREDICTIONCOL, Boolean.TRUE);

	OutputField pmmlPredictedOutputField = ModelUtil.createPredictedField(FieldName.create("pmml(" + predictionCol + ")"), OpType.CATEGORICAL, categoricalLabel.getDataType())
		.setFinalResult(false);

	DerivedOutputField pmmlPredictedField = encoder.createDerivedField(pmmlModel, pmmlPredictedOutputField, keepPredictionCol);

	MapValues mapValues = PMMLUtil.createMapValues(pmmlPredictedField.getName(), categoricalLabel.getValues(), categories)
		.setDataType(DataType.DOUBLE);

	OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CONTINUOUS, DataType.DOUBLE)
		.setResultFeature(ResultFeature.TRANSFORMED_VALUE)
		.setExpression(mapValues);

	DerivedOutputField predictedField = encoder.createDerivedField(pmmlModel, predictedOutputField, keepPredictionCol);

	encoder.putOnlyFeature(predictionCol, new IndexFeature(encoder, predictedField, categories));

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

	if(model instanceof HasProbabilityCol){
		HasProbabilityCol hasProbabilityCol = (HasProbabilityCol)model;

		String probabilityCol = hasProbabilityCol.getProbabilityCol();

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

		for(int i = 0; i < categoricalLabel.size(); i++){
			Object value = categoricalLabel.getValue(i);

			OutputField probabilityField = ModelUtil.createProbabilityField(FieldName.create(probabilityCol + "(" + value + ")"), DataType.DOUBLE, value);

			result.add(probabilityField);

			features.add(new ContinuousFeature(encoder, probabilityField));
		}

		// XXX
		encoder.putFeatures(probabilityCol, features);
	}

	return result;
}