Spark mllib 逻辑回归

栏目: 服务器 · 发布时间: 6年前

内容简介:逻辑回归其实是一个分类算法而不是回归算法。通常是利用已知的自变量来预测一个离散型因变量的值(像二进制值0/1,是/否,真/假)。简单来说,它就是通过拟合一个逻辑函数(logit fuction)来预测一个事件发生的概率。所以它预测的是一个概率值,自然,它的输出值应该在0到1之间。假设你的一个朋友让你回答一道题。可能的结果只有两种:你答对了或没有答对。为了研究你最擅长的题目领域,你做了各种领域的题目。那么这个研究的结果可能是这样的:如果是一道十年级的三角函数题,你有70%的可能性能解出它。但如果是一道五年级

逻辑回归

逻辑回归其实是一个分类算法而不是回归算法。通常是利用已知的自变量来预测一个离散型因变量的值(像二进制值0/1,是/否,真/假)。简单来说,它就是通过拟合一个逻辑函数(logit fuction)来预测一个事件发生的概率。所以它预测的是一个概率值,自然,它的输出值应该在0到1之间。

假设你的一个朋友让你回答一道题。可能的结果只有两种:你答对了或没有答对。为了研究你最擅长的题目领域,你做了各种领域的题目。那么这个研究的结果可能是这样的:如果是一道十年级的三角函数题,你有70%的可能性能解出它。但如果是一道五年级的历史题,你会的概率可能只有30%。逻辑回归就是给你这样的概率结果。

Logistic回归简单分析

优点:计算代价不高,易于理解和实现

缺点:容易欠拟合,分类精度可能不高

适用数据类型:数值型和标称型数据

package com.immooc.spark

import org.apache.spark.mllib.classification.{LogisticRegressionModel, LogisticRegressionWithLBFGS}
import org.apache.spark.mllib.evaluation.MulticlassMetrics
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.{SparkConf, SparkContext}

object logistic_regression {


  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("LogisticRegressionWithLBFGSExample").setMaster("local[2]")
    val sc = new SparkContext(conf)

    // $example on$
    // Load training data in LIBSVM format.
    val data = MLUtils.loadLibSVMFile(sc, "file:///Users/walle/Documents/D3/sparkmlib/wa.txt")

    // Split data into training (60%) and test (40%).
    val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L)
    val training = splits(0).cache()
    val test = splits(1)

    // Run training algorithm to build the model
    val model = new LogisticRegressionWithLBFGS()
      .setNumClasses(10)
      .run(training)

    // Compute raw scores on the test set.
    val predictionAndLabels = test.map { case LabeledPoint(label, features) =>
      val prediction = model.predict(features)
      (prediction, label)
    }

    val print_predict = predictionAndLabels.take(20)
    println("prediction" + "\t" + "label")

    for (i <- 0 to print_predict.length - 1){
       println(print_predict(i)._1 + "\t" + print_predict(i)._2)
    }

    val patient = Vectors.dense(Array(70,3,180.0,4,3))
    val prediction = model.predict(patient)
    println(prediction)

    // Get evaluation metrics.
    val metrics = new MulticlassMetrics(predictionAndLabels)
    val accuracy = metrics.accuracy
    println(s"Accuracy = $accuracy")

    // Save and load model
    //    model.save(sc, "target/tmp/scalaLogisticRegressionWithLBFGSModel")
    //    val sameModel = LogisticRegressionModel.load(sc,
    //      "target/tmp/scalaLogisticRegressionWithLBFGSModel")
    // $example off$

    sc.stop()
  }
}
0 1:59 2:2 3:43.4 4:2 5:1
0 1:36 2:1 3:57.2 4:1 5:1
0 1:61 2:2 3:190 4:2 5:1
1 1:58 2:3 3:128 4:4 5:3
1 1:55 2:3 3:80 4:3 5:4
0 1:61 2:1 3:94 4:4 5:2
0 1:38 2:1 3:76 4:1 5:1
0 1:42 2:1 3:240 4:3 5:2
0 1:50 2:1 3:74 4:1 5:1
0 1:58 2:2 3:68.6 4:2 5:2
0 1:68 2:3 3:132.8 4:4 5:2
1 1:25 2:2 3:94.6 4:4 5:3
0 1:52 2:1 3:56 4:1 5:1
0 1:31 2:1 3:47.8 4:2 5:1
1 1:36 2:3 3:31.6 4:3 5:1
0 1:42 2:1 3:66.2 4:2 5:1
1 1:14 2:3 3:138.6 4:3 5:3
0 1:32 2:1 3:114 4:2 5:3
0 1:35 2:1 3:40.2 4:2 5:1
1 1:70 2:3 3:177.2 4:4 5:3
1 1:65 2:2 3:51.6 4:4 5:4
0 1:45 2:2 3:124 4:2 5:4
1 1:68 2:3 3:127.2 4:3 5:3
0 1:31 2:2 3:124.8 4:2 5:3

输出

prediction	label
0.0	0.0
0.0	1.0
0.0	0.0
0.0	0.0
1.0	1.0
0.0	0.0
1.0	1.0
0.0	0.0
0.0	1.0
0.0	0.0
0.0	1.0
0.0	0.0
0.0
Accuracy = 0.75

4649


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Persuasive Technology

Persuasive Technology

B.J. Fogg / Morgan Kaufmann / 2002-12 / USD 39.95

Can computers change what you think and do? Can they motivate you to stop smoking, persuade you to buy insurance, or convince you to join the Army? "Yes, they can," says Dr. B.J. Fogg, directo......一起来看看 《Persuasive Technology》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器