我在使用Spark 2和Scala训练基于逻辑回归的二分类模型,并且使用了import org.apache.spark.ml.classification.LogisticRegression
,这是Spark 2中的新ml API。然而,当我通过AUROC评估模型时,我没有找到使用概率值(0到1之间的双精度数)而不是二分类结果(0/1)的方法。之前可以通过removeThreshold()
实现这一点,但在ml.LogisticRegression
中我没有找到类似的方法。因此,有没有办法做到这一点?
我使用的评估器是
val evaluator = new BinaryClassificationEvaluator() .setLabelCol("label") .setRawPredictionCol("rawPrediction") .setMetricName("areaUnderROC")val auroc = evaluator.evaluate(predictions)`
回答:
如果你想获得除了0/1之外的概率输出,可以尝试以下方法:
import org.apache.spark.ml.classification.{BinaryLogisticRegressionSummary, LogisticRegression}val lr = new LogisticRegression() .setMaxIter(100) .setRegParam(0.3)val lrModel = lr.fit(trainData)val summary = lrModel.summarysummary.predictions.select("probability").show()