-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-29095][ML] add extractInstances #25802
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| package org.apache.spark.ml | ||
|
|
||
| import org.apache.spark.annotation.{DeveloperApi, Since} | ||
| import org.apache.spark.ml.feature.LabeledPoint | ||
| import org.apache.spark.ml.feature.{Instance, LabeledPoint} | ||
| import org.apache.spark.ml.linalg.{Vector, VectorUDT} | ||
| import org.apache.spark.ml.param._ | ||
| import org.apache.spark.ml.param.shared._ | ||
|
|
@@ -62,6 +62,39 @@ private[ml] trait PredictorParams extends Params | |
| } | ||
| SchemaUtils.appendColumn(schema, $(predictionCol), DoubleType) | ||
| } | ||
|
|
||
| /** | ||
| * Extract [[labelCol]], weightCol(if any) and [[featuresCol]] from the given dataset, | ||
| * and put it in an RDD with strong types. | ||
| */ | ||
| protected def extractInstances(dataset: Dataset[_]): RDD[Instance] = { | ||
| val w = this match { | ||
| case p: HasWeightCol => | ||
| if (isDefined(p.weightCol) && $(p.weightCol).nonEmpty) { | ||
| col($(p.weightCol)).cast(DoubleType) | ||
| } else { | ||
| lit(1.0) | ||
|
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. Here too do you need a weight col, if the implementation doesn't support it (and shouldn't be calling this method)? or is it different?
Contributor
Author
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 is different from the above place. Even if a ML impl supports weighting, its |
||
| } | ||
| } | ||
|
|
||
| dataset.select(col($(labelCol)).cast(DoubleType), w, col($(featuresCol))).rdd.map { | ||
| case Row(label: Double, weight: Double, features: Vector) => | ||
| Instance(label, weight, features) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extract [[labelCol]], weightCol(if any) and [[featuresCol]] from the given dataset, | ||
| * and put it in an RDD with strong types. | ||
| * Validate the output instances with the given function. | ||
| */ | ||
| protected def extractInstances(dataset: Dataset[_], | ||
| validateInstance: Instance => Unit): RDD[Instance] = { | ||
| extractInstances(dataset).map { instance => | ||
| validateInstance(instance) | ||
| instance | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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.
I place it in
PredictorParamso that methods likeGBTModel.evaluateEachIterationcan reuse it in the future.