当前位置:   article > 正文

逻辑回归原理及spark例子_scala logisticregressionmodel.load

scala logisticregressionmodel.load

之前在乐视网的时候组内有同事的挖掘工作用到逻辑回归,最近利用零散时间看了下逻辑回归的原理。主要参考了https://www.cnblogs.com/pinard/p/6029432.html  这篇文章,感觉写的比较清晰。

例子中对K元逻辑回归没有详细推导,我自己推导了一下,过程也比较简单。(太长时间不写字,感觉已经不会拿笔了。。。)

过程如图:

然后运行了一下spark自带的LogisticRegressionWithLBFGSExample例子。

源码如下:

  1. import org.apache.spark.{SparkConf, SparkContext}
  2. // $example on$
  3. import org.apache.spark.mllib.classification.{LogisticRegressionModel, LogisticRegressionWithLBFGS}
  4. import org.apache.spark.mllib.evaluation.MulticlassMetrics
  5. import org.apache.spark.mllib.regression.LabeledPoint
  6. import org.apache.spark.mllib.util.MLUtils
  7. // $example off$
  8. object LogisticRegressionWithLBFGSExample {
  9. def main(args: Array[String]): Unit = {
  10. val conf = new SparkConf().setAppName("LogisticRegressionWithLBFGSExample")
  11. val sc = new SparkContext(conf)
  12. // $example on$
  13. // Load training data in LIBSVM format.
  14. val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
  15. // Split data into training (60%) and test (40%).
  16. val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L)
  17. val training = splits(0).cache()
  18. val test = splits(1)
  19. // Run training algorithm to build the model
  20. val model = new LogisticRegressionWithLBFGS()
  21. .setNumClasses(10)
  22. .run(training)
  23. // Compute raw scores on the test set.
  24. val predictionAndLabels = test.map { case LabeledPoint(label, features) =>
  25. val prediction = model.predict(features)
  26. (prediction, label)
  27. }
  28. // Get evaluation metrics.
  29. val metrics = new MulticlassMetrics(predictionAndLabels)
  30. val accuracy = metrics.accuracy
  31. println(s"Accuracy = $accuracy")
  32. // Save and load model
  33. model.save(sc, "target/tmp/scalaLogisticRegressionWithLBFGSModel")
  34. val sameModel = LogisticRegressionModel.load(sc,
  35. "target/tmp/scalaLogisticRegressionWithLBFGSModel")
  36. // $example off$
  37. sc.stop()
  38. }
  39. }
  40. // scalastyle:on println
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/737096
推荐阅读
相关标签
  

闽ICP备14008679号