2017-04-07 64 views
1

閱讀下面如何推斷星火MLlib預測類標籤計算原始分數

https://spark.apache.org/docs/latest/mllib-optimization.html

星火文件下面二元分類預測的示例代碼段:

val model = new LogisticRegressionModel(
    Vectors.dense(weightsWithIntercept.toArray.slice(0,weightsWithIntercept.size - 1)), 
    weightsWithIntercept(weightsWithIntercept.size - 1)) 

    // Clear the default threshold. 
    model.clearThreshold() 

    // Compute raw scores on the test set. 
    val scoreAndLabels = test.map { point => 
    val score = model.predict(point.features) 
    (score, point.label) 

正如你看到的model.prediction(point.features)返回原始分數,它是超平面分離距離的邊界。

我的問題是:

(1)我怎麼能知道,如果基於上述計算原始分數的預測類別標籤是0或1?

或者

(2)如何推斷預測類別標籤(0或1)在從上述計算原始分數這種二元分類情況?

回答

2

默認情況下,閾值爲0.5,因此使用BinaryClassificationMetrics時,如果分數爲< 0.5,則分類標籤爲0,如果分數較高,則分類爲0。所以你可以做同樣的從分數中推斷出課堂。

+0

如何獲得算法確定的計算ROC曲線的最佳閾值? – Tom

+0

在度量對象上,您可以通過各種度量的閾值獲取得分。例如:val f1Score = metrics.fMeasureByThreshold。然後你可以迭代找到最好的閾值細節:https://spark.apache.org/docs/latest/mllib-evaluation-metrics.html –