fMeasure() missing 1 required positional argument: 'label' - print_performance_metrics(predictions)

Viewed 431
from pyspark.ml.evaluation import BinaryClassificationEvaluator
from pyspark.mllib.evaluation import BinaryClassificationMetrics, MulticlassMetrics

def print_performance_metrics(predictions):
  # Evaluate model
  evaluator = BinaryClassificationEvaluator(rawPredictionCol="rawPrediction")
  auc = evaluator.evaluate(predictions, {evaluator.metricName: "areaUnderROC"})
  aupr = evaluator.evaluate(predictions, {evaluator.metricName: "areaUnderPR"})
  print("auc = {}".format(auc))
  print("aupr = {}".format(aupr))

  # get rdd of predictions and labels for mllib eval metrics
  predictionAndLabels = predictions.select("prediction","label").rdd

  # Instantiate metrics objects
  binary_metrics = BinaryClassificationMetrics(predictionAndLabels)
  multi_metrics = MulticlassMetrics(predictionAndLabels)

  # Area under precision-recall curve
  print("Area under PR = {}".format(binary_metrics.areaUnderPR))
  # Area under ROC curve
  print("Area under ROC = {}".format(binary_metrics.areaUnderROC))
  # Accuracy
  print("Accuracy = {}".format(multi_metrics.accuracy))
  # Confusion Matrix
  print(multi_metrics.confusionMatrix())
  

  ### Question F1, Precision Score ###
  
  # F1
  print("F1 = {}".format(multi_metrics.fMeasure()))
  # Precision
 # print("Precision = {}".format(multi_metrics.precision()))
  # Recall
 # print("Recall = {}".format(multi_metrics.recall()))
  # FPR
 # print("FPR = {}".format(multi_metrics.falsePositiveRate(0.0)))
  # TPR
 # print("TPR = {}".for

 mat(multi_metrics.truePositiveRate(0.0)))

I am working on the above function for model scores. It gave me the auc and accuracy score but it did not work for multiclassmetrics. I am not able to get the f1 and precision scores it gives

fMeasure() missing 1 required positional argument: 'label'

1 Answers

If you take a look at the formula of F1 score you will see that it is interested in true/false labeling of one class only. So F1 score is label centric. It evaluates a kind of accuracy of models considering the true/false labeling of a certain class. The "Example" section under the official documentation of MultiClassMetrics clearly shows that you need to select one label among your existing classes to call fMeasure which is plausible when you realize that its a label centric metric as I explained.

Related