PMML モデル抽出 - RDDベースのAPI
spark.mllib
はモデルをサポートします
spark.mllib
はPredictive Model Markup Language (PMML)へのモデルのエクスポートをサポートします。
以下のテーブルはPMMLへエクスポートすることができるspark.mllib
のモデルと、それらに等価なPMMLモデルの概略です。
`spark.mllib` モデル | PMML モデル |
---|---|
KMeansModel | ClusteringModel |
LinearRegressionModel | RegressionModel (functionName="regression") |
RidgeRegressionModel | RegressionModel (functionName="regression") |
LassoModel | RegressionModel (functionName="regression") |
SVMModel | RegressionModel (functionName="classification" normalizationMethod="none") |
Binary LogisticRegressionModel | RegressionModel (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("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
が例外を投げるでしょう。