-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-14479] [ML] GLM supports output link prediction #12287
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 |
|---|---|---|
|
|
@@ -78,6 +78,20 @@ private[regression] trait GeneralizedLinearRegressionBase extends PredictorParam | |
| @Since("2.0.0") | ||
| def getLink: String = $(link) | ||
|
|
||
| /** | ||
| * Param for link prediction (linear predictor) column name. | ||
| * Default is empty, which means we do not output link prediction. | ||
| * @group param | ||
| */ | ||
| @Since("2.0.0") | ||
| final val linkPredictionCol: Param[String] = new Param[String](this, "linkPredictionCol", | ||
| "link prediction (linear predictor) column name") | ||
| setDefault(linkPredictionCol, "") | ||
|
|
||
| /** @group getParam */ | ||
| @Since("2.0.0") | ||
| def getLinkPredictionCol: String = $(linkPredictionCol) | ||
|
|
||
| import GeneralizedLinearRegression._ | ||
|
|
||
| @Since("2.0.0") | ||
|
|
@@ -93,7 +107,12 @@ private[regression] trait GeneralizedLinearRegressionBase extends PredictorParam | |
| Family.fromName($(family)) -> Link.fromName($(link))), "Generalized Linear Regression " + | ||
| s"with ${$(family)} family does not support ${$(link)} link function.") | ||
| } | ||
| super.validateAndTransformSchema(schema, fitting, featuresDataType) | ||
| val newSchema = super.validateAndTransformSchema(schema, fitting, featuresDataType) | ||
|
Contributor
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. This assumes
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. We do not assume |
||
| if ($(linkPredictionCol).nonEmpty) { | ||
|
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. The check |
||
| SchemaUtils.appendColumn(newSchema, $(linkPredictionCol), DoubleType) | ||
| } else { | ||
| newSchema | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -196,6 +215,13 @@ class GeneralizedLinearRegression @Since("2.0.0") (@Since("2.0.0") override val | |
| def setSolver(value: String): this.type = set(solver, value) | ||
| setDefault(solver -> "irls") | ||
|
|
||
| /** | ||
| * Sets the link prediction (linear predictor) column name. | ||
| * @group setParam | ||
| */ | ||
| @Since("2.0.0") | ||
| def setLinkPredictionCol(value: String): this.type = set(linkPredictionCol, value) | ||
|
|
||
| override protected def train(dataset: Dataset[_]): GeneralizedLinearRegressionModel = { | ||
| val familyObj = Family.fromName($(family)) | ||
| val linkObj = if (isDefined(link)) { | ||
|
|
@@ -664,6 +690,13 @@ class GeneralizedLinearRegressionModel private[ml] ( | |
| extends RegressionModel[Vector, GeneralizedLinearRegressionModel] | ||
| with GeneralizedLinearRegressionBase with MLWritable { | ||
|
|
||
| /** | ||
| * Sets the link prediction (linear predictor) column name. | ||
| * @group setParam | ||
| */ | ||
| @Since("2.0.0") | ||
| def setLinkPredictionCol(value: String): this.type = set(linkPredictionCol, value) | ||
|
|
||
| import GeneralizedLinearRegression._ | ||
|
|
||
| lazy val familyObj = Family.fromName($(family)) | ||
|
|
@@ -675,10 +708,35 @@ class GeneralizedLinearRegressionModel private[ml] ( | |
| lazy val familyAndLink = new FamilyAndLink(familyObj, linkObj) | ||
|
|
||
| override protected def predict(features: Vector): Double = { | ||
| val eta = BLAS.dot(features, coefficients) + intercept | ||
| val eta = predictLink(features) | ||
| familyAndLink.fitted(eta) | ||
| } | ||
|
|
||
| /** | ||
| * Calculate the link prediction (linear predictor) of the given instance. | ||
| */ | ||
| private def predictLink(features: Vector): Double = { | ||
| BLAS.dot(features, coefficients) + intercept | ||
| } | ||
|
|
||
| override def transform(dataset: Dataset[_]): DataFrame = { | ||
| transformSchema(dataset.schema) | ||
| transformImpl(dataset) | ||
| } | ||
|
|
||
| override protected def transformImpl(dataset: Dataset[_]): DataFrame = { | ||
| val predictUDF = udf { (features: Vector) => predict(features) } | ||
| val predictLinkUDF = udf { (features: Vector) => predictLink(features) } | ||
| var output = dataset | ||
| if ($(predictionCol).nonEmpty) { | ||
|
Contributor
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. See my previous comment about the assumption on
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. Because we checked |
||
| output = output.withColumn($(predictionCol), predictUDF(col($(featuresCol)))) | ||
| } | ||
| if ($(linkPredictionCol).nonEmpty) { | ||
| output = output.withColumn($(linkPredictionCol), predictLinkUDF(col($(featuresCol)))) | ||
| } | ||
| output.toDF | ||
| } | ||
|
|
||
| private var trainingSummary: Option[GeneralizedLinearRegressionSummary] = None | ||
|
|
||
| /** | ||
|
|
||
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.
Mention the default value in the doc.