org.jpmml.converter.Label Java Examples

The following examples show how to use org.jpmml.converter.Label. 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: Classification.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Label encodeLabel(FieldName targetField, List<?> targetCategories, PMMLEncoder encoder){
	DataField dataField;

	if(targetCategories == null){
		targetCategories = LabelUtil.createTargetCategories(this.num_class_);

		dataField = encoder.createDataField(targetField, OpType.CATEGORICAL, DataType.INTEGER, targetCategories);
	} else

	{
		if(targetCategories.size() != this.num_class_){
			throw new IllegalArgumentException("Expected " + this.num_class_ + " target categories, got " + targetCategories.size() + " target categories");
		}

		dataField = encoder.createDataField(targetField, OpType.CATEGORICAL, DataType.STRING, targetCategories);
	}

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

	List<OutputField> result = super.registerOutputFields(label, pmmlModel, encoder);

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

			result = new ArrayList<>(result);
			result.addAll(ModelUtil.createProbabilityFields(DataType.DOUBLE, categoricalLabel.getValues()));
			break;
		default:
			break;
	}

	return result;
}
 
Example #3
Source File: Classification.java    From jpmml-xgboost with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Label encodeLabel(FieldName targetField, List<?> targetCategories, PMMLEncoder encoder){
	DataField dataField;

	if(targetCategories == null){
		targetCategories = LabelUtil.createTargetCategories(this.num_class);

		dataField = encoder.createDataField(targetField, OpType.CATEGORICAL, DataType.INTEGER, targetCategories);
	} else

	{
		if(targetCategories.size() != this.num_class){
			throw new IllegalArgumentException("Expected " + this.num_class + " target categories, got " + targetCategories.size() + " target categories");
		}

		dataField = encoder.createDataField(targetField, OpType.CATEGORICAL, DataType.STRING, targetCategories);
	}

	return new CategoricalLabel(dataField);
}
 
Example #4
Source File: ModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private void checkSchema(Schema schema){
	Label label = schema.getLabel();
	List<? extends Feature> features = schema.getFeatures();

	if(label == null){
		return;
	}

	for(Feature feature : features){

		if(Objects.equals(label.getName(), feature.getName())){
			throw new IllegalArgumentException("Label column '" + label.getName() + "' is contained in the list of feature columns");
		}
	}
}
 
Example #5
Source File: RecipeEncoder.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Schema createSchema(){
	RGenericVector recipe = getObject();

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

	RGenericVector steps = recipe.getGenericElement("steps");

	List<FieldName> outcomeNames = this.termRoles.entrySet().stream()
		.filter(entry -> (Role.OUTCOME).equals(entry.getValue()))
		.map(entry -> entry.getKey())
		.collect(Collectors.toList());

	if(outcomeNames.size() == 1){
		FieldName outcomeName = outcomeNames.get(0);

		renameDataField(label.getName(), outcomeName);

		label = label.toRenamedLabel(outcomeName);
	} else

	if(outcomeNames.size() >= 2){
		throw new IllegalArgumentException();
	} // End if

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

	return new Schema(label, features);
}
 
Example #6
Source File: GLMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void encodeSchema(RExpEncoder encoder){
	RGenericVector glm = getObject();

	RGenericVector family = glm.getGenericElement("family");
	RGenericVector model = glm.getGenericElement("model", false);

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

	super.encodeSchema(encoder);

	MiningFunction miningFunction = getMiningFunction(familyFamily.asScalar());
	switch(miningFunction){
		case CLASSIFICATION:
			Label label = encoder.getLabel();

			if(model != null){
				RIntegerVector variable = model.getFactorElement((label.getName()).getValue());

				DataField dataField = (DataField)encoder.toCategorical(label.getName(), RExpUtil.getFactorLevels(variable));

				encoder.setLabel(dataField);
			}
			break;
		default:
			break;
	}
}
 
Example #7
Source File: RuleSetClassifier.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public RuleSetModel encodeModel(Schema schema){
	String defaultScore = getDefaultScore();
	List<Object[]> rules = getRules();

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

	RuleSelectionMethod ruleSelectionMethod = new RuleSelectionMethod(RuleSelectionMethod.Criterion.FIRST_HIT);

	RuleSet ruleSet = new RuleSet()
		.addRuleSelectionMethods(ruleSelectionMethod);

	if(defaultScore != null){
		ruleSet
			.setDefaultConfidence(1d)
			.setDefaultScore(defaultScore);
	}

	Scope scope = new DataFrameScope(FieldName.create("X"), features);

	for(Object[] rule : rules){
		String predicate = TupleUtil.extractElement(rule, 0, String.class);
		String score = TupleUtil.extractElement(rule, 1, String.class);

		Predicate pmmlPredicate = PredicateTranslator.translate(predicate, scope);

		SimpleRule simpleRule = new SimpleRule(score, pmmlPredicate);

		ruleSet.addRules(simpleRule);
	}

	RuleSetModel ruleSetModel = new RuleSetModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(label), ruleSet);

	return ruleSetModel;
}
 
