org.dmg.pmml.Model Java Examples

The following examples show how to use org.dmg.pmml.Model. 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: AdaConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	RGenericVector ada = getObject();

	RGenericVector model = ada.getGenericElement("model");

	RGenericVector trees = model.getGenericElement("trees");
	RDoubleVector alpha = model.getDoubleElement("alpha");

	List<TreeModel> treeModels = encodeTreeModels(trees);

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(null))
		.setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.WEIGHTED_SUM, treeModels, alpha.getValues()))
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("adaValue"), OpType.CONTINUOUS, DataType.DOUBLE));

	return MiningModelUtil.createBinaryLogisticClassification(miningModel, 2d, 0d, RegressionModel.NormalizationMethod.LOGIT, true, schema);
}
 
Example #2
Source File: LogisticRegression.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	String multiClass = getMultiClass();

	if(("auto").equals(multiClass)){
		throw new IllegalArgumentException("Attribute \'" + ClassDictUtil.formatMember(this, "multi_class") + "\' must be explicitly set to the 'ovr' or 'multinomial' value");
	} else

	if(("multinomial").equals(multiClass)){
		return encodeMultinomialModel(schema);
	} else

	if(("ovr").equals(multiClass)){
		return encodeOvRModel(schema);
	} else

	{
		throw new IllegalArgumentException(multiClass);
	}
}
 
Example #3
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 #4
Source File: GLMNetConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	RGenericVector glmnet = getObject();

	RDoubleVector a0 = glmnet.getDoubleElement("a0");
	RExp beta = glmnet.getElement("beta");
	RDoubleVector lambda = glmnet.getDoubleElement("lambda");

	Double lambdaS = getLambdaS();
	if(lambdaS == null){
		lambdaS = loadLambdaS();
	}

	int column = (lambda.getValues()).indexOf(lambdaS);
	if(column < 0){
		throw new IllegalArgumentException();
	}

	return encodeModel(a0, beta, column, schema);
}
 
Example #5
Source File: AbstractParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public PMMLObject popParent(){
	PMMLObject parent = super.popParent();

	if(parent instanceof Model){
		this.dataTypes.clear();
	} else

	if(parent instanceof TransformationDictionary){
		this.dataTypes.clear();
	} else

	if(parent instanceof LocalTransformations){
		this.dataTypes.clear();
	}

	return parent;
}
 
Example #6
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void pushParent(PMMLObject parent){
	super.pushParent(parent);

	if(parent instanceof MiningModel){
		MiningModel miningModel = (MiningModel)parent;

		processMiningModel(miningModel);
	} else

	if(parent instanceof Model){
		Model model = (Model)parent;

		processModel(model);
	}
}
 
Example #7
Source File: BoostingConverter.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	RGenericVector boosting = getObject();

	RGenericVector trees = boosting.getGenericElement("trees");
	RDoubleVector weights = boosting.getDoubleElement("weights");

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

	List<TreeModel> treeModels = encodeTreeModels(trees);

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

	return miningModel;
}
 
Example #8
Source File: MiningModelUtil.java    From pyramid with Apache License 2.0 6 votes vote down vote up
static
public MiningModel createModelChain(List<? extends Model> models, Schema schema){

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

    Segmentation segmentation = createSegmentation(Segmentation.MultipleModelMethod.MODEL_CHAIN, models);

    Model lastModel = Iterables.getLast(models);

    MiningModel miningModel = new MiningModel(lastModel.getMiningFunction(), ModelUtil.createMiningSchema(schema.getLabel()))
            .setMathContext(ModelUtil.simplifyMathContext(lastModel.getMathContext()))
            .setSegmentation(segmentation);

    return miningModel;
}
 
Example #9
Source File: KMeansPMMLUtils.java    From oryx with Apache License 2.0 6 votes vote down vote up
/**
 * Validates that the encoded PMML model received matches expected schema.
 *
 * @param pmml {@link PMML} encoding of KMeans Clustering
 * @param schema expected schema attributes of KMeans Clustering
 */
