Bird
Raised Fist0
ML Pythonml~8 mins

Multi-class classification in ML Python - Model Metrics & Evaluation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Metrics & Evaluation - Multi-class classification
Which metric matters for Multi-class classification and WHY

In multi-class classification, the model predicts one label out of many possible classes. We want to know how often it picks the right class. Accuracy is a simple metric that shows the percentage of correct predictions overall.

However, accuracy alone can hide problems if some classes appear much more than others. So, we also use Precision, Recall, and F1-score for each class. These tell us how well the model finds each class without mixing them up.

For example, if the model often confuses class A with class B, precision and recall for those classes will be low. This helps us understand where the model struggles.

Confusion matrix for Multi-class classification

A confusion matrix shows how predictions match true classes. For 3 classes (A, B, C), it looks like this:

          Predicted
          A   B   C
    True A 50  2   3
         B  4 45   1
         C  5  3  40
    

Here, 50 times the model correctly predicted A when the true class was A (True Positives for A). The 2 and 3 are mistakes where the true class was A but predicted as B or C.

We use these numbers to calculate precision, recall, and F1 for each class.

Precision vs Recall tradeoff with examples

In multi-class tasks, precision and recall help us understand errors better:

  • Precision for a class tells us: When the model says this class, how often is it right?
  • Recall for a class tells us: Of all true examples of this class, how many did the model find?

Example: Imagine a model classifying animals: cat, dog, rabbit.

  • If the model has high precision but low recall for "rabbit", it means when it says "rabbit" it is usually correct, but it misses many rabbits (calls them other animals).
  • If it has high recall but low precision for "rabbit", it finds most rabbits but often mistakes other animals as rabbits.

Depending on the goal, you might want to improve precision or recall for certain classes.

What "good" vs "bad" metric values look like for Multi-class classification

Good metrics:

  • Accuracy above 80% usually means the model predicts well overall.
  • Precision and recall above 75% for each class show balanced performance.
  • F1-score close to precision and recall means the model is consistent.

Bad metrics:

  • Accuracy near random chance (e.g., 33% for 3 classes) means poor learning.
  • Very low precision or recall for some classes means the model confuses those classes badly.
  • Big gaps between precision and recall suggest the model is biased or missing examples.
Common pitfalls in Multi-class classification metrics
  • Accuracy paradox: High accuracy can hide poor performance on rare classes.
  • Ignoring class imbalance: If some classes are rare, metrics should be checked per class, not just overall.
  • Data leakage: If test data leaks into training, metrics look too good but model fails in real life.
  • Overfitting: Very high training accuracy but low test accuracy means the model memorizes training data, not generalizing.
Self-check question

Your multi-class model has 98% accuracy but only 12% recall on one important class. Is it good for production?

Answer: No. The model misses most examples of that class, which can be critical depending on the use case. High overall accuracy hides this problem. You should improve recall for that class before using the model.

Key Result
In multi-class classification, accuracy shows overall correctness, but precision, recall, and F1 per class reveal detailed strengths and weaknesses.

Practice

(1/5)
1. What does multi-class classification mean in machine learning?
easy
A. Sorting data into only two groups
B. Sorting data into three or more groups
C. Predicting continuous numbers
D. Clustering data without labels

Solution

  1. Step 1: Understand classification types

    Binary classification sorts data into two groups, while multi-class sorts into three or more.
  2. Step 2: Match definition to options

    Sorting data into three or more groups correctly states sorting into three or more groups, which matches multi-class classification.
  3. Final Answer:

    Sorting data into three or more groups -> Option B
  4. Quick Check:

    Multi-class = three or more groups [OK]
Hint: Multi-class means 3+ groups, not just 2 [OK]
Common Mistakes:
  • Confusing multi-class with binary classification
  • Thinking multi-class predicts numbers
  • Mixing classification with clustering
2. Which of the following is the correct way to specify a multi-class classification model in Python using scikit-learn?
easy
A. from sklearn.linear_model import LogisticRegression\nmodel = LogisticRegression(multi_class='multinomial', solver='lbfgs')
B. from sklearn.linear_model import LogisticRegression\nmodel = LogisticRegression(multi_class='binary')
C. from sklearn.svm import SVC\nmodel = SVC(kernel='linear', multi_class=true)
D. from sklearn.tree import DecisionTreeClassifier\nmodel = DecisionTreeClassifier(multi_class='multinomial')

