PMML モデルのエクスポート - RDD ベース API

spark.mllib がサポートするモデル

spark.mllib は、Predictive Model Markup Language (PMML) へのモデルエクスポートをサポートしています。

以下の表は、PMML にエクスポートできる spark.mllib モデルとその同等の PMML モデルの概要を示しています。

spark.mllib モデルPMML モデル
KMeansModelClusteringModel
LinearRegressionModelRegressionModel (functionName="regression")
RidgeRegressionModelRegressionModel (functionName="regression")
LassoModelRegressionModel (functionName="regression")
SVMModelRegressionModel (functionName="classification" normalizationMethod="none")
Binary LogisticRegressionModelRegressionModel (functionName="classification" normalizationMethod="logit")

サポートされている model (上記の表を参照) を PMML にエクスポートするには、単に model.toPMML を呼び出します。

PMML モデルを文字列にエクスポートする (model.toPMML、上記の例を参照) ことに加えて、PMML モデルを他の形式にエクスポートすることもできます。

API の詳細については、KMeans Scala ドキュメントおよびVectors Scala ドキュメントを参照してください。

以下は、KMeansModel を構築し、PMML 形式で出力する完全な例です。

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 = data.map(s => 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 to a String in PMML format
println(s"PMML Model:\n ${clusters.toPMML()}")

// Export the model to a local file in PMML format
clusters.toPMML("/tmp/kmeans.xml")

// Export the model to a directory on a distributed file system in PMML format
clusters.toPMML(sc, "/tmp/kmeans")

// Export the model to the OutputStream in PMML format
clusters.toPMML(System.out)
完全なサンプル コードは、Spark リポジトリの「examples/src/main/scala/org/apache/spark/examples/mllib/PMMLModelExportExample.scala」にあります。

サポートされていないモデルの場合、.toPMML メソッドが見つからないか、IllegalArgumentException がスローされます。