Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.80 KB | None | 0 0
  1. package org.vitalstar
  2.  
  3. import org.apache.spark.mllib.optimization.LBFGS;
  4.  
  5. import org.apache.spark.mllib.classification.LogisticRegressionModel
  6. import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics
  7. import org.apache.spark.mllib.linalg.Vectors
  8. import org.apache.spark.mllib.optimization.{LBFGS, LogisticGradient, SquaredL2Updater}
  9. import org.apache.spark.mllib.util.MLUtils
  10.  
  11.  
  12. import org.scalatest.{FunSuite, BeforeAndAfterAll}
  13. import org.scalatest.junit.JUnitRunner
  14. import org.junit.Assert._
  15. import org.junit.runner.RunWith
  16.  
  17. import org.apache.spark.{SparkConf, SparkContext}
  18.  
  19.  
  20. @RunWith(classOf[JUnitRunner])
  21. class lbfgs extends FunSuite with BeforeAndAfterAll {
  22.  
  23.   def main(): Unit ={
  24.     var testString = ""
  25.     testString = "Hello World"
  26.   }
  27.  
  28.   def lbfgstest(): Double ={
  29.  
  30.  
  31.     val conf = new SparkConf()
  32.     conf.setMaster("local")
  33.     conf.setAppName("iLBFGS")
  34.     val sc = new SparkContext(conf)
  35.  
  36.     val data = MLUtils.loadLibSVMFile(sc, "testdata/sample_libsvm_data.txt")
  37.     val numFeatures = data.take(1)(0).features.size
  38.  
  39.     // Split data into training (60%) and test (40%).
  40.     val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L)
  41.  
  42.     // Append 1 into the training data as intercept.
  43.     val training = splits(0).map(x => (x.label, MLUtils.appendBias(x.features))).cache()
  44.  
  45.     val test = splits(1)
  46.  
  47.     // Run training algorithm to build the model
  48.     val numCorrections = 10
  49.     val convergenceTol = 1e-4
  50.     val maxNumIterations = 20
  51.     val regParam = 0.1
  52.     val initialWeightsWithIntercept = Vectors.dense(new Array[Double](numFeatures + 1))
  53.  
  54.     val (weightsWithIntercept, loss) = LBFGS.runLBFGS(
  55.       training,
  56.       new LogisticGradient(),
  57.       new SquaredL2Updater(),
  58.       numCorrections,
  59.       convergenceTol,
  60.       maxNumIterations,
  61.       regParam,
  62.       initialWeightsWithIntercept)
  63.  
  64.     val model = new LogisticRegressionModel(
  65.       Vectors.dense(weightsWithIntercept.toArray.slice(0, weightsWithIntercept.size - 1)),
  66.       weightsWithIntercept(weightsWithIntercept.size - 1))
  67.  
  68.     // Clear the default threshold.
  69.     model.clearThreshold()
  70.  
  71.     // Compute raw scores on the test set.
  72.     val scoreAndLabels = test.map { point =>
  73.       val score = model.predict(point.features)
  74.       (score, point.label)
  75.     }
  76.  
  77.     // Get evaluation metrics.
  78.     val metrics = new BinaryClassificationMetrics(scoreAndLabels)
  79.     val auROC = metrics.areaUnderROC()
  80.  
  81.     println("Loss of each step in training process")
  82.     loss.foreach(println)
  83.     println("Area under ROC = " + auROC)
  84.     return auROC
  85.   }
  86.  
  87.  
  88.   test("Testing String") {
  89.  
  90.     var testString = ""
  91.     testString = "Hello World"
  92.     assert(testString == "Hello World")
  93.   }
  94.  
  95.   test("Testing LBFGS"){
  96.     var au_lbfgs = lbfgstest()
  97.     assert(au_lbfgs == 1.0)
  98.   }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement