-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-9312][ML] Add RawPrediction, numClasses, and numFeatures for OneVsRestModel #21044
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
0cfc20a
2a47e2b
0c32fca
ebf4a6c
b3c7fec
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 |
|---|---|---|
|
|
@@ -191,6 +191,7 @@ final class OneVsRestModel private[ml] ( | |
| val updateUDF = udf { (predictions: Map[Int, Double], prediction: Vector) => | ||
| predictions + ((index, prediction(1))) | ||
| } | ||
|
|
||
| model.setFeaturesCol($(featuresCol)) | ||
| val transformedDataset = model.transform(df).select(columns: _*) | ||
| val updatedDataset = transformedDataset | ||
|
|
@@ -206,18 +207,31 @@ final class OneVsRestModel private[ml] ( | |
| } | ||
|
|
||
| // output the RawPrediction as vector | ||
| val rawPredictionUDF = udf { (predictions: Map[Int, Double]) => | ||
| Vectors.sparse(numClasses, predictions.toList ) | ||
| } | ||
| if (getRawPredictionCol != "") { | ||
| val rawPredictionUDF = udf { (predictions: Map[Int, Double]) => | ||
| val myArray = Array.fill[Double](numClasses)(0.0) | ||
| predictions.foreach { case (idx, value) => myArray(idx) = value } | ||
| Vectors.dense(myArray) | ||
| } | ||
|
|
||
| // output the index of the classifier with highest confidence as prediction | ||
| val labelUDF = udf { (predictions: Vector) => predictions.argmax.toDouble } | ||
| // output the index of the classifier with highest confidence as prediction | ||
| val labelUDF = udf { (predictions: Vector) => predictions.argmax.toDouble } | ||
|
|
||
| // output confidence as rwa prediction, label and label metadata as prediction | ||
| aggregatedDataset | ||
| .withColumn(getRawPredictionCol, rawPredictionUDF(col(accColName))) | ||
| .withColumn(getPredictionCol, labelUDF(col(getRawPredictionCol)), labelMetadata) | ||
| .drop(accColName) | ||
| aggregatedDataset | ||
| .withColumn(getRawPredictionCol, rawPredictionUDF(col(accColName))) | ||
| .withColumn(getPredictionCol, labelUDF(col(getRawPredictionCol)), labelMetadata) | ||
| .drop(accColName) | ||
| } | ||
| else { | ||
|
||
| // output the index of the classifier with highest confidence as prediction | ||
| val labelUDF = udf { (predictions: Map[Int, Double]) => | ||
| predictions.maxBy(_._2)._1.toDouble | ||
| } | ||
| // output confidence as rwa prediction, label and label metadata as prediction | ||
|
||
| aggregatedDataset | ||
| .withColumn(getPredictionCol, labelUDF(col(accColName)), labelMetadata) | ||
| .drop(accColName) | ||
| } | ||
| } | ||
|
|
||
| @Since("1.4.1") | ||
|
|
||
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.
==>
udf { (rawPredictions: Vector) => ... }