Getting Started with Sentiment Analysis using Python

Sentiment analysis is the automated process of tagging text data by polarity—typically positive, negative, or neutral—to enable companies to analyze data at scale and automate business processes. By leveraging the Hugging Face Hub and the Transformers library, developers can now implement state-of-the-art sentiment analysis with minimal code and no prior machine learning experience.

Implementing Sentiment Analysis with Pre-trained Models

Developers can integrate sentiment analysis into Python applications using the pipeline class from the transformers library. This approach allows for immediate predictions using default or specific models hosted on the Hugging Face Hub.

Quick Implementation

Integrating a default sentiment analysis model requires only five lines of code:

pip install -q transformers
from transformers import pipeline
sentiment_pipeline = pipeline("sentiment-analysis")
data = ["I love you", "I hate you"]
sentiment_pipeline(data)

Recommended Models

Depending on the use case, different models from the Hub may be more appropriate:

  • Twitter-roberta-base-sentiment: A roBERTa model trained on approximately 58 million tweets.
  • Bert-base-multilingual-uncased-sentiment: A model fine-tuned for product reviews across six languages (English, Dutch, German, French, Spanish, and Italian).
  • Distilbert-base-uncased-emotion: A model designed to detect specific emotions such as joy, sadness, love, anger, fear, and surprise.

Building Custom Sentiment Analysis Models

While pre-trained models provide a strong starting point, fine-tuning a model on domain-specific data can increase accuracy. Hugging Face offers two primary paths for customization: the Trainer API for developers and AutoNLP for those seeking a no-code solution.

Fine-tuning with the Trainer API

Using the Trainer API allows developers to take a pre-trained model and tweak it with additional training data. For example, fine-tuning a DistilBERT model (which is 40% smaller and 60% faster than BERT while retaining over 95% of its performance) on the IMDB dataset involves the following steps:

  1. Preprocessing: Using the AutoTokenizer to prepare text inputs and a DataCollatorWithPadding to convert samples into PyTorch tensors.
  2. Model Configuration: Replacing the pre-training head of DistilBERT with a classification head using AutoModelForSequenceClassification.
  3. Training: Utilizing the Trainer class to manage the training loop, learning rate, and evaluation metrics (such as accuracy and F1 score).

In a provided example using 3,000 samples from the IMDB dataset, this process achieved 88% accuracy and an 89% F1 score in approximately 10 minutes on a GPU.

No-Code Training with AutoNLP

AutoNLP is a tool that automatically trains, evaluates, and deploys NLP models without requiring code. Users upload their dataset, map the text and target columns, and AutoNLP handles hyperparameter tuning and model selection. In a test case using 3,000 samples from the Sentiment140 dataset, AutoNLP produced a model with 77.87% accuracy.

Practical Application: Analyzing Social Media Data

Sentiment analysis can be applied to real-time data streams, such as Twitter, to derive business insights. A practical workflow for analyzing tweets involves:

  1. Data Collection: Using the Tweepy library to interface with the Twitter API and extract tweets based on specific hashtags (e.g., #NFTs).
  2. Inference: Applying a Hub model, such as finiteautomata/bertweet-base-sentiment-analysis, via the pipeline class to label each tweet.
  3. Visualization: Using pandas for data organization and matplotlib or wordcloud to visualize sentiment distribution and key terms.

In a sample analysis of 1,000 tweets regarding NFTs, the results indicated that 56.1% of the conversation was positive, while only 2.0% was negative.

Sources