public static void validatePMMLVsSchema(PMML pmml, InputSchema schema) {
  List<Model> models = pmml.getModels();
  Preconditions.checkArgument(models.size() == 1,
      "Should have exactly one model, but had %s", models.size());

  Model model = models.get(0);
  Preconditions.checkArgument(model instanceof ClusteringModel);
  Preconditions.checkArgument(model.getMiningFunction() == MiningFunction.CLUSTERING);

  DataDictionary dictionary = pmml.getDataDictionary();
  Preconditions.checkArgument(
      schema.getFeatureNames().equals(AppPMMLUtils.getFeatureNames(dictionary)),
      "Feature names in schema don't match names in PMML");

  MiningSchema miningSchema = model.getMiningSchema();
  Preconditions.checkArgument(schema.getFeatureNames().equals(
      AppPMMLUtils.getFeatureNames(miningSchema)));

}
 
Example #10
Source File: GBDTLRClassifier.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	Classifier gbdt = getGBDT();
	MultiOneHotEncoder ohe = getOHE();
	LinearClassifier lr = getLR();

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

	SchemaUtil.checkSize(2, categoricalLabel);

	List<? extends Number> coef = lr.getCoef();
	List<? extends Number> intercept = lr.getIntercept();

	Schema segmentSchema = schema.toAnonymousSchema();

	MiningModel miningModel = GBDTUtil.encodeModel(gbdt, ohe, coef, Iterables.getOnlyElement(intercept), segmentSchema)
		.setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE));

	return MiningModelUtil.createBinaryLogisticClassification(miningModel, 1d, 0d, RegressionModel.NormalizationMethod.LOGIT, lr.hasProbabilityDistribution(), schema);
}
 
Example #11
Source File: VotingRegressor.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	List<? extends Regressor> estimators = getEstimators();
	List<? extends Number> weights = getWeights();

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

	for(Regressor estimator : estimators){
		Model model = estimator.encodeModel(schema);

		models.add(model);
	}

	Segmentation.MultipleModelMethod multipleModelMethod = (weights != null && weights.size() > 0 ? Segmentation.MultipleModelMethod.WEIGHTED_AVERAGE : Segmentation.MultipleModelMethod.AVERAGE);

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()))
		.setSegmentation(MiningModelUtil.createSegmentation(multipleModelMethod, models, weights));

	return miningModel;
}
 
Example #12
Source File: ModelEvaluatorBuilder.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
protected void checkSchema(ModelEvaluator<?> modelEvaluator){
	Model model = modelEvaluator.getModel();

	MiningSchema miningSchema = model.getMiningSchema();

	List<InputField> inputFields = modelEvaluator.getInputFields();
	List<InputField> groupFields = Collections.emptyList();

	if(modelEvaluator instanceof HasGroupFields){
		HasGroupFields hasGroupFields = (HasGroupFields)modelEvaluator;

		groupFields = hasGroupFields.getGroupFields();
	} // End if

	if((inputFields.size() + groupFields.size()) > 1000){
		throw new InvalidElementException("Model has too many input fields", miningSchema);
	}

	List<TargetField> targetFields = modelEvaluator.getTargetFields();
	List<OutputField> outputFields = modelEvaluator.getOutputFields();

	if((targetFields.size() + outputFields.size()) < 1){
		throw new InvalidElementException("Model does not have any target or output fields", miningSchema);
	}
}
 
Example #13
Source File: AdaBoostRegressor.java    From jpmml-sklearn with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public MiningModel encodeModel(Schema schema){
	List<? extends Regressor> estimators = getEstimators();
	List<? extends Number> estimatorWeights = getEstimatorWeights();

	Schema segmentSchema = schema.toAnonymousSchema();

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

	for(Regressor estimator : estimators){
		Model model = estimator.encodeModel(segmentSchema);

		models.add(model);
	}

	MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()))
		.setSegmentation(MiningModelUtil.createSegmentation(MultipleModelMethod.WEIGHTED_MEDIAN, models, estimatorWeights));

	return miningModel;
}
 
