问题描述
我有一个包含 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
这是混淆矩阵。
既然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")
}