This repository has been archived by the owner on Nov 15, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add implicit method to convert spark dataset to smile sparsedataset
- Loading branch information
1 parent
5bd982b
commit 0771da7
Showing
6 changed files
with
103 additions
and
5 deletions.
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
58 changes: 58 additions & 0 deletions
58
src/main/scala/org/apache/spark/smile/implicits/package.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,58 @@ | ||
package org.apache.spark.smile | ||
|
||
import org.apache.spark.ml.linalg.Vector | ||
import org.apache.spark.sql.functions._ | ||
import org.apache.spark.sql.{Row, Dataset => SparkDataset} | ||
import smile.data.{NominalAttribute, SparseDataset} | ||
|
||
package object implicits { | ||
|
||
implicit class BetterSmileDataset(dataset: SparkDataset[_]) { | ||
|
||
def toSmileDataset( | ||
featuresColName: String = "features", | ||
labelColName: String = "label", | ||
weightColName: String = "weight"): SparseDataset = { | ||
val classification = | ||
dataset.select(labelColName).take(20).forall { case Row(x: Double) => (x % 1) == 0 } | ||
val minClass = | ||
if (classification) | ||
dataset.select(labelColName).agg(min(labelColName)).head.getDouble(0).toInt | ||
else 0 | ||
val res = | ||
if (classification) new SparseDataset(new NominalAttribute("class")) | ||
else new SparseDataset(new NominalAttribute("response")) | ||
if (dataset.columns.contains(weightColName)) { | ||
dataset | ||
.select(Array(featuresColName, labelColName, weightColName).map(col): _*) | ||
.collect() | ||
.toList | ||
.zipWithIndex | ||
.foreach { | ||
case (Row(features: Vector, label: Double, weight: Double), i: Int) => | ||
features.toArray.toList.zipWithIndex.foreach { | ||
case (x: Double, j: Int) => res.set(i, j, x) | ||
} | ||
if (classification) res.set(i, label.toInt - minClass, weight) | ||
else res.set(i, label, weight) | ||
} | ||
} else { | ||
dataset | ||
.select(Array(featuresColName, labelColName).map(col): _*) | ||
.collect() | ||
.zipWithIndex | ||
.foreach { | ||
case (Row(features: Vector, label: Double), i: Int) => | ||
features.toArray.zipWithIndex.foreach { | ||
case (x: Double, j: Int) => res.set(i, j, x) | ||
} | ||
if (classification) res.set(i, label.toInt - minClass) else res.set(i, label) | ||
} | ||
} | ||
res | ||
|
||
} | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package smile | ||
|
||
package object tuning extends SparkOperators {} | ||
package object tuning extends Operators {} |
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,25 @@ | ||
package org.apache.spark.smile | ||
|
||
import com.holdenkarau.spark.testing.DatasetSuiteBase | ||
import org.scalatest.FunSuite | ||
import smile.classification.knn | ||
import smile.data._ | ||
import smile.validation.{Accuracy, ClassificationMeasure, _} | ||
|
||
class ImplicitSuite extends FunSuite with DatasetSuiteBase { | ||
|
||
test("toSmileDataset") { | ||
|
||
import org.apache.spark.smile.implicits._ | ||
|
||
val mushrooms = spark.read.format("libsvm").load("data/mushrooms.svm") | ||
|
||
val (x,y) = mushrooms.toSmileDataset().unzipInt | ||
|
||
val res = cv(x, y, 5, Seq(new Accuracy().asInstanceOf[ClassificationMeasure]): _*) { (x, y) => knn(x, y, 3) } | ||
|
||
assert(res(0) == 1) | ||
|
||
} | ||
|
||
} |
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