Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -1100,28 +1100,32 @@ class LogisticRegressionModel private[spark] (

private lazy val _intercept = interceptVector(0)
private lazy val _interceptVector = interceptVector.toDense
private var _threshold = Double.NaN
private var _rawThreshold = Double.NaN

updateBinaryThreshold()
private lazy val _binaryThresholdArray = {
val array = Array(Double.NaN, Double.NaN)
updateBinaryThresholds(array)
array
}
private def _threshold: Double = _binaryThresholdArray(0)
private def _rawThreshold: Double = _binaryThresholdArray(1)

private def updateBinaryThreshold(): Unit = {
private def updateBinaryThresholds(array: Array[Double]): Unit = {
if (!isMultinomial) {
_threshold = getThreshold
val _threshold = getThreshold
array(0) = _threshold
if (_threshold == 0.0) {
_rawThreshold = Double.NegativeInfinity
array(1) = Double.NegativeInfinity
} else if (_threshold == 1.0) {
_rawThreshold = Double.PositiveInfinity
array(1) = Double.PositiveInfinity
} else {
_rawThreshold = math.log(_threshold / (1.0 - _threshold))
array(1) = math.log(_threshold / (1.0 - _threshold))
}
}
}

@Since("1.5.0")
override def setThreshold(value: Double): this.type = {
super.setThreshold(value)
updateBinaryThreshold()
updateBinaryThresholds(_binaryThresholdArray)
this
}

Expand All @@ -1131,7 +1135,7 @@ class LogisticRegressionModel private[spark] (
@Since("1.5.0")
override def setThresholds(value: Array[Double]): this.type = {
super.setThresholds(value)
updateBinaryThreshold()
updateBinaryThresholds(_binaryThresholdArray)
this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,9 @@ class LogisticRegressionSuite extends MLTest with DefaultReadWriteTest {
}

test("thresholds prediction") {
val blr = new LogisticRegression().setFamily("binomial")
val blr = new LogisticRegression().setFamily("binomial").setThreshold(1.0)
val binaryModel = blr.fit(smallBinaryDataset)

binaryModel.setThreshold(1.0)
testTransformer[(Double, Vector)](smallBinaryDataset.toDF(), binaryModel, "prediction") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

master fails in this modified case:
tx

row => assert(row.getDouble(0) === 0.0)
}
Expand Down