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

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

spark.mllib は、予測的モデルマークアップ言語(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 がスローされます。