title: MLlib: Calculating Precision and Recall for multiple threshold values
body: <p>I set the setting the threshold value of my logistic regression to 0.5 before I use it for scoring. I now want to get precision, recall, f1 score for that value. Unfortunately, when I try doing that the only threshold values that I see are 1.0 and 0.0. How do I get metrics for threshold values other than 0 and 1.</p>
  
  <p>For example here is the o/p:</p>
  
  <p>Threshold is: 1.0, Precision is: 0.85</p>
  
  <p>Threshold is: 0.0, Precision is: 0.312641</p>
  
  <p>I don't get Precision for Threshold 0.5. Here is the relevant code.</p>
  
  <p>// I am setting the threshold value of my Logistic regression model here. </p>
  
  <pre><code>model.setThreshold(0.5)
  
  // Compute the score and generate an RDD with prediction and label values.  
  val predictionAndLabels = data.map { 
    case LabeledPoint(label, features) =&gt; (model.predict(features), label)
  }
  </code></pre>
  
  <p>// I now want to compute the precision and recall and other metrics. Since I have set the model threshold to 0.5, I want to get PR at that value.</p>
  
  <pre><code>val metrics = new BinaryClassificationMetrics(predictionAndLabels)
  val precision = metrics.precisionByThreshold()
  
  precision.foreach { 
    case (t, p) =&gt; {
      println(s"Threshold is: $t, Precision is: $p")
  
      if (t == 0.5) {
        println(s"Desired: Threshold is: $t, Precision is: $p")        
      }
  }
  </code></pre>