user3803714
user3803714

Reputation: 5389

Spark: Logistic regression

This code works great!

val model = new LogisticRegressionWithLBFGS().setNumClasses(2).run(training)

I am able to call model.predict(...)

However, when I try to setup the model parameters, I can't call model.predict For example, with the following code, I can't call predict on model variable.

val model = new LogisticRegressionWithLBFGS().setNumClasses(2)

model.optimizer.setUpdater(new L1Updater).setRegParam(0.0000001).setNumIterations(numIterations)
 model.run(training)

Any help with this will be great.

Upvotes: 1

Views: 843

Answers (1)

zero323
zero323

Reputation: 330393

It happens because model in the second case is LogisticRegressionWithLBFGS not LogisticRegressionModel. What you need is something like this:

import org.apache.spark.mllib.classification.{
  LogisticRegressionWithLBFGS, LogisticRegressionModel}
import org.apache.spark.mllib.optimization.L1Updater

// Create algorithm instance
val lr: LogisticRegressionWithLBFGS = new LogisticRegressionWithLBFGS()
  .setNumClasses(2)

// Set optimizer params (it modifies lr object)
lr.optimizer
  .setUpdater(new L1Updater)
  .setRegParam(0.0000001)
  .setNumIterations(numIterations)

// Train model
val model: LogisticRegressionModel = lr.run(training)

Now model is LogisticRegressionModel and can be used for predictions.

Upvotes: 2

Related Questions