Home

Awesome

JPMML-SparkML Build Status

Java library and command-line application for converting Apache Spark ML pipelines to PMML.

Table of Contents

Features

Overview

Supported libraries

<details> <summary>Apache Spark ML</summary>

Examples: main.py

</details> <details> <summary>JPMML-SparkML</summary> </details> <details> <summary>LightGBM</summary>

Examples: LightGBMAuditNA.scala, LightGBMAutoNA.scaka, etc.

</details> <details> <summary>XGBoost</summary>

Examples: XGBoostAuditNA.scala, XGBoostAutoNA.scala, etc.

</details>

Prerequisites

Installation

Library

JPMML-SparkML library JAR file (together with accompanying Java source and Javadocs JAR files) is released via Maven Central Repository.

The current version is 2.5.1 (20 June, 2024).

<dependency>
	<groupId>org.jpmml</groupId>
	<artifactId>pmml-sparkml</artifactId>
	<version>2.5.1</version>
</dependency>

Compatibility matrix

Active development branches:

Apache Spark versionJPMML-SparkML branch
3.0.X2.0.X
3.1.X2.1.X
3.2.X2.2.X
3.3.X2.3.X
3.4.X2.4.X
3.5.Xmaster

Archived development branches:

Apache Spark versionJPMML-SparkML branch
1.5.X and 1.6.X1.0.X
2.0.X1.1.X
2.1.X1.2.X
2.2.X1.3.X
2.3.X1.4.X
2.4.X1.5.X
3.0.X1.6.X
3.1.X1.7.X
3.2.X1.8.X

Example application

Enter the project root directory and build using Apache Maven:

mvn clean install

The build produces two JAR files:

Usage

Library

Fitting a Spark ML pipeline:

import org.apache.spark.ml.Pipeline
import org.apache.spark.ml.classification.DecisionTreeClassifier
import org.apache.spark.ml.feature.RFormula

val irisData = spark.read.format("csv").option("header", "true").option("inferSchema", "true").load("Iris.csv")
val irisSchema = irisData.schema

val rFormula = new RFormula().setFormula("Species ~ .")
val dtClassifier = new DecisionTreeClassifier().setLabelCol(rFormula.getLabelCol).setFeaturesCol(rFormula.getFeaturesCol)
val pipeline = new Pipeline().setStages(Array(rFormula, dtClassifier))

val pipelineModel = pipeline.fit(irisData)

Converting the fitted Spark ML pipeline to an in-memory PMML class model object:

import org.jpmml.sparkml.PMMLBuilder

val pmml = new PMMLBuilder(irisSchema, pipelineModel).build()

The representation of individual Spark ML pipeline stages can be customized via conversion options:

import org.jpmml.sparkml.PMMLBuilder
import org.jpmml.sparkml.model.HasTreeOptions

val dtClassifierModel = pipelineModel.stages(1)

val pmml = new PMMLBuilder(irisSchema, pipelineModel).putOption(dtClassifierModel, HasTreeOptions.OPTION_COMPACT, false).putOption(dtClassifierModel, HasTreeOptions.OPTION_ESTIMATE_FEATURE_IMPORTANCES, true).build()

Viewing the in-memory PMML class model object:

import javax.xml.transform.stream.StreamResult
import org.jpmml.model.JAXBUtil

JAXBUtil.marshalPMML(pmml, new StreamResult(System.out))

Example application

The example application JAR file contains an executable class org.jpmml.sparkml.example.Main, which can be used to convert a pair of serialized org.apache.spark.sql.types.StructType and org.apache.spark.ml.PipelineModel objects to PMML.

The example application JAR file does not include Apache Spark runtime libraries. Therefore, this executable class must be executed using Apache Spark's spark-submit helper script.

For example, converting a pair of Spark ML schema and pipeline serialization files pmml-sparkml/src/test/resources/schema/Iris.json and pmml-sparkml/src/test/resources/pipeline/DecisionTreeIris.zip, respectively, to a PMML file DecisionTreeIris.pmml:

spark-submit --master local --class org.jpmml.sparkml.example.Main pmml-sparkml-example/target/pmml-sparkml-example-executable-2.5-SNAPSHOT.jar --schema-input pmml-sparkml/src/test/resources/schema/Iris.json --pipeline-input pmml-sparkml/src/test/resources/pipeline/DecisionTreeIris.zip --pmml-output DecisionTreeIris.pmml

Getting help:

spark-submit --master local --class org.jpmml.sparkml.example.Main pmml-sparkml-example/target/pmml-sparkml-example-executable-2.5-SNAPSHOT.jar --help

Documentation

License

JPMML-SparkML is licensed under the terms and conditions of the GNU Affero General Public License, Version 3.0.

If you would like to use JPMML-SparkML in a proprietary software project, then it is possible to enter into a licensing agreement which makes JPMML-SparkML available under the terms and conditions of the BSD 3-Clause License instead.

Additional information

JPMML-SparkML is developed and maintained by Openscoring Ltd, Estonia.

Interested in using Java PMML API software in your company? Please contact info@openscoring.io