Solution

  1. Step 1: Check scikit-learn multi-class syntax

    LogisticRegression supports multi_class='multinomial' with solver='lbfgs' for multi-class tasks.
  2. Step 2: Evaluate each option

    from sklearn.linear_model import LogisticRegression\nmodel = LogisticRegression(multi_class='multinomial', solver='lbfgs') uses correct parameters. from sklearn.linear_model import LogisticRegression\nmodel = LogisticRegression(multi_class='binary') wrongly uses 'binary'. from sklearn.svm import SVC\nmodel = SVC(kernel='linear', multi_class=true)'s SVC does not have multi_class parameter. from sklearn.tree import DecisionTreeClassifier\nmodel = DecisionTreeClassifier(multi_class='multinomial')'s DecisionTreeClassifier does not accept multi_class parameter.
  3. Final Answer:

    from sklearn.linear_model import LogisticRegression model = LogisticRegression(multi_class='multinomial', solver='lbfgs') -> Option A
  4. Quick Check:

    LogisticRegression multi_class='multinomial' is correct [OK]
Hint: Use multi_class='multinomial' with LogisticRegression [OK]
Common Mistakes:
  • Using multi_class='binary' for multi-class tasks
  • Passing multi_class to models that don't accept it
  • Forgetting to set solver='lbfgs' with multinomial
3. Given the following code, what will be the shape of the predicted output array?
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

iris = load_iris()
X, y = iris.data, iris.target
model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=200)
model.fit(X, y)
predictions = model.predict(X)
medium
A. (3, 150)
B. (150, 3)
C. (150,)
D. (150, 1)

Solution

  1. Step 1: Understand predict output shape

    For multi-class classification, predict returns a 1D array of class labels, one per sample.
  2. Step 2: Check input data size

    iris dataset has 150 samples, so predictions shape is (150,)
  3. Final Answer:

    (150,) -> Option C
  4. Quick Check:

    Predict output shape = (number of samples,) [OK]
Hint: Predict returns 1D array of labels, length = number of samples [OK]
Common Mistakes:
  • Expecting predict to return probabilities shape
  • Confusing predict with predict_proba output
  • Assuming output is 2D array always
4. You trained a multi-class classifier but it throws this error: ValueError: Unknown label type: 'continuous'. What is the most likely cause?
medium
A. The training data has too few samples
B. The model does not support multi-class classification
C. The input features have missing values
D. The target labels are continuous numbers instead of discrete classes

Solution

  1. Step 1: Analyze error message

    ValueError about 'continuous' label type means labels are not discrete classes but continuous numbers.
  2. Step 2: Match cause to options

    The target labels are continuous numbers instead of discrete classes correctly identifies continuous labels as cause. Other options do not relate to label type error.
  3. Final Answer:

    The target labels are continuous numbers instead of discrete classes -> Option D
  4. Quick Check:

    Continuous labels cause 'Unknown label type' error [OK]
Hint: Check if labels are discrete classes, not continuous numbers [OK]
Common Mistakes:
  • Ignoring label type and focusing on features
  • Assuming model limitation causes this error
  • Not verifying label data format
5. You want to improve a multi-class classification model's performance on an imbalanced dataset with 5 classes. Which approach is best to try first?
hard
A. Use class weights to give more importance to minority classes during training
B. Reduce the number of classes to 2 by merging some classes
C. Increase the learning rate to speed up training
D. Remove samples from majority classes to balance dataset

Solution

  1. Step 1: Understand imbalance problem

    Imbalanced classes cause model to favor majority classes, hurting minority class accuracy.
  2. Step 2: Evaluate options for imbalance handling

    Using class weights (Use class weights to give more importance to minority classes during training) helps model focus on minority classes without losing data. Reducing classes (B) changes problem scope. Increasing learning rate (A) may harm training. Removing samples (D) loses valuable data.
  3. Final Answer:

    Use class weights to give more importance to minority classes during training -> Option A
  4. Quick Check:

    Class weights help handle imbalance best [OK]
Hint: Apply class weights to balance learning on imbalanced classes [OK]
Common Mistakes:
  • Merging classes loses important distinctions
  • Increasing learning rate can cause unstable training
  • Removing data wastes valuable information