Example #8
Source File: SelectFirstUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private MiningModel encodeModel(MiningFunction miningFunction, List<Object[]> steps, Schema schema){

	if(steps.size() < 1){
		throw new IllegalArgumentException();
	}

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

	Segmentation segmentation = new Segmentation(Segmentation.MultipleModelMethod.SELECT_FIRST, null);

	Scope scope = new DataFrameScope(FieldName.create("X"), features);

	for(Object[] step : steps){
		String name = TupleUtil.extractElement(step, 0, String.class);
		Estimator estimator = TupleUtil.extractElement(step, 1, Estimator.class);
		String predicate = TupleUtil.extractElement(step, 2, String.class);

		if(!(miningFunction).equals(estimator.getMiningFunction())){
			throw new IllegalArgumentException();
		}

		Predicate pmmlPredicate = PredicateTranslator.translate(predicate, scope);

		Model model = estimator.encodeModel(schema);

		Segment segment = new Segment(pmmlPredicate, model)
			.setId(name);

		segmentation.addSegments(segment);
	}

	MiningModel miningModel = new MiningModel(miningFunction, ModelUtil.createMiningSchema(label))
		.setSegmentation(segmentation);

	return miningModel;
}
 
Example #9
Source File: Regression.java    From jpmml-xgboost with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Label encodeLabel(FieldName targetField, List<?> targetCategories, PMMLEncoder encoder){

	if(targetCategories != null){
		throw new IllegalArgumentException("Regression requires zero target categories");
	}

	DataField dataField = encoder.createDataField(targetField, OpType.CONTINUOUS, DataType.FLOAT);

	return new ContinuousLabel(dataField);
}
 
Example #10
Source File: Regression.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Label encodeLabel(FieldName targetField, List<?> targetCategories, PMMLEncoder encoder){

	if(targetCategories != null && targetCategories.size() > 0){
		throw new IllegalArgumentException("Regression requires zero target categories");
	}

	DataField dataField = encoder.createDataField(targetField, OpType.CONTINUOUS, DataType.DOUBLE);

	return new ContinuousLabel(dataField);
}
 
Example #11
Source File: LinearRegressor.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public RegressionModel encodeModel(TensorFlowEncoder encoder){
	DataField dataField = encoder.createDataField(FieldName.create("_target"), OpType.CONTINUOUS, DataType.FLOAT);

	Label label = new ContinuousLabel(dataField);

	RegressionModel regressionModel = encodeRegressionModel(encoder)
		.setMiningFunction(MiningFunction.REGRESSION)
		.setMiningSchema(ModelUtil.createMiningSchema(label));

	return regressionModel;
}
 
Example #12
Source File: ClusteringModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<OutputField> registerOutputFields(Label label, org.dmg.pmml.Model pmmlModel, SparkMLEncoder encoder){
	T model = getTransformer();

	List<Integer> clusters = LabelUtil.createTargetCategories(getNumberOfClusters());

	String predictionCol = model.getPredictionCol();

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

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

	OutputField predictedOutputField = new OutputField(FieldName.create(predictionCol), OpType.CATEGORICAL, DataType.INTEGER)
		.setResultFeature(ResultFeature.TRANSFORMED_VALUE)
		.setExpression(new FieldRef(pmmlPredictedField.getName()));

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

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

	return Collections.emptyList();
}
 
Example #13
Source File: ObjectiveFunction.java    From jpmml-lightgbm with GNU Affero General Public License v3.0 4 votes vote down vote up
abstract
public Label encodeLabel(FieldName targetField, List<?> targetCategories, PMMLEncoder encoder);
 
Example #14
Source File: LRMConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void encodeSchema(RExpEncoder encoder){
	RGenericVector lrm = getObject();

	RIntegerVector freq = lrm.getIntegerElement("freq");

	RStringVector freqNames = freq.dimnames(0);

	super.encodeSchema(encoder);

	Label label = encoder.getLabel();

	DataField dataField = (DataField)encoder.toCategorical(label.getName(), freqNames.getValues());

	encoder.setLabel(dataField);
}
 
