PMML モデル抽出 - RDDベースのAPI

spark.mllib でサポートされるモデル

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

以下のテーブルはPMMLへエクスポートすることができるspark.mllibのモデルと、それらに等価なPMMLモデルの概略です。

spark.mllib modelPMML モデル
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 が例外を投げるでしょう。

TOP
inserted by FC2 system