Example #14
Source File: FieldResolver.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void declareLocalFields(Model model, boolean transformations){
	List<Field<?>> scope = this.scopes.get(model);

	if(scope != null){
		scope.clear();
	}

	LocalTransformations localTransformations = model.getLocalTransformations();
	if(transformations && (localTransformations != null && localTransformations.hasDerivedFields())){
		declareFields(model, localTransformations.getDerivedFields());
	}
}
 
Example #15
Source File: RDFPMMLUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * Validates that the encoded PMML model received matches expected schema.
 *
 * @param pmml {@link PMML} encoding of a decision forest
 * @param schema expected schema attributes of decision forest
 */
public static void validatePMMLVsSchema(PMML pmml, InputSchema schema) {
  List<Model> models = pmml.getModels();
  Preconditions.checkArgument(models.size() == 1,
                              "Should have exactly one model, but had %s", models.size());

  Model model = models.get(0);
  MiningFunction function = model.getMiningFunction();
  if (schema.isClassification()) {
    Preconditions.checkArgument(function == MiningFunction.CLASSIFICATION,
                                "Expected classification function type but got %s",
                                function);
  } else {
    Preconditions.checkArgument(function == MiningFunction.REGRESSION,
                                "Expected regression function type but got %s",
                                function);
  }

  DataDictionary dictionary = pmml.getDataDictionary();
  Preconditions.checkArgument(
      schema.getFeatureNames().equals(AppPMMLUtils.getFeatureNames(dictionary)),
      "Feature names in schema don't match names in PMML");

  MiningSchema miningSchema = model.getMiningSchema();
  Preconditions.checkArgument(schema.getFeatureNames().equals(
      AppPMMLUtils.getFeatureNames(miningSchema)));

  Integer pmmlIndex = AppPMMLUtils.findTargetIndex(miningSchema);
  if (schema.hasTarget()) {
    int schemaIndex = schema.getTargetFeatureIndex();
    Preconditions.checkArgument(
        pmmlIndex != null && schemaIndex == pmmlIndex,
        "Configured schema expects target at index %s, but PMML has target at index %s",
        schemaIndex, pmmlIndex);
  } else {
    Preconditions.checkArgument(pmmlIndex == null);
  }
}
 
Example #16
Source File: FieldResolver.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private List<Output> getEarlierOutputs(Segmentation segmentation, Segment targetSegment){
	List<Output> result = new ArrayList<>();

	Segmentation.MultipleModelMethod multipleModelMethod = segmentation.getMultipleModelMethod();
	switch(multipleModelMethod){
		case MODEL_CHAIN:
			break;
		default:
			return Collections.emptyList();
	}

	List<Segment> segments = segmentation.getSegments();
	for(Segment segment : segments){
		Model model = segment.getModel();

		if(targetSegment != null && (targetSegment).equals(segment)){
			break;
		}

		Output output = model.getOutput();
		if(output != null){
			result.add(output);
		}
	}

	return result;
}
 
Example #17
Source File: MiningModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private void checkMiningFunction(Model model, MiningFunction parentMiningFunction){
	MiningFunction miningFunction = model.getMiningFunction();

	if(miningFunction == null){
		throw new MissingAttributeException(MissingAttributeException.formatMessage(XPathUtil.formatElement(model.getClass()) + "@functionName"), model);
	} // End if

	if(!(miningFunction).equals(parentMiningFunction)){
		throw new InvalidAttributeException(InvalidAttributeException.formatMessage(XPathUtil.formatElement(model.getClass()) + "@functionName=" + miningFunction.value()), model);
	}
}
 
