-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20351] [ML] Add trait hasTrainingSummary to replace the duplicate code #17654
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 5 commits
3bca3b1
ffbdb6e
4b660e3
0008b89
ecef1d0
79ae7fe
11ba5f6
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 |
|---|---|---|
|
|
@@ -647,33 +647,20 @@ class LinearRegressionModel private[ml] ( | |
| @Since("1.3.0") val intercept: Double, | ||
| @Since("2.3.0") val scale: Double) | ||
| extends RegressionModel[Vector, LinearRegressionModel] | ||
| with LinearRegressionParams with GeneralMLWritable { | ||
| with LinearRegressionParams with GeneralMLWritable | ||
| with HasTrainingSummary[LinearRegressionTrainingSummary] { | ||
|
|
||
| private[ml] def this(uid: String, coefficients: Vector, intercept: Double) = | ||
| this(uid, coefficients, intercept, 1.0) | ||
|
|
||
| private var trainingSummary: Option[LinearRegressionTrainingSummary] = None | ||
|
|
||
| override val numFeatures: Int = coefficients.size | ||
|
|
||
| /** | ||
| * Gets summary (e.g. residuals, mse, r-squared ) of model on training set. An exception is | ||
| * thrown if `trainingSummary == None`. | ||
| */ | ||
| @Since("1.5.0") | ||
| def summary: LinearRegressionTrainingSummary = trainingSummary.getOrElse { | ||
| throw new SparkException("No training summary available for this LinearRegressionModel") | ||
| } | ||
|
|
||
| private[regression] | ||
| def setSummary(summary: Option[LinearRegressionTrainingSummary]): this.type = { | ||
| this.trainingSummary = summary | ||
| this | ||
| } | ||
|
|
||
| /** Indicates whether a training summary exists for this model instance. */ | ||
| @Since("1.5.0") | ||
| def hasSummary: Boolean = trainingSummary.isDefined | ||
| override def summary: LinearRegressionTrainingSummary = super.summary | ||
|
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. On the one hand, you don't need these overrides for this to work correctly, right? but I suppose it's necessary to preserve the
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. I got an error message from Java side when removing
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. Ah OK nevermind then. Thanks for checking. |
||
|
|
||
| /** | ||
| * Evaluates the model on a test dataset. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.ml.util | ||
|
|
||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.annotation.Since | ||
|
|
||
|
|
||
| /** | ||
| * Trait for models that provides Training summary. | ||
| * | ||
| * @tparam T Summary instance type | ||
| */ | ||
| @Since("3.0.0") | ||
| private[ml] trait HasTrainingSummary[T] { | ||
|
|
||
| private[ml] final var trainingSummary: Option[T] = None | ||
|
|
||
| /** Indicates whether a training summary exists for this model instance. */ | ||
| @Since("3.0.0") | ||
| def hasSummary: Boolean = trainingSummary.isDefined | ||
|
|
||
| /** | ||
| * Gets summary of model on training set. An exception is | ||
| * thrown if `trainingSummary == None`. | ||
|
||
| */ | ||
| @Since("3.0.0") | ||
| def summary: T = trainingSummary.getOrElse { | ||
| throw new SparkException( | ||
| s"No training summary available for this ${this.getClass.getSimpleName}") | ||
| } | ||
|
|
||
| private[ml] def setSummary(summary: Option[T]): this.type = { | ||
| this.trainingSummary = summary | ||
| this | ||
| } | ||
| } | ||
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.
Nit: space before braces
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.
Updated. Thanks
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.
It looks like there still isn't a space here.