在pyspark中获取分类后的所有评估指标
Get all evaluation metrics after classification in pyspark
我训练了一个模型,想计算几个重要指标,例如 accuracy
、precision
、recall
和 f1 score
。
我遵循的过程是:
from pyspark.ml.classification import LogisticRegression
lr = LogisticRegression(featuresCol='features',labelCol='label')
lrModel = lr.fit(train)
lrPredictions = lrModel.transform(test)
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.ml.evaluation import BinaryClassificationEvaluator
eval_accuracy = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy")
eval_precision = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="precision")
eval_recall = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="recall")
eval_f1 = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="f1Measure")
eval_auc = BinaryClassificationEvaluator(labelCol="label", rawPredictionCol="prediction")
accuracy = eval_accuracy.evaluate(lrPredictions)
precision = eval_precision.evaluate(lrPredictions)
recall = eval_recall.evaluate(lrPredictions)
f1score = eval_f1.evaluate(lrPredictions)
auc = eval_accuracy.evaluate(lrPredictions)
但是,它只能计算accuracy
和auc
,而不能计算其他三个。我应该在这里修改什么?
根据docs,对于F1 measure,precision,recall,MulticlassClassificationEvaluator
的相关参数应该分别是
metricName="f1"
metricName="precisionByLabel"
metricName="recallByLabel"
我训练了一个模型,想计算几个重要指标,例如 accuracy
、precision
、recall
和 f1 score
。
我遵循的过程是:
from pyspark.ml.classification import LogisticRegression
lr = LogisticRegression(featuresCol='features',labelCol='label')
lrModel = lr.fit(train)
lrPredictions = lrModel.transform(test)
from pyspark.ml.evaluation import MulticlassClassificationEvaluator
from pyspark.ml.evaluation import BinaryClassificationEvaluator
eval_accuracy = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="accuracy")
eval_precision = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="precision")
eval_recall = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="recall")
eval_f1 = MulticlassClassificationEvaluator(labelCol="label", predictionCol="prediction", metricName="f1Measure")
eval_auc = BinaryClassificationEvaluator(labelCol="label", rawPredictionCol="prediction")
accuracy = eval_accuracy.evaluate(lrPredictions)
precision = eval_precision.evaluate(lrPredictions)
recall = eval_recall.evaluate(lrPredictions)
f1score = eval_f1.evaluate(lrPredictions)
auc = eval_accuracy.evaluate(lrPredictions)
但是,它只能计算accuracy
和auc
,而不能计算其他三个。我应该在这里修改什么?
根据docs,对于F1 measure,precision,recall,MulticlassClassificationEvaluator
的相关参数应该分别是
metricName="f1"
metricName="precisionByLabel"
metricName="recallByLabel"