[置顶] SparkML实战之三:Logistic回归

package MLlib

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.mllib.classification.{LogisticRegressionWithLBFGS, LogisticRegressionModel}
import org.apache.spark.mllib.evaluation.MulticlassMetrics
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.util.MLUtils
/** * Created by root on 16-1-12. */
object LogisticRegression {
  def main(args: Array[String]) {

    val conf = new SparkConf().setAppName("LinearRegression").setMaster("local[4]")
    val sc = new SparkContext(conf)
    // Load training data in LIBSVM format.
    val data = MLUtils.loadLibSVMFile(sc, "/usr/local/spark/spark-1.4.1-bin-hadoop2.4" +
      "/data/mllib/sample_libsvm_data.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)
    }

    // Get evaluation metrics.
    val metrics = new MulticlassMetrics(predictionAndLabels)
    val precision = metrics.precision
    println("Precision = " + precision)

    // Save and load model
// model.save(sc, "myModelPath")
// val sameModel = LogisticRegressionModel.load(sc, "myModelPath")
  }
}

你可能感兴趣的:(spark,机器学习,ml,Logistic)