Skip to content

SPARK-1791 - SVM implementation does not use threshold parameter #725

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

Closed
wants to merge 1 commit into from
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 @@ -65,7 +65,7 @@ class SVMModel private[mllib] (
intercept: Double) = {
val margin = weightMatrix.toBreeze.dot(dataMatrix.toBreeze) + intercept
threshold match {
case Some(t) => if (margin < 0) 0.0 else 1.0
case Some(t) => if (margin < t) 0.0 else 1.0
case None => margin
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,43 @@ class SVMSuite extends FunSuite with LocalSparkContext {
assert(numOffPredictions < input.length / 5)
}

test("SVM with threshold") {
val nPoints = 10000

// NOTE: Intercept should be small for generating equal 0s and 1s
val A = 0.01
val B = -1.5
val C = 1.0

val testData = SVMSuite.generateSVMInput(A, Array[Double](B, C), nPoints, 42)

val testRDD = sc.parallelize(testData, 2)
testRDD.cache()

val svm = new SVMWithSGD().setIntercept(true)
svm.optimizer.setStepSize(1.0).setRegParam(1.0).setNumIterations(100)

val model = svm.run(testRDD)

val validationData = SVMSuite.generateSVMInput(A, Array[Double](B, C), nPoints, 17)
val validationRDD = sc.parallelize(validationData, 2)

// Test prediction on RDD.

var predictions = model.predict(validationRDD.map(_.features)).collect()
assert(predictions.count(_ == 0.0) != predictions.length)

// High threshold makes all the predictions 0.0
model.setThreshold(10000.0)
predictions = model.predict(validationRDD.map(_.features)).collect()
assert(predictions.count(_ == 0.0) == predictions.length)

// Low threshold makes all the predictions 1.0
model.setThreshold(-10000.0)
predictions = model.predict(validationRDD.map(_.features)).collect()
assert(predictions.count(_ == 1.0) == predictions.length)
}

test("SVM using local random SGD") {
val nPoints = 10000

Expand Down