簡體   English   中英

Spark Logistic回歸和指標

[英]Spark Logistic regression and metrics

我想進行邏輯回歸100次,隨機分為測試和培訓。 然后,我想保存各個運行的性能指標,然后在以后使用它們來深入了解性能。

    for (index <- 1 to 100) {
    val splits = training_data.randomSplit(Array(0.90, 0.10), seed = index)
    val training = splits(0).cache()
    val test = splits(1)

    logrmodel = train_LogisticRegression_model(training)
    performLogisticRegressionRuns(logrmodel, test, index)
    }

    spark.stop()
  }

  def performLogisticRegressionRuns(model: LogisticRegressionModel, test: RDD[LabeledPoint], iterationcount: Int) {
   private val sb = StringBuilder.newBuilder

 // Compute raw scores on the test set. Once I cle

    model.clearThreshold()

    val predictionAndLabels = test.map { case LabeledPoint(label, features) =>
      val prediction = model.predict(features)
      (prediction, label)
    }


    val bcmetrics = new BinaryClassificationMetrics(predictionAndLabels)

    // I am showing two sample metrics, but I am collecting more including recall, area under roc, f1 score etc....

val precision = bcmetrics.precisionByThreshold()

precision.foreach { case (t, p) =>
  // If threshold is 0.5 as what we want, then get the precision and append it to the string. Idea is if score is <0.5 class 0, else class 1.
  if (t == 0.5) {
    println(s"Threshold is: $t, Precision is: $p")
    sb ++= p.toString() + "\t"

  }
}
    val auROC = bcmetrics.areaUnderROC
    sb ++= iteration + auPRC.toString() + "\t"

我想將每次迭代的性能結果保存在單獨的文件中。 我試過了,但是沒有用,任何幫助都很好

val data = spark.parallelize(sb)  
val filename =  "logreg-metrics" + iterationcount.toString() + ".txt"
data.saveAsTextFile(filename)

}

我能夠解決此問題,我做了以下工作。 我將字符串轉換為列表。

val data = spark.parallelize(List(sb))
val filename =  "logreg-metrics" + iterationcount.toString() + ".txt"
data.saveAsTextFile(filename)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM