-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-1406] Mllib pmml model export #3062
Closed
selvinsource
wants to merge
48
commits into
apache:master
from
selvinsource:mllib_pmml_model_export_SPARK-1406
Closed
Changes from 46 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
a0e3679
export and pmml export traits
selvinsource 226e184
added javadoc and export model type in case there is a need to support
selvinsource 9bc494f
added scala suite tests
selvinsource 8e71b8d
kmeans pmml export implementation
selvinsource 1433b11
complete suite tests
selvinsource 8841439
adjust scala style in order to compile
selvinsource 07a29bf
Update LICENSE
selvinsource f75b988
Merge remote-tracking branch 'origin/master' into mllib_pmml_model_ex…
selvinsource cd6c07c
fixed scala style to run tests
selvinsource aba5ee1
fixed cluster export
selvinsource e1eb251
removed serialization part, this will be part of the ModelExporter
selvinsource 6357b98
set it to private
selvinsource c3ef9b8
set it to private
selvinsource 349a76b
new helper object to serialize the models to pmml format
selvinsource 834ca44
reordered the import accordingly to the guidelines
selvinsource a1b4dc3
updated imports
selvinsource df8a89e
added pmml version to pmml model
selvinsource ae8b993
updated some commented tests to use the new ModelExporter object
selvinsource e29dfb9
removed version, by default is set to 4.2 (latest from jpmml)
selvinsource 78515ec
[SPARK-1406] added pmml export for LinearRegressionModel,
selvinsource c67ce81
Merge remote-tracking branch 'upstream/master' into mllib_pmml_model_…
selvinsource 3ae8ae5
[SPARK-1406] Adjusted imported order according to the guidelines
selvinsource 1faf985
[SPARK-1406] Added target field to the regression model for completeness
selvinsource 19adf29
[SPARK-1406] Fixed scala style
selvinsource 82f2131
Merge remote-tracking branch 'upstream/master' into mllib_pmml_model_…
selvinsource da2ec11
[SPARK-1406] added linear SVM PMML export
selvinsource 03bc3a5
added logistic regression
selvinsource 8fe12bb
[SPARK-1406] Adjusted logistic regression export description and target
selvinsource d559ec5
Merge remote-tracking branch 'upstream/master' into mllib_pmml_model_…
selvinsource 7b33b4e
[SPARK-1406] Added a PMMLExportable interface
selvinsource f46c75c
[SPARK-1406] Added PMMLExportable to supported models
selvinsource 7a949d0
[SPARK-1406] Fixed scala style
selvinsource b25bbf7
[SPARK-1406] Added export of pmml to distributed file system using the
selvinsource b8823b0
Merge remote-tracking branch 'upstream/master' into
selvinsource e2ffae8
fixed scala style
selvinsource 1676e15
fixed scala issue
selvinsource 472d757
fix code style
mengxr e2313df
Merge pull request #1 from mengxr/SPARK-1406
selvinsource 3c22f79
more code style
mengxr a0a55f7
Merge pull request #2 from mengxr/SPARK-1406
selvinsource 66b7c12
[SPARK-1406] Updated pmml model lib to 1.1.15, latest Java 6 compatible
selvinsource dea98ca
[SPARK-1406] Exclude transitive dependency for pmml model
selvinsource 25dce33
[SPARK-1406] Update code to latest pmml model
selvinsource cfcb596
[SPARK-1406] Throw IllegalArgumentException when exporting a multinomial
selvinsource 7a5e0ec
[SPARK-1406] Binary classification for SVM and Logistic Regression
selvinsource 30165c4
[SPARK-1406] Fixed extreme cases for logit
selvinsource 085cf42
[SPARK-1406] Added Double Min and Max
selvinsource 852aac6
[SPARK-1406] Update JPMML version to 1.1.15 in LICENSE file
selvinsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
mllib/src/main/scala/org/apache/spark/mllib/pmml/PMMLExportable.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.mllib.pmml | ||
|
||
import java.io.{File, OutputStream, StringWriter} | ||
import javax.xml.transform.stream.StreamResult | ||
|
||
import org.jpmml.model.JAXBUtil | ||
|
||
import org.apache.spark.SparkContext | ||
import org.apache.spark.mllib.pmml.export.PMMLModelExportFactory | ||
|
||
/** | ||
* Export model to the PMML format | ||
* Predictive Model Markup Language (PMML) is an XML-based file format | ||
* developed by the Data Mining Group (www.dmg.org). | ||
*/ | ||
trait PMMLExportable { | ||
|
||
/** | ||
* Export the model to the stream result in PMML format | ||
*/ | ||
private def toPMML(streamResult: StreamResult): Unit = { | ||
val pmmlModelExport = PMMLModelExportFactory.createPMMLModelExport(this) | ||
JAXBUtil.marshalPMML(pmmlModelExport.getPmml, streamResult) | ||
} | ||
|
||
/** | ||
* Export the model to a local file in PMML format | ||
*/ | ||
def toPMML(localPath: String): Unit = { | ||
toPMML(new StreamResult(new File(localPath))) | ||
} | ||
|
||
/** | ||
* Export the model to a directory on a distributed file system in PMML format | ||
*/ | ||
def toPMML(sc: SparkContext, path: String): Unit = { | ||
val pmml = toPMML() | ||
sc.parallelize(Array(pmml), 1).saveAsTextFile(path) | ||
} | ||
|
||
/** | ||
* Export the model to the OutputStream in PMML format | ||
*/ | ||
def toPMML(outputStream: OutputStream): Unit = { | ||
toPMML(new StreamResult(outputStream)) | ||
} | ||
|
||
/** | ||
* Export the model to a String in PMML format | ||
*/ | ||
def toPMML(): String = { | ||
val writer = new StringWriter | ||
toPMML(new StreamResult(writer)) | ||
writer.toString | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
...c/main/scala/org/apache/spark/mllib/pmml/export/BinaryClassificationPMMLModelExport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.mllib.pmml.export | ||
|
||
import scala.{Array => SArray} | ||
|
||
import org.dmg.pmml._ | ||
|
||
import org.apache.spark.mllib.regression.GeneralizedLinearModel | ||
|
||
/** | ||
* PMML Model Export for GeneralizedLinearModel class with binary ClassificationModel | ||
*/ | ||
private[mllib] class BinaryClassificationPMMLModelExport( | ||
model : GeneralizedLinearModel, | ||
description : String, | ||
normalizationMethod : RegressionNormalizationMethodType, | ||
threshold: Double) | ||
extends PMMLModelExport { | ||
|
||
populateBinaryClassificationPMML() | ||
|
||
/** | ||
* Export the input LogisticRegressionModel or SVMModel to PMML format. | ||
*/ | ||
private def populateBinaryClassificationPMML(): Unit = { | ||
pmml.getHeader.setDescription(description) | ||
|
||
if (model.weights.size > 0) { | ||
val fields = new SArray[FieldName](model.weights.size) | ||
val dataDictionary = new DataDictionary | ||
val miningSchema = new MiningSchema | ||
val regressionTableYES = new RegressionTable(model.intercept).withTargetCategory("1") | ||
var interceptNO = threshold | ||
if (RegressionNormalizationMethodType.LOGIT == normalizationMethod) { | ||
if (threshold <= 0) | ||
interceptNO = -1000 | ||
else if (threshold >= 1) | ||
interceptNO = 1000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
else | ||
interceptNO = -math.log(1/threshold -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space around |
||
} | ||
val regressionTableNO = new RegressionTable(interceptNO).withTargetCategory("0") | ||
val regressionModel = new RegressionModel() | ||
.withFunctionName(MiningFunctionType.CLASSIFICATION) | ||
.withMiningSchema(miningSchema) | ||
.withModelName(description) | ||
.withNormalizationMethod(normalizationMethod) | ||
.withRegressionTables(regressionTableYES, regressionTableNO) | ||
|
||
for (i <- 0 until model.weights.size) { | ||
fields(i) = FieldName.create("field_" + i) | ||
dataDictionary.withDataFields(new DataField(fields(i), OpType.CONTINUOUS, DataType.DOUBLE)) | ||
miningSchema | ||
.withMiningFields(new MiningField(fields(i)) | ||
.withUsageType(FieldUsageType.ACTIVE)) | ||
regressionTableYES.withNumericPredictors(new NumericPredictor(fields(i), model.weights(i))) | ||
} | ||
|
||
// add target field | ||
val targetField = FieldName.create("target") | ||
dataDictionary | ||
.withDataFields(new DataField(targetField, OpType.CATEGORICAL, DataType.STRING)) | ||
miningSchema | ||
.withMiningFields(new MiningField(targetField) | ||
.withUsageType(FieldUsageType.TARGET)) | ||
|
||
dataDictionary.withNumberOfFields(dataDictionary.getDataFields.size) | ||
|
||
pmml.setDataDictionary(dataDictionary) | ||
pmml.withModels(regressionModel) | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
.../src/main/scala/org/apache/spark/mllib/pmml/export/GeneralizedLinearPMMLModelExport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.mllib.pmml.export | ||
|
||
import scala.{Array => SArray} | ||
|
||
import org.dmg.pmml._ | ||
|
||
import org.apache.spark.mllib.regression.GeneralizedLinearModel | ||
|
||
/** | ||
* PMML Model Export for GeneralizedLinearModel abstract class | ||
*/ | ||
private[mllib] class GeneralizedLinearPMMLModelExport( | ||
model: GeneralizedLinearModel, | ||
description: String) | ||
extends PMMLModelExport { | ||
|
||
populateGeneralizedLinearPMML(model) | ||
|
||
/** | ||
* Export the input GeneralizedLinearModel model to PMML format. | ||
*/ | ||
private def populateGeneralizedLinearPMML(model: GeneralizedLinearModel): Unit = { | ||
pmml.getHeader.setDescription(description) | ||
|
||
if (model.weights.size > 0) { | ||
val fields = new SArray[FieldName](model.weights.size) | ||
val dataDictionary = new DataDictionary | ||
val miningSchema = new MiningSchema | ||
val regressionTable = new RegressionTable(model.intercept) | ||
val regressionModel = new RegressionModel() | ||
.withFunctionName(MiningFunctionType.REGRESSION) | ||
.withMiningSchema(miningSchema) | ||
.withModelName(description) | ||
.withRegressionTables(regressionTable) | ||
|
||
for (i <- 0 until model.weights.size) { | ||
fields(i) = FieldName.create("field_" + i) | ||
dataDictionary.withDataFields(new DataField(fields(i), OpType.CONTINUOUS, DataType.DOUBLE)) | ||
miningSchema | ||
.withMiningFields(new MiningField(fields(i)) | ||
.withUsageType(FieldUsageType.ACTIVE)) | ||
regressionTable.withNumericPredictors(new NumericPredictor(fields(i), model.weights(i))) | ||
} | ||
|
||
// for completeness add target field | ||
val targetField = FieldName.create("target") | ||
dataDictionary.withDataFields(new DataField(targetField, OpType.CONTINUOUS, DataType.DOUBLE)) | ||
miningSchema | ||
.withMiningFields(new MiningField(targetField) | ||
.withUsageType(FieldUsageType.TARGET)) | ||
|
||
dataDictionary.withNumberOfFields(dataDictionary.getDataFields.size) | ||
|
||
pmml.setDataDictionary(dataDictionary) | ||
pmml.withModels(regressionModel) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
Double.MinValue
.