0
0
NLPml~5 mins

Fine-grained sentiment (5-class) in NLP

Choose your learning style9 modes available
Introduction

Fine-grained sentiment helps us understand feelings in more detail, not just good or bad but also in between.

When you want to know if a review is very positive, positive, neutral, negative, or very negative.
When analyzing customer feedback to improve products or services.
When tracking how opinions change over time in social media posts.
When you want more detailed insights than just positive or negative sentiment.
Syntax
NLP
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report

# Example steps:
# 1. Prepare text data and labels (5 classes)
# 2. Split data into train and test
# 3. Convert text to numbers
# 4. Train a classifier
# 5. Predict and evaluate

Labels should be integers representing the 5 sentiment classes, e.g., 0 to 4.

Text needs to be converted to numbers before training a model.

Examples
Define sentiment classes as numbers for the model.
NLP
labels = [0, 1, 2, 3, 4]  # 0=very negative, 4=very positive
Convert text data into a matrix of token counts.
NLP
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(texts)
Train a simple logistic regression model on the training data.
NLP
model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)
Predict sentiment classes and print detailed evaluation metrics.
NLP
predictions = model.predict(X_test)
print(classification_report(y_test, predictions))
Sample Model

This program trains a simple model to classify text into 5 sentiment classes and shows how well it works.

NLP
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report

# Sample data: texts and their fine-grained sentiment labels (0 to 4)
texts = [
    "I hate this product, it is terrible.",  # very negative
    "This is bad, not what I expected.",      # negative
    "It's okay, nothing special.",            # neutral
    "I like it, works well.",                 # positive
    "Absolutely love it, highly recommend!"  # very positive
]
labels = [0, 1, 2, 3, 4]

# Split data
X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.4, random_state=42)

# Convert text to numbers
vectorizer = CountVectorizer()
X_train_vec = vectorizer.fit_transform(X_train)
X_test_vec = vectorizer.transform(X_test)

# Train model
model = LogisticRegression(max_iter=1000)
model.fit(X_train_vec, y_train)

# Predict
predictions = model.predict(X_test_vec)

# Evaluate
report = classification_report(y_test, predictions, zero_division=0)
print(report)
OutputSuccess
Important Notes

Fine-grained sentiment needs more data to train well than simple positive/negative.

Try different models or text features for better accuracy.

Labels must be consistent and cover all 5 classes.

Summary

Fine-grained sentiment divides feelings into 5 levels from very negative to very positive.

We convert text to numbers and train a model to predict these levels.

Evaluation shows how well the model guesses the correct sentiment class.