PySpark
Multiclass Classification
Multilabel Classification
Evaluator
Machine Learning

What is the difference between MulticlassClassificationEvaluator and MultilabelClassificationEvaluator in PySpark?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Apache Spark's MLlib is a robust library for machine learning that provides tools for classification, regression, clustering, and collaborative filtering. Within classification tasks, especially in cases involving multiple classes or labels, the choice of evaluation metric becomes crucial to accurately assess model performance. Here, we explore two significant evaluators within PySpark: `MulticlassClassificationEvaluator` and `MultilabelClassificationEvaluator`. Understanding the distinctions between these evaluators is essential for choosing the appropriate one based on the type of classification problem.

MulticlassClassificationEvaluator

`MulticlassClassificationEvaluator` is used for evaluating models that output a discrete number of classes—often referred to as multiclass classification problems. These problems assign each instance to one of three or more classes, where each instance can belong to only one class.

Core `Parameters` and Usage

  • Prediction Column: The column in the dataframe that contains the predicted classes.
  • Label Column: The column in the dataframe that contains the true class labels.
  • Metric Name: This specifies the metric used to evaluate the model. Common metrics include:
    • `"f1"`: Harmonic mean of precision and recall.
    • `"accuracy"`: Fraction of correct predictions.
    • `"weightedPrecision"`: Precision weighted by label frequencies.
    • `"weightedRecall"`: Recall weighted by label frequencies.
    • `"weightedFMeasure"`: F1 score weighted by label frequencies.

Example

  • Prediction Column: Contains an array of predicted labels for each instance.
  • Label Column: Contains an array of actual labels for each instance.
  • Metric Name: Some typical metrics include:
    • `"subsetAccuracy"`: Fraction of exactly correct predictions across instances.
    • `"accuracy"`: Fraction of correctly predicted labels out of total predicted and actual labels.
    • `"f1Measure"`: F1-measure over the set of labels.
    • `"precision"`: Multilabel precision.
    • `"recall"`: Multilabel recall.
  • Metric Choice: Always choose the evaluation metric that aligns with your model's primary objective. Metrics can influence how a model is perceived as successful or not.
  • Label Handling: Ensure the labels are appropriately handled and transformed into the correct format expected by your chosen evaluator, especially in multilabel cases.
  • Data Representation: Consider the representation of your data; for multilabel, having predictions and labels as arrays is essential for accurate evaluation.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.