Example #15
Source File: XGBoostConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void encodeSchema(RExpEncoder encoder){
	RGenericVector booster = getObject();

	RStringVector featureNames = booster.getStringElement("feature_names", false);
	RGenericVector schema = booster.getGenericElement("schema", false);

	FeatureMap featureMap = ensureFeatureMap();

	if(featureNames != null){
		checkFeatureMap(featureMap, featureNames);
	} // End if

	if(schema != null){
		RVector<?> missing = schema.getVectorElement("missing", false);

		if(missing != null){
			featureMap.addMissingValue(ValueUtil.asString(missing.asScalar()));
		}
	}

	Learner learner = ensureLearner();

	ObjFunction obj = learner.obj();

	FieldName targetField = FieldName.create("_target");
	List<String> targetCategories = null;

	if(schema != null){
		RStringVector responseName = schema.getStringElement("response_name", false);
		RStringVector responseLevels = schema.getStringElement("response_levels", false);

		if(responseName != null){
			targetField = FieldName.create(responseName.asScalar());
		} // End if

		if(responseLevels != null){
			targetCategories = responseLevels.getValues();
		}
	}

	Label label = obj.encodeLabel(targetField, targetCategories, encoder);

	encoder.setLabel(label);

	List<Feature> features = featureMap.encodeFeatures(encoder);
	for(Feature feature : features){
		encoder.addFeature(feature);
	}
}
 
Example #16
Source File: ModelConverter.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
protected Map<VerificationField, List<?>> encodeTargetValues(RGenericVector dataFrame, Label label){
	List<RExp> columns = dataFrame.getValues();
	FieldName name = label.getName();

	return encodeVerificationData(columns, Collections.singletonList(name));
}
 
Example #17
Source File: RExpEncoder.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
public void setLabel(Label label){
	this.label = label;
}
 
Example #18
Source File: RExpEncoder.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
public Label getLabel(){
	return this.label;
}
 
Example #19
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 #20
Source File: ModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
public org.dmg.pmml.Model registerModel(SparkMLEncoder encoder){
	Schema schema = encodeSchema(encoder);

	Label label = schema.getLabel();

	org.dmg.pmml.Model model = encodeModel(schema);

	List<OutputField> sparkOutputFields = registerOutputFields(label, model, encoder);
	if(sparkOutputFields != null && sparkOutputFields.size() > 0){
		org.dmg.pmml.Model finalModel = MiningModelUtil.getFinalModel(model);

		Output output = ModelUtil.ensureOutput(finalModel);

		List<OutputField> outputFields = output.getOutputFields();

		outputFields.addAll(sparkOutputFields);
	}

	return model;
}
 
Example #21
Source File: MultilayerPerceptronUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public NeuralNetwork encodeNeuralNetwork(MiningFunction miningFunction, String activation, List<? extends HasArray> coefs, List<? extends HasArray> intercepts, Schema schema){
	NeuralNetwork.ActivationFunction activationFunction = parseActivationFunction(activation);

	ClassDictUtil.checkSize(coefs, intercepts);

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

	NeuralInputs neuralInputs = NeuralNetworkUtil.createNeuralInputs(features, DataType.DOUBLE);

	List<? extends NeuralEntity> entities = neuralInputs.getNeuralInputs();

	List<NeuralLayer> neuralLayers = new ArrayList<>();

	for(int layer = 0; layer < coefs.size(); layer++){
		HasArray coef = coefs.get(layer);
		HasArray intercept = intercepts.get(layer);

		int[] shape = coef.getArrayShape();

		int rows = shape[0];
		int columns = shape[1];

		NeuralLayer neuralLayer = new NeuralLayer();

		List<?> coefMatrix = coef.getArrayContent();
		List<?> interceptVector = intercept.getArrayContent();

		for(int column = 0; column < columns; column++){
			List<? extends Number> weights = (List)CMatrixUtil.getColumn(coefMatrix, rows, columns, column);
			Number bias = (Number)interceptVector.get(column);

			Neuron neuron = NeuralNetworkUtil.createNeuron(entities, weights, bias)
				.setId(String.valueOf(layer + 1) + "/" + String.valueOf(column + 1));

			neuralLayer.addNeurons(neuron);
		}

		neuralLayers.add(neuralLayer);

		entities = neuralLayer.getNeurons();

		if(layer == (coefs.size() - 1)){
			neuralLayer.setActivationFunction(NeuralNetwork.ActivationFunction.IDENTITY);

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

					// Binary classification
					if(categoricalLabel.size() == 2){
						List<NeuralLayer> transformationNeuralLayers = NeuralNetworkUtil.createBinaryLogisticTransformation(Iterables.getOnlyElement(entities));

						neuralLayers.addAll(transformationNeuralLayers);

						neuralLayer = Iterables.getLast(transformationNeuralLayers);

						entities = neuralLayer.getNeurons();
					} else

					// Multi-class classification
					if(categoricalLabel.size() > 2){
						neuralLayer.setNormalizationMethod(NeuralNetwork.NormalizationMethod.SOFTMAX);
					} else

					{
						throw new IllegalArgumentException();
					}
					break;
				default:
					break;
			}
		}
	}

	NeuralOutputs neuralOutputs = null;

	switch(miningFunction){
		case REGRESSION:
			neuralOutputs = NeuralNetworkUtil.createRegressionNeuralOutputs(entities, (ContinuousLabel)label);
			break;
		case CLASSIFICATION:
			neuralOutputs = NeuralNetworkUtil.createClassificationNeuralOutputs(entities, (CategoricalLabel)label);
			break;
		default:
			break;
	}

	NeuralNetwork neuralNetwork = new NeuralNetwork(miningFunction, activationFunction, ModelUtil.createMiningSchema(label), neuralInputs, neuralLayers)
		.setNeuralOutputs(neuralOutputs);

	return neuralNetwork;
}
 
