【发布时间】: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