Skip to content
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

[SPARK-6705][MLLIB] Add fit intercept api to ml logisticregression #5301

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.storage.StorageLevel
* Params for logistic regression.
*/
private[classification] trait LogisticRegressionParams extends ProbabilisticClassifierParams
with HasRegParam with HasMaxIter with HasThreshold
with HasRegParam with HasMaxIter with HasFitIntercept with HasThreshold


/**
Expand All @@ -46,6 +46,7 @@ class LogisticRegression
with LogisticRegressionParams {

setRegParam(0.1)
setFitIntercept(true)
setMaxIter(100)
setThreshold(0.5)

Expand All @@ -55,6 +56,9 @@ class LogisticRegression
/** @group setParam */
def setMaxIter(value: Int): this.type = set(maxIter, value)

/** @group setParam */
def setFitIntercept(value: Boolean): this.type = set(fitIntercept, value)

/** @group setParam */
def setThreshold(value: Double): this.type = set(threshold, value)

Expand All @@ -71,6 +75,7 @@ class LogisticRegression
lr.optimizer
.setRegParam(paramMap(regParam))
.setNumIterations(paramMap(maxIter))
.addIntercept(paramMap(fitIntercept))
val oldModel = lr.run(oldDataset)
val lrm = new LogisticRegressionModel(this, paramMap, oldModel.weights, oldModel.intercept)

Expand Down
12 changes: 12 additions & 0 deletions mllib/src/main/scala/org/apache/spark/ml/param/sharedParams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ private[ml] trait HasProbabilityCol extends Params {
def getProbabilityCol: String = get(probabilityCol)
}

private[ml] trait HasFitIntercept extends Params {
/**
* param for fitting the intercept term
* @group param
*/
val fitIntercept: BooleanParam =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about this, can you please make this default to true? (Add "Some(true)" as a 4th argument, plus update doc.) Thanks!

new BooleanParam(this, "fitIntercept", "indicates whether to fit an intercept term")

/** @group getParam */
def getFitIntercept: Boolean = get(fitIntercept)
}

private[ml] trait HasThreshold extends Params {
/**
* param for threshold in (binary) prediction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class LogisticRegressionSuite extends FunSuite with MLlibTestSparkContext {
assert(lr.getPredictionCol == "prediction")
assert(lr.getRawPredictionCol == "rawPrediction")
assert(lr.getProbabilityCol == "probability")
assert(lr.getFitIntercept == true)
val model = lr.fit(dataset)
model.transform(dataset)
.select("label", "probability", "prediction", "rawPrediction")
Expand All @@ -55,6 +56,14 @@ class LogisticRegressionSuite extends FunSuite with MLlibTestSparkContext {
assert(model.getPredictionCol == "prediction")
assert(model.getRawPredictionCol == "rawPrediction")
assert(model.getProbabilityCol == "probability")
assert(model.intercept != 0.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use "!==" instead of "!=" (It handles types better.)

}

test("logistic regression doesn't fit intercept when fitIntercept is off") {
val lr = new LogisticRegression
lr.setFitIntercept(false)
val model = lr.fit(dataset)
assert(model.intercept == 0.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar: Please use "===" instead of "=="

}

test("logistic regression with setters") {
Expand Down