Example #22
Source File: StackingUtil.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public <E extends Estimator> MiningModel encodeStacking(List<? extends E> estimators, List<String> stackMethods, PredictFunction predictFunction, E finalEstimator, boolean passthrough, Schema schema){
	ClassDictUtil.checkSize(estimators, stackMethods);

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

	SkLearnEncoder encoder = (SkLearnEncoder)getEncoder(features);

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

	List<Model> models = new ArrayList<>();

	for(int i = 0; i < estimators.size(); i++){
		E estimator = estimators.get(i);
		String stackMethod = stackMethods.get(i);

		Model model = estimator.encodeModel(schema);

		List<Feature> predictFeatures = predictFunction.apply(i, model, stackMethod, encoder);
		if(predictFeatures != null && predictFeatures.size() > 0){
			stackFeatures.addAll(predictFeatures);
		}

		models.add(model);
	}

	if(passthrough){
		stackFeatures.addAll(features);
	}

	{
		Schema stackSchema = new Schema(label, stackFeatures);

		Model finalModel = finalEstimator.encodeModel(stackSchema);

		models.add(finalModel);
	}

	return MiningModelUtil.createModelChain(models);
}
 
Example #23
Source File: ObjFunction.java    From jpmml-xgboost with GNU Affero General Public License v3.0 4 votes vote down vote up
abstract
public Label encodeLabel(FieldName targetField, List<?> targetCategories, PMMLEncoder encoder);
 
Example #24
Source File: ModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
public List<OutputField> registerOutputFields(Label label, org.dmg.pmml.Model model, SparkMLEncoder encoder){
	return null;
}
 
Example #25
Source File: RegressionModelConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<OutputField> registerOutputFields(Label label, Model pmmlModel, SparkMLEncoder encoder){
	T model = getTransformer();

	String predictionCol = model.getPredictionCol();

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

	OutputField predictedOutputField = ModelUtil.createPredictedField(FieldName.create(predictionCol), OpType.CONTINUOUS, label.getDataType());

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

	encoder.putOnlyFeature(predictionCol, new ContinuousFeature(encoder, predictedField));

	return Collections.emptyList();
}
 
Example #26
Source File: Learner.java    From jpmml-xgboost with GNU Affero General Public License v3.0 3 votes vote down vote up
public Schema encodeSchema(FieldName targetField, List<String> targetCategories, FeatureMap featureMap, XGBoostEncoder encoder){

		if(targetField == null){
			targetField = FieldName.create("_target");
		}

		Label label = this.obj.encodeLabel(targetField, targetCategories, encoder);

		List<Feature> features = featureMap.encodeFeatures(encoder);

		return new Schema(label, features);
	}
 
Example #27
Source File: BaseEstimator.java    From jpmml-sklearn with GNU Affero General Public License v3.0 2 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	MojoModel mojoModel = getMojoModel();

	Converter<?> converter;

	try {
		ConverterFactory converterFactory = ConverterFactory.newConverterFactory();

		converter = converterFactory.newConverter(mojoModel);
	} catch(Exception e){
		throw new IllegalArgumentException(e);
	}

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

	H2OEncoder encoder = new H2OEncoder();

	Schema h2oSchema = converter.encodeSchema(encoder);

	Label h2oLabel = h2oSchema.getLabel();
	List<? extends Feature> h2oFeatures = h2oSchema.getFeatures();

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

	for(Feature h2oFeature : h2oFeatures){
		FieldName name = h2oFeature.getName();

		Feature feature;

		if(features instanceof FeatureList){
			FeatureList namedFeatures = (FeatureList)features;

			feature = namedFeatures.getFeature(name.getValue());
		} else

		{
			int index = Integer.parseInt((name.getValue()).substring(1)) - 1;

			feature = features.get(index);
		}

		sortedFeatures.add(feature);
	}

	Schema mojoModelSchema = converter.toMojoModelSchema(new Schema(label, sortedFeatures));

	return converter.encodeModel(mojoModelSchema);
}
 
Example #28
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;
}