-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-5990] [MLLIB] Model import/export for IsotonicRegression #5270
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,9 +133,7 @@ class IsotonicRegressionModel ( | |
| } | ||
|
|
||
| override def save(sc: SparkContext, path: String): Unit = { | ||
| val intervals = boundaries.toList.zip(predictions.toList).toArray | ||
| val data = IsotonicRegressionModel.SaveLoadV1_0.Data(intervals) | ||
| IsotonicRegressionModel.SaveLoadV1_0.save(sc, path, data, isotonic) | ||
| IsotonicRegressionModel.SaveLoadV1_0.save(sc, path, boundaries, predictions, isotonic) | ||
| } | ||
|
|
||
| override protected def formatVersion: String = "1.0" | ||
|
|
@@ -153,9 +151,14 @@ object IsotonicRegressionModel extends Loader[IsotonicRegressionModel] { | |
| def thisClassName: String = "org.apache.spark.mllib.regression.IsotonicRegressionModel" | ||
|
|
||
| /** Model data for model import/export */ | ||
| case class Data(intervals: Array[(Double, Double)]) | ||
|
|
||
| def save(sc: SparkContext, path: String, data: Data, isotonic: Boolean): Unit = { | ||
| case class Data(boundary: Double, prediction: Double) | ||
|
|
||
| def save( | ||
| sc: SparkContext, | ||
| path: String, | ||
| boundaries: Array[Double], | ||
| predictions: Array[Double], | ||
| isotonic: Boolean): Unit = { | ||
| val sqlContext = new SQLContext(sc) | ||
| import sqlContext.implicits._ | ||
|
|
||
|
|
@@ -164,21 +167,18 @@ object IsotonicRegressionModel extends Loader[IsotonicRegressionModel] { | |
| ("isotonic" -> isotonic))) | ||
| sc.parallelize(Seq(metadata), 1).saveAsTextFile(metadataPath(path)) | ||
|
|
||
| val dataRDD: DataFrame = sc.parallelize(Seq(data), 1).toDF() | ||
| dataRDD.saveAsParquetFile(dataPath(path)) | ||
| sqlContext.createDataFrame(boundaries.toList.zip(predictions.toList) | ||
| .map { case (b, p) => Data(b, p) }).saveAsParquetFile(dataPath(path)) | ||
|
Contributor
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. It may read better with the following style: sqlContext.createDataFrame(
boundaries.toSeq.zip(predictions).map { case (b, p) => Data(b, p) }
).saveAsParquetFile(dataPath(path))
Contributor
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. Please see my previous comment about the code style here. The following has a better separation of the logic: sqlContext.createDataFrame(
boundaries.toSeq.zip(predictions).map { case (b, p) => Data(b, p) }
).saveAsParquetFile(dataPath(path)) |
||
| } | ||
|
|
||
| def load(sc: SparkContext, path: String): (Array[Double], Array[Double]) = { | ||
| val sqlContext = new SQLContext(sc) | ||
| val dataRDD = sqlContext.parquetFile(dataPath(path)) | ||
|
|
||
| checkSchema[Data](dataRDD.schema) | ||
| val dataArray = dataRDD.select("intervals").take(1) | ||
| assert(dataArray.size == 1, | ||
| s"Unable to load IsotonicRegressionModel data from: ${dataPath(path)}") | ||
| val data = dataArray(0) | ||
| val intervals = data.getAs[Seq[(Double, Double)]](0) | ||
| val (boundaries, predictions) = intervals.unzip | ||
| val dataArray = dataRDD.select("boundary", "prediction").collect() | ||
| val (boundaries, predictions) = dataArray.map { | ||
| x => (x.getAs[Double](0), x.getAs[Double](1)) }.toList.unzip | ||
|
Contributor
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.
The data might not be loaded into a single partition. So we need to sort (boundary, prediction) list first based on |
||
| (boundaries.toArray, predictions.toArray) | ||
| } | ||
| } | ||
|
|
||
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.
Remove this line because no implicits are used.