Bird
Raised Fist0
NLPml~8 mins

Fine-grained sentiment (5-class) in NLP - 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 - Fine-grained sentiment (5-class)
Which metric matters for Fine-grained sentiment (5-class) and WHY

In fine-grained sentiment analysis with 5 classes (e.g., very negative, negative, neutral, positive, very positive), accuracy is a common metric because it shows how often the model predicts the exact sentiment correctly.

However, accuracy alone can hide problems if some classes are rare. So, we also use macro-averaged precision, recall, and F1-score. These treat each class equally, helping us see if the model struggles with any specific sentiment.

For example, if the model often misses "very negative" reviews, recall for that class will be low, signaling a problem.

Confusion matrix example for 5-class sentiment
       Predicted
       VN   N   Neu  P   VP
    VN  40   5    3   1    1
    N    4  50    6   3    2
    Neu  2   7   60   5    6
    P    1   3    7  55    4
    VP   0   1    4   6   60

    VN = Very Negative
    N = Negative
    Neu = Neutral
    P = Positive
    VP = Very Positive
    

This matrix shows how many samples from each true class (rows) were predicted as each class (columns).

Precision vs Recall tradeoff with examples

Imagine the "very negative" class is important to catch because it signals urgent customer issues.

  • High precision for "very negative" means when the model says a review is very negative, it usually is. This avoids false alarms.
  • High recall means the model finds most very negative reviews, even if some are wrongly labeled.

If you want to quickly fix urgent problems, high recall is better to not miss any bad reviews.

But if you want to avoid bothering your team with false alarms, high precision is better.

What "good" vs "bad" metric values look like

Good model example:

  • Accuracy around 70% or higher (since 5 classes is harder than 2)
  • Macro F1-score above 0.65, showing balanced performance across classes
  • Precision and recall for each class above 0.6, especially for important classes like "very negative" and "very positive"

Bad model example:

  • Accuracy below 50%, close to random guessing (20% for 5 classes)
  • Macro F1-score below 0.4, meaning poor balance
  • Very low recall for some classes (e.g., 0.2 for "very negative"), meaning many missed cases
Common pitfalls in metrics for fine-grained sentiment
  • Accuracy paradox: High accuracy can hide poor performance on rare classes if the dataset is imbalanced.
  • Ignoring class imbalance: Some sentiments may be rare but important; metrics must reflect this.
  • Data leakage: If test data leaks into training, metrics will be unrealistically high.
  • Overfitting: Very high training accuracy but low test accuracy means the model memorizes instead of generalizing.
Self-check question

Your fine-grained sentiment model has 98% accuracy but only 12% recall on the "very negative" class. Is it good for production? Why or why not?

Answer: No, it is not good. The very low recall on "very negative" means the model misses most very negative reviews. Even though overall accuracy is high, the model fails to catch important negative feedback, which could harm customer satisfaction.

Key Result
Use accuracy plus macro-averaged precision, recall, and F1 to fairly evaluate all 5 sentiment classes, especially rare but important ones.

Practice

(1/5)
1. What does a fine-grained sentiment analysis with 5 classes typically represent?
easy
A. It translates text into five different languages.
B. It detects whether the text is about five different topics.
C. It summarizes text into five key points.
D. It classifies text into five levels from very negative to very positive feelings.

Solution

  1. Step 1: Understand sentiment analysis levels

    Fine-grained sentiment analysis divides feelings into multiple levels, often five, ranging from very negative to very positive.
  2. Step 2: Match the description to options

    It classifies text into five levels from very negative to very positive feelings correctly describes this as classifying text by sentiment levels. Other options describe unrelated tasks.
  3. Final Answer:

    It classifies text into five levels from very negative to very positive feelings. -> Option D
  4. Quick Check:

    Fine-grained sentiment = 5-level sentiment classification [OK]
Hint: Fine-grained means detailed sentiment levels, not topics or languages [OK]
Common Mistakes:
  • Confusing sentiment classes with topic categories
  • Thinking it translates text instead of analyzing feelings
  • Assuming it summarizes text instead of classifying sentiment
