简体   繁体   English

Spark:逻辑回归

[英]Spark: Logistic regression

This code works great! 这段代码很棒!

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

I am able to call model.predict(...) 我可以打电话给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. 但是,当我尝试设置模型参数时,无法调用model.predict例如,使用以下代码,就无法对模型变量调用预测。

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 . 发生这种情况是因为在第二种情况下, modelLogisticRegressionWithLBFGS而不是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. 现在,模型为LogisticRegressionModel ,可用于预测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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