what is the difference between bigram and unigram text features extraction
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In the realm of natural language processing (NLP) and text analysis, feature extraction is a critical step for converting textual content into numerical representations that machine learning algorithms can effectively process. Among the foundational techniques for text feature extraction are unigram and bigram models. Understanding the differences between these methods is crucial for selecting the right approach for a given application.
Understanding Unigrams and Bigrams
Unigrams
A unigram is a single word or token in a text. When text features are extracted using a unigram model, each word in the corpus is treated as an independent feature. This means the model considers the frequency of each word without regard to any contextual information from surrounding words.
Example
Consider the sentence: "The cat sat on the mat."
• Unigrams: `["The", "cat", "sat", "on", "the", "mat"]`
In this case, each word becomes a feature, and the text representation focuses on individual word occurrences.
Bigrams
A bigram consists of two consecutive words (tokens) in a text. A bigram model captures some level of context by considering word pairs together. This can be particularly useful for detecting common phrases or sequences words often appear in.
Example
Using the same sentence: "The cat sat on the mat."
• Bigrams: `[("The", "cat"), ("cat", "sat"), ("sat", "on"), ("on", "the"), ("the", "mat")]`
Here, each pair of words is treated as a feature, capturing relationships between adjacent words.
Technical Explanation
Unigram Model
In the unigram model, each sentence is transformed into a sparse vector where the dimensionality equals the vocabulary size. The vector comprises counts (or frequencies) of each word in the sentence with respect to the whole dataset.
Mathematical Representation
Suppose the vocabulary . For any given document , a unigram feature vector can be represented as:
where signifies the frequency of word in document .
Bigram Model
In the bigram model, the focus shifts to sequences of two consecutive words. Similar to unigrams, a bigram vector represents the frequency of each bigram in the document.
Mathematical Representation
For the vocabulary , a bigram vocabulary is constructed as . For a document , the bigram feature vector is:
where denotes the frequency of bigram in document .
Comparison Table
| Feature Extraction Method | Definition | Contextual Information | Example | Computational Complexity |
| Unigram | Single word as feature | Minimal | "The", "cat" | Lower: Linear in terms of number of words |
| Bigram | Pair of words as feature | Moderate | "The cat", "sat on" | Higher: Linear in terms of number of word pairs |
Applications and Considerations
Applications
• Unigrams: Suitable for applications where individual word importance prevails, such as simple document classification or sentiment analysis based on single-word sentiment lexicons. • Bigrams: Ideal for capturing more contextual nuances, as in phrase detection, more nuanced sentiment analysis, and language modeling where certain word combinations carry specific meanings.
Considerations
• Sparsity: Bigram models result in a much larger feature space, which may lead to increased sparsity and require more data to train effectively. • Interpretability: Unigram models are often more interpretable as they directly map to individual words. • Performance: Depending on the dataset and specific task, the choice between unigram and bigram can significantly affect the performance. For instance, in tasks benefiting from phrase detection, bigrams can outperform unigrams.
Conclusion
Understanding the differences between unigram and bigram feature extraction methods allows for more informed decisions in text processing tasks. While unigrams focus on individual word occurrences, bigrams offer insight into word sequences and context. Each approach has its strengths and is best suited for different types of text analysis problems, making feature selection a paramount step in any NLP workflow.
Related reading
- What is the difference between keras.tokenize.text_to_sequences and word embeddings
- What is the good metric to evaluate NER model trained in Spacy
- What is the network structure inside a Tensorflow Embedding Layer?
- What is the preferred ratio between the vocabulary size and embedding dimension?
- What is the difference between binary crossentropy and binary crossentropy with logits in keras?
- What is the difference between causal models and directed graphical models?
- What is the purpose of weights and biases in tensorflow word2vec example?
- What is UNK Token in Vector Representation of Words
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.