微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pyspark 中的二元分类评估器 AUC 分数

如何解决Pyspark 中的二元分类评估器 AUC 分数

我有一个包含 2 个类别(流失者和非流失者)的数据集,比例为 1:4。我通过 Spark MLlib 使用了随机森林算法。我的模型在预测流失率方面很糟糕,什么也没做。 我使用 BinaryClassificationEvaluator 在 Pyspark 中评估我的模型。 BinaryClassificationEvaluator 的认指标是 AreaUnderRoc。

我的代码

from pyspark.ml.classification import RandomForestClassifier
evaluator = BinaryClassificationEvaluator()

# Create an initial RandomForest model.
rf = RandomForestClassifier(labelCol="label",featuresCol="indexedFeatures",numTrees=1000,impurity="entropy")
# Train model with Training Data
rfModel = rf.fit(train_df)
rfModel.featureImportances

# Make predictions on test data using the Transformer.transform() method.
predictions = rfModel.transform(test_df)

# AUC Evaluate best model
evaluator.evaluate(predictions)
print('Test Area Under Roc',evaluator.evaluate(predictions))

Test Area Under Roc 0.8672196520652589

这是混淆矩阵。

confusion matrix

既然TP=0,那分数怎么可能呢?这个值会不会出错?

我有其他模型可以正常工作,但这个分数让我怀疑其他模型是否也有问题。

解决方法

您的数据可能严重偏向其中一个类,我建议使用 Precision 或 F-Measure,因为在这种情况下它是更好的指标。 尝试使用这个:

import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics
val metrics = new BinaryClassificationMetrics(predictions)
val f1Score = metrics.fMeasureByThreshold
f1Score.collect.foreach { case (t,f) =>
  println(s"Threshold: $t,F-score: $f,Beta = 1")
}

https://spark.apache.org/docs/latest/api/scala/org/apache/spark/mllib/evaluation/BinaryClassificationMetrics.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。