【问题标题】:Why pysark areaUnderROC is different from sklearn roc_auc_score?为什么 pysark areaUnderROC 与 sklearn roc_auc_score 不同?
【发布时间】:2021-05-30 13:39:03
【问题描述】:

我正在使用 pyspark 运行一些二进制分类,并且我正在使用 BinaryClassificationEvaluator 来评估在测试集上做出的预测。如果我使用 sklearn roc_auc_score,为什么我会得到不同的结果?例如:

from pyspark.ml.evaluation import BinaryClassificationEvaluator
from pyspark.ml.classification import RandomForestClassifier

trainDF, testDF = df.randomSplit([.8, .2], seed=42)
evaluator = BinaryClassificationEvaluator(
    labelCol="label", rawPredictionCol="prediction", metricName="areaUnderROC")

rf = RandomForestClassifier(labelCol="label", featuresCol="features")
rfModel = rf.fit(trainDF)
prediction = rfModel.transform(testDF)

# now in the DataFrame prediction I have those columns: 'label','prediction','probability'
#  +-----+----------+---------------------+
#  |label|prediction|probability          |
#  +-----+----------+---------------------+
#  |0    |0.0       |[1.0,0.0]            |
#  |0    |0.0       |[0.9765625,0.0234375]|
#  |0    |0.0       |[0.9765625,0.0234375]|
#  +-----+----------+---------------------+

areaUnderROC = evaluator.evaluate(prediction) #IT RETURNS 0.954459

#NOW I USE PANDAS
RF_pred = prediction.select('label', 'prediction', 'probability').toPandas()
probRF=[]
for i in range(prediction.count()):
    probRF.append(RF_pred['probability'][i][1])  #it takes only the probability for the label 1 

auc = roc_auc_score(RF_pred['label'], probRF) #IT RETURNS 0.9962

这怎么可能?

【问题讨论】:

  • 将其发布为答案,而不是编辑。 stackoverflow.com/help/self-answer
  • 正如@BenReiniger 正确指出的那样-请不要在问题中回答;将此作为答案发布,然后接受(您需要等待 48 小时才能这样做,因为您是自己回答的),以便将来对其他人有用。

标签: pyspark scikit-learn apache-spark-mllib apache-spark-ml


【解决方案1】:

我应该使用 probability 列或者保留我猜的默认值!!这样我就有了相同的 roc_auc_score 值。我不应该有错误或错误的列吗?

evaluator = BinaryClassificationEvaluator(
                                         labelCol="label",
                                         rawPredictionCol="probability", 
                                         metricName="areaUnderROC")

我希望这会有所帮助,因为即使在官方文档中也没有太多关于 BinaryClassificationEvaluator 的内容,他们只在示例中使用 MulticlassClassificationEvaluator

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 2021-05-27
    • 2016-10-18
    • 2021-06-15
    • 1970-01-01
    • 2020-06-07
    • 2021-03-24
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多