-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0cfc20a
add rawPrediction as an output column;
lu-wang-dl 2a47e2b
make rawPrediction optionall
lu-wang-dl 0c32fca
change the tmp array name
lu-wang-dl ebf4a6c
fix typos and comments
lu-wang-dl b3c7fec
add require in OneVsRestModel
lu-wang-dl 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 hidden or 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 |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ import org.apache.spark.SparkContext | |
| import org.apache.spark.annotation.Since | ||
| import org.apache.spark.ml._ | ||
| import org.apache.spark.ml.attribute._ | ||
| import org.apache.spark.ml.linalg.Vector | ||
| import org.apache.spark.ml.linalg.{Vector, Vectors} | ||
| import org.apache.spark.ml.param.{Param, ParamMap, ParamPair, Params} | ||
| import org.apache.spark.ml.param.shared.{HasParallelism, HasWeightCol} | ||
| import org.apache.spark.ml.util._ | ||
|
|
@@ -55,7 +55,7 @@ private[ml] trait ClassifierTypeTrait { | |
| /** | ||
| * Params for [[OneVsRest]]. | ||
| */ | ||
| private[ml] trait OneVsRestParams extends PredictorParams | ||
| private[ml] trait OneVsRestParams extends ClassifierParams | ||
| with ClassifierTypeTrait with HasWeightCol { | ||
|
|
||
| /** | ||
|
|
@@ -138,6 +138,14 @@ final class OneVsRestModel private[ml] ( | |
| @Since("1.4.0") val models: Array[_ <: ClassificationModel[_, _]]) | ||
| extends Model[OneVsRestModel] with OneVsRestParams with MLWritable { | ||
|
|
||
| require(models.nonEmpty, "OneVsRestModel requires at least one model for one class") | ||
|
|
||
| @Since("2.4.0") | ||
| val numClasses: Int = models.length | ||
|
|
||
| @Since("2.4.0") | ||
| val numFeatures: Int = models.head.numFeatures | ||
|
|
||
| /** @group setParam */ | ||
| @Since("2.1.0") | ||
| def setFeaturesCol(value: String): this.type = set(featuresCol, value) | ||
|
|
@@ -146,6 +154,10 @@ final class OneVsRestModel private[ml] ( | |
| @Since("2.1.0") | ||
| def setPredictionCol(value: String): this.type = set(predictionCol, value) | ||
|
|
||
| /** @group setParam */ | ||
| @Since("2.4.0") | ||
| def setRawPredictionCol(value: String): this.type = set(rawPredictionCol, value) | ||
|
Member
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. You'll need to add this to the Estimator too. |
||
|
|
||
| @Since("1.4.0") | ||
| override def transformSchema(schema: StructType): StructType = { | ||
| validateAndTransformSchema(schema, fitting = false, getClassifier.featuresDataType) | ||
|
|
@@ -181,6 +193,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 | ||
|
|
@@ -195,15 +208,34 @@ final class OneVsRestModel private[ml] ( | |
| newDataset.unpersist() | ||
| } | ||
|
|
||
| // output the index of the classifier with highest confidence as prediction | ||
| val labelUDF = udf { (predictions: Map[Int, Double]) => | ||
| predictions.maxBy(_._2)._1.toDouble | ||
| } | ||
| if (getRawPredictionCol != "") { | ||
| val numClass = models.length | ||
|
|
||
| // output label and label metadata as prediction | ||
| aggregatedDataset | ||
| .withColumn($(predictionCol), labelUDF(col(accColName)), labelMetadata) | ||
| .drop(accColName) | ||
| // output the RawPrediction as vector | ||
| val rawPredictionUDF = udf { (predictions: Map[Int, Double]) => | ||
| val predArray = Array.fill[Double](numClass)(0.0) | ||
| predictions.foreach { case (idx, value) => predArray(idx) = value } | ||
| Vectors.dense(predArray) | ||
| } | ||
|
|
||
| // output the index of the classifier with highest confidence as prediction | ||
| val labelUDF = udf { (rawPredictions: Vector) => rawPredictions.argmax.toDouble } | ||
|
|
||
| // output confidence as raw prediction, label and label metadata as prediction | ||
| 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 label and label metadata as prediction | ||
| aggregatedDataset | ||
| .withColumn(getPredictionCol, labelUDF(col(accColName)), labelMetadata) | ||
| .drop(accColName) | ||
| } | ||
| } | ||
|
|
||
| @Since("1.4.1") | ||
|
|
@@ -297,6 +329,10 @@ final class OneVsRest @Since("1.4.0") ( | |
| @Since("1.5.0") | ||
| def setPredictionCol(value: String): this.type = set(predictionCol, value) | ||
|
|
||
| /** @group setParam */ | ||
| @Since("2.4.0") | ||
| def setRawPredictionCol(value: String): this.type = set(rawPredictionCol, value) | ||
|
|
||
| /** | ||
| * The implementation of parallel one vs. rest runs the classification for | ||
| * each class in a separate threads. | ||
|
|
||
This file contains hidden or 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
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.
Let's add a require() statement here which checks that models.nonEmpty is true (to throw an exception upon construction, rather than when numFeatures calls models.head below). Just to be safe...