2. Which of the following is the correct way to represent sentiment labels for a 5-class fine-grained sentiment model in Python?
easy
A. labels = {1: 'positive', 2: 'neutral', 3: 'negative'}
B. labels = ['very negative', 'negative', 'neutral', 'positive', 'very positive']
C. labels = ['happy', 'sad', 'angry', 'excited']
D. labels = ['positive', 'negative']

Solution

  1. Step 1: Identify correct label list for 5-class sentiment

    The 5-class sentiment labels should cover very negative to very positive, exactly five classes.
  2. Step 2: Check each option

    labels = ['very negative', 'negative', 'neutral', 'positive', 'very positive'] lists five sentiment levels correctly. Options B, C, and D have wrong counts or unrelated labels.
  3. Final Answer:

    labels = ['very negative', 'negative', 'neutral', 'positive', 'very positive'] -> Option B
  4. Quick Check:

    5-class sentiment labels = labels = ['very negative', 'negative', 'neutral', 'positive', 'very positive'] [OK]
Hint: Five classes must cover full sentiment range, not fewer or unrelated words [OK]
Common Mistakes:
  • Using fewer than five labels
  • Using unrelated emotion words
  • Confusing label types with numeric codes
3. Given the following Python code snippet for a fine-grained sentiment model prediction, what will be the printed output?
import numpy as np
predictions = np.array([[0.1, 0.2, 0.4, 0.2, 0.1]])
predicted_class = np.argmax(predictions)
print(predicted_class)
medium
A. 2
B. 3
C. 1
D. 0

Solution

  1. Step 1: Understand np.argmax on prediction array

    np.argmax returns the index of the highest value in the array. Here, predictions are [0.1, 0.2, 0.4, 0.2, 0.1].
  2. Step 2: Find the index of max value

    The max value is 0.4 at index 2 (0-based). So predicted_class = 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Max probability index = 2 [OK]
Hint: np.argmax returns index of max value, count from zero [OK]
Common Mistakes:
  • Confusing index with value
  • Counting indices from 1 instead of 0
  • Misreading the prediction array
4. You trained a fine-grained sentiment model with 5 classes but your evaluation shows accuracy stuck at 20%. What is the most likely cause?
medium
A. The model is randomly guessing because the output layer has 5 units but the loss function expects 2 classes.
B. The model is overfitting the training data perfectly.
C. The input text is too long for the model to process.
D. The optimizer learning rate is too high.

Solution

  1. Step 1: Analyze low accuracy with 5-class output

    Accuracy near 20% suggests random guessing among 5 classes (1/5 = 20%).
  2. Step 2: Check mismatch between output and loss

    If the model output layer has 5 units but the loss function expects 2 classes (binary), the model cannot learn properly, causing random predictions.
  3. Final Answer:

    Output layer and loss function class count mismatch causing random guessing. -> Option A
  4. Quick Check:

    Mismatch output vs loss classes = random 20% accuracy [OK]
Hint: Check output units match loss classes to avoid random guessing [OK]
Common Mistakes:
  • Assuming overfitting causes low accuracy
  • Blaming input length without evidence
  • Ignoring loss function and output layer mismatch
5. You want to improve a fine-grained sentiment model's performance on imbalanced data where 'neutral' class is very common. Which approach is best?
hard
A. Increase the batch size to speed up training.
B. Remove the 'neutral' class from the dataset to balance classes.
C. Use class weights in the loss function to give more importance to rare classes.
D. Use a simpler model with fewer layers.

Solution

  1. Step 1: Understand class imbalance problem

    When one class dominates, the model may ignore rare classes, hurting performance on them.
  2. Step 2: Choose method to handle imbalance

    Using class weights in the loss function tells the model to pay more attention to rare classes, improving balanced learning.
  3. Final Answer:

    Use class weights in loss to handle imbalanced classes effectively. -> Option C
  4. Quick Check:

    Class weights improve learning on rare classes [OK]
Hint: Apply class weights to balance rare vs common classes [OK]
Common Mistakes:
  • Removing common classes loses important data
  • Changing batch size doesn't fix imbalance
  • Simpler models may underfit complex data