Java library and command-line application for converting Apache Spark ML pipelines to PMML.
- Supported Spark ML
PipelineStage
types:- Feature extractors, transformers and selectors:
feature.Binarizer
feature.Bucketizer
feature.ChiSqSelectorModel
(the result of fitting afeature.ChiSqSelector
)feature.ColumnPruner
feature.CountVectorizerModel
(the result of fitting afeature.CountVectorizer
)feature.IDFModel
(the result of fitting afeature.IDF
)feature.ImputerModel
(the result of fitting afeature.Imputer
)feature.IndexToString
feature.Interaction
feature.MaxAbsScalerModel
(the result of fitting afeature.MaxAbsScaler
)feature.MinMaxScalerModel
(the result of fitting afeature.MinMaxScaler
)feature.NGram
feature.OneHotEncoder
feature.PCAModel
(the result of fitting afeature.PCA
)feature.QuantileDiscretizer
feature.RegexTokenizer
feature.RFormulaModel
(the result of fitting afeature.RFormula
)feature.StandardScalerModel
(the result of fitting afeature.StandardScaler
)feature.StopWordsRemover
feature.StringIndexerModel
(the result of fitting afeature.StringIndexer
)feature.Tokenizer
feature.VectorAssembler
feature.VectorAttributeRewriter
feature.VectorIndexerModel
(the result of fitting afeature.VectorIndexer
)feature.VectorSlicer
- Prediction models:
classification.DecisionTreeClassificationModel
classification.GBTClassificationModel
classification.LogisticRegressionModel
classification.MultilayerPerceptronClassificationModel
classification.RandomForestClassificationModel
clustering.KMeansModel
regression.DecisionTreeRegressionModel
regression.GBTRegressionModel
regression.GeneralizedLinearRegressionModel
regression.LinearRegressionModel
regression.RandomForestRegressionModel
- Prediction model chains:
PipelineModel
- Referencing the prediction column (
HasPredictionCol#getPredictionCol()
) of earlier clustering, classification and regression models. - Referencing the predicted probabilities column (
HasProbabilityCol#getProbabilityCol()
) of earlier classification models.
- Hyperparameter selectors and tuners:
- Feature extractors, transformers and selectors:
- Production quality:
- Complete test coverage.
- Fully compliant with the JPMML-Evaluator library.
- Apache Spark version 1.5.X, 1.6.X, 2.0.X, 2.1.X or 2.2.X.
JPMML-SparkML library JAR file (together with accompanying Java source and Javadocs JAR files) is released via Maven Central Repository.
The current version is 1.3.3 (19 December, 2017).
<dependency>
<groupId>org.jpmml</groupId>
<artifactId>jpmml-sparkml</artifactId>
<version>1.3.3</version>
</dependency>
Compatibility matrix:
JPMML-SparkML version | Apache Spark version | PMML version |
---|---|---|
1.0.0 through 1.0.9 | 1.5.X and 1.6.X | 4.2 |
1.1.0 | 2.0.X | 4.2 |
1.1.1 through 1.1.15 | 2.0.X | 4.3 |
1.2.0 through 1.2.7 | 2.1.X | 4.3 |
1.3.0 through 1.3.3 | 2.2.X | 4.3 |
JPMML-SparkML depends on the latest and greatest version of the JPMML-Model library, which is in conflict with the legacy version that is part of the Apache Spark distribution.
This conflict is documented in SPARK-15526.
The embodiment of the legacy version of the JPMML-Model library:
$SPARK_HOME/jars/pmml-model-1.2.15.jar
$SPARK_HOME/jars/pmml-schema-1.2.15.jar
Removing these two JAR files will solve all conflicts for all applications forever.
Excluding the legacy version of the JPMML-Model library:
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_2.11</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.jpmml</groupId>
<artifactId>pmml-model</artifactId>
</exclusion>
</exclusions>
</dependency>
Using the Maven Shade Plugin to relocate all org.dmg.pmml.*
and org.jpmml.*
classes of the latest and greatest version of the JPMML-Model library to a different namespace (aka "shading"):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.dmg.pmml</pattern>
<shadedPattern>org.shaded.dmg.pmml</shadedPattern>
</relocation>
<relocation>
<pattern>org.jpmml</pattern>
<shadedPattern>org.shaded.jpmml</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
The downside of shading is that such relocated classes are incompatible with other JPMML APIs. For example, the ConverterUtil#toPMML(StructType, PipelineModel)
utility method would start returning org.shaded.dmg.pmml.PMML
object instances, which are not valid substitutes for org.dmg.pmml.PMML
object instances.
Enter the project root directory and build using Apache Maven:
mvn clean install
The build produces two JAR files:
target/jpmml-sparkml-1.3-SNAPSHOT.jar
- Library JAR file.target/converter-executable-1.3-SNAPSHOT.jar
- Example application JAR file.
Fitting a Spark ML pipeline that only makes use of supported Transformer types:
DataFrame irisData = ...;
StructType schema = irisData.schema();
RFormula formula = new RFormula()
.setFormula("Species ~ .");
DecisionTreeClassifier classifier = new DecisionTreeClassifier()
.setLabelCol(formula.getLabelCol())
.setFeaturesCol(formula.getFeaturesCol());
Pipeline pipeline = new Pipeline()
.setStages(new PipelineStage[]{formula, classifier});
PipelineModel pipelineModel = pipeline.fit(irisData);
Converting the Spark ML pipeline to PMML using the org.jpmml.sparkml.ConverterUtil#toPMML(StructType, PipelineModel)
utility method:
PMML pmml = ConverterUtil.toPMML(schema, pipelineModel);
// Viewing the result
JAXBUtil.marshalPMML(pmml, new StreamResult(System.out));
The example application JAR file contains an executable class org.jpmml.sparkml.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 src/test/resources/schema/Iris.json
and src/test/resources/pipeline/DecisionTreeIris.zip
, respectively, to a PMML file DecisionTreeIris.pmml
:
spark-submit --master local --properties-file src/etc/converter.properties --class org.jpmml.sparkml.Main target/converter-executable-1.3-SNAPSHOT.jar --schema-input src/test/resources/schema/Iris.json --pipeline-input src/test/resources/pipeline/DecisionTreeIris.zip --pmml-output DecisionTreeIris.pmml
Getting help:
spark-submit --master local --class org.jpmml.sparkml.Main target/converter-executable-1.3-SNAPSHOT.jar --help
JPMML-SparkML is licensed under the GNU Affero General Public License (AGPL) version 3.0. Other licenses are available on request.
Please contact info@openscoring.io