简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM