Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address comments
  • Loading branch information
yanboliang committed Apr 9, 2015
commit 49600cc3be22383d787d40a73cae09ed7106c083
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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._
Copy link
Contributor

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.


Expand All @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The 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))

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getAs[Double](0) -> getDouble

The data might not be loaded into a single partition. So we need to sort (boundary, prediction) list first based on isotonic.

(boundaries.toArray, predictions.toArray)
}
}
Expand Down