MLlib - PMML model export

MLlib supported models

MLlib supports model export to Predictive Model Markup Language (PMML).

The table below outlines the MLlib models that can be exported to PMML and their equivalent PMML model.

MLlib modelPMML model
LinearRegressionModelRegressionModel (functionName="regression")
RidgeRegressionModelRegressionModel (functionName="regression")
LassoModelRegressionModel (functionName="regression")
SVMModelRegressionModel (functionName="classification" normalizationMethod="none")
Binary LogisticRegressionModelRegressionModel (functionName="classification" normalizationMethod="logit")


To export a supported model (see table above) to PMML, simply call model.toPMML.

Here a complete example of building a KMeansModel and print it out in PMML format:

import org.apache.spark.mllib.clustering.KMeans
import org.apache.spark.mllib.linalg.Vectors

// Load and parse the data
val data = sc.textFile("data/mllib/kmeans_data.txt")
val parsedData = => Vectors.dense(s.split(' ').map(_.toDouble))).cache()

// Cluster the data into two classes using KMeans
val numClusters = 2
val numIterations = 20
val clusters = KMeans.train(parsedData, numClusters, numIterations)

// Export to PMML
println("PMML Model:\n" + clusters.toPMML)

As well as exporting the PMML model to a String (model.toPMML as in the example above), you can export the PMML model to other formats:

// Export the model to a String in PMML format

// Export the model to a local file in PMML format

// Export the model to a directory on a distributed file system in PMML format

// Export the model to the OutputStream in PMML format

For unsupported models, either you will not find a .toPMML method or an IllegalArgumentException will be thrown.