Example #18
Source File: PmmlProcessorConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
private Model selectModel(Message<?> input) {
	String modelName = properties.getModelName();
	if (modelName == null && properties.getModelNameExpression() == null) {
		return pmml.getModels().get(0);
	}
	else if (properties.getModelNameExpression() != null) {
		modelName = properties.getModelNameExpression().getValue(evaluationContext, input, String.class);
	}
	for (Model model : pmml.getModels()) {
		if (model.getModelName().equals(modelName)) {
			return model;
		}
	}
	throw new RuntimeException("Unable to use model named '" + modelName + "'");
}
 
Example #19
Source File: GBDTLMRegressor.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	Regressor gbdt = getGBDT();
	MultiOneHotEncoder ohe = getOHE();
	LinearRegressor lm = getLM();

	List<? extends Number> coef = lm.getCoef();
	List<? extends Number> intercept = lm.getIntercept();

	return GBDTUtil.encodeModel(gbdt, ohe, coef, Iterables.getOnlyElement(intercept), schema);
}
 
Example #20
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public PMMLObject popParent(){
	PMMLObject parent = super.popParent();

	if(parent instanceof Model){
		this.targetDataTypes.pop();

		this.dataType = null;
	}

	return parent;
}
 
Example #21
Source File: LoadingModelEvaluatorBuilder.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public LoadingModelEvaluatorBuilder load(InputStream is, String modelName) throws SAXException, JAXBException {
	Schema schema = getSchema();
	ValidationEventHandler validationEventHandler = getValidationEventHandler();
	List<? extends XMLFilter> filters = getFilters();
	boolean locatable = getLocatable();
	VisitorBattery visitors = getVisitors();

	Unmarshaller unmarshaller = JAXBUtil.createUnmarshaller();
	unmarshaller.setSchema(schema);
	unmarshaller.setEventHandler(validationEventHandler);

	if(filters == null){
		filters = Collections.singletonList(new ImportFilter());
	}

	Source source = SAXUtil.createFilteredSource(is, filters.toArray(new XMLFilter[filters.size()]));

	PMML pmml = (PMML)unmarshaller.unmarshal(source);

	Visitor locatorHandler = (locatable ? new LocatorTransformer() : new LocatorNullifier());

	locatorHandler.applyTo(pmml);

	if(visitors != null && !visitors.isEmpty()){
		visitors.applyTo(pmml);
	}

	Model model = PMMLUtil.findModel(pmml, modelName);

	setPMML(pmml);
	setModel(model);

	return this;
}
 
Example #22
Source File: ElNetConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Model encodeModel(RDoubleVector a0, RExp beta, int column, Schema schema){
	Double intercept = a0.getValue(column);
	List<Double> coefficients = getCoefficients((S4Object)beta, column);

	GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERAL_LINEAR, MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), null, null, null)
		.setDistribution(GeneralRegressionModel.Distribution.NORMAL);

	GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, schema.getFeatures(), coefficients, intercept, null);

	return generalRegressionModel;
}
 
Example #23
Source File: KMeansPMMLUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * @param pmml PMML representation of Clusters
 * @return List of {@link ClusterInfo}
 */
public static List<ClusterInfo> read(PMML pmml) {
  Model model = pmml.getModels().get(0);
  Preconditions.checkArgument(model instanceof ClusteringModel);
  ClusteringModel clusteringModel = (ClusteringModel) model;

  return clusteringModel.getClusters().stream().map(cluster ->
    new ClusterInfo(Integer.parseInt(cluster.getId()),
                    VectorMath.parseVector(
                        TextUtils.parseDelimited(cluster.getArray().getValue().toString(), ' ')),
                    cluster.getSize())
  ).collect(Collectors.toList());
}
 
Example #24
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private void processModel(Model model){
	MiningSchema miningSchema = model.getMiningSchema();
	if(miningSchema == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(model.getClass()) + "/" + XPathUtil.formatElement(MiningSchema.class)), model);
	}

	Map<FieldName, DataType> targetDataTypes = new LinkedHashMap<>();

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(MiningField miningField : miningFields){
			FieldName name = miningField.getName();
			if(name == null){
				throw new MissingAttributeException(miningField, PMMLAttributes.MININGFIELD_NAME);
			}

			MiningField.UsageType usageType = miningField.getUsageType();
			switch(usageType){
				case PREDICTED:
				case TARGET:
					DataType dataType = resolveTargetDataType(name);

					targetDataTypes.put(name, dataType);
					break;
				default:
					break;
			}
		}
	}

	this.targetDataTypes.push(targetDataTypes);

	this.dataType = getDataType();
}
 
Example #25
Source File: MiningModelUtil.java    From pyramid with Apache License 2.0 5 votes vote down vote up
@Override
public Feature apply(Model model){
    Output output = model.getOutput();

    if(output == null || !output.hasOutputFields()){
        throw new IllegalArgumentException();
    }

    OutputField outputField = Iterables.getLast(output.getOutputFields());

    return new ContinuousFeature(null, outputField.getName(), outputField.getDataType());
}
 
Example #26
Source File: LinearSVC.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Model encodeModel(Schema schema){
	String multiClass = getMultiClass();

	if(!("ovr").equals(multiClass)){
		throw new IllegalArgumentException(multiClass);
	}

	return super.encodeModel(schema);
}
 
Example #27
Source File: PMMLPipeline.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
private PMML encodePMML(Model model, String repr, SkLearnEncoder encoder){
	PMML pmml = encoder.encodePMML(model);

	if(repr != null){
		Extension extension = new Extension()
			.addContent(repr);

		MiningBuildTask miningBuildTask = new MiningBuildTask()
			.addExtensions(extension);

		pmml.setMiningBuildTask(miningBuildTask);
	}

	return pmml;
}
 
Example #28
Source File: MiningModelUtil.java    From pyramid with Apache License 2.0 5 votes vote down vote up
static
public Segmentation createSegmentation(Segmentation.MultipleModelMethod multipleModelMethod, List<? extends Model> models, List<? extends Number> weights){

    if((weights != null) && (models.size() != weights.size())){
        throw new IllegalArgumentException();
    }

    List<Segment> segments = new ArrayList<>();

    for(int i = 0; i < models.size(); i++){
        Model model = models.get(i);
        Number weight = (weights != null ? weights.get(i) : null);

        Segment segment = new Segment()
                .setId(String.valueOf(i + 1))
                .setPredicate(new True())
                .setModel(model);

        if(weight != null && !ValueUtil.isOne(weight)){
            segment.setWeight(ValueUtil.asDouble(weight));
        }

        segments.add(segment);
    }

    return new Segmentation(multipleModelMethod, segments);
}
 
Example #29
Source File: FishNetConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Model encodeModel(RDoubleVector a0, RExp beta, int column, Schema schema){
	Double intercept = a0.getValue(column);
	List<Double> coefficients = getCoefficients((S4Object)beta, column);

	GeneralRegressionModel generalRegressionModel = new GeneralRegressionModel(GeneralRegressionModel.ModelType.GENERAL_LINEAR, MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), null, null, null)
		.setDistribution(GeneralRegressionModel.Distribution.POISSON);

	GeneralRegressionModelUtil.encodeRegressionTable(generalRegressionModel, schema.getFeatures(), coefficients, intercept, null);

	return generalRegressionModel;
}
 
Example #30
Source File: LogNetConverter.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Model encodeModel(RDoubleVector a0, RExp beta, int column, Schema schema){
	Double intercept = a0.getValue(column);
	List<Double> coefficients = getCoefficients((S4Object)beta, column);

	return RegressionModelUtil.createBinaryLogisticClassification(schema.getFeatures(), coefficients, intercept, RegressionModel.NormalizationMethod.LOGIT, true, schema);
}