Skip to main content

Ngram (Sequential)

Description

The Ngram policy implements a simple, frequency-based sequential model. It predicts the next item based on the conditional probability derived from counts of the immediately preceding n-1 items (the n-gram context). It's effective at capturing short-term co-occurrence patterns in user behavior sequences.

Policy Type: ngram Supports: scoring_policy

Premium Model

This model requires the Standard Plan or higher.

Hyperparameter tuning

  • event_values: List of event value strings to filter interactions by.
  • n: Length of n-grams (sequences of n consecutive items).
  • laplace_smoothing: Laplace smoothing parameter to handle unseen n-grams.

V1 API

policy_configs:
scoring_policy:
policy_type: ngram
n: 3 # Sequence length (e.g., 3 for trigrams - uses last 2 items)
laplace_smoothing: 0.05 # Smoothing factor to handle unseen sequences (avoids zero probability)

Usage

Use this model when:

  • You want a simple, frequency-based baseline for sequential or session-based recommendations
  • You have sparse interaction data where complex neural sequential models may overfit
  • You primarily care about short-term co-occurrence patterns in recent interactions
  • You need a fast, interpretable sequential model with minimal training complexity

Choose a different model when:

  • You need to capture long-range dependencies or complex temporal patterns (use SASRec, GSASRec, or BERT4Rec)
  • You have dense sequential data and can benefit from more expressive neural models
  • You need general item similarity or retrieval embeddings rather than next-item prediction (use Two-Tower, ALS, or Item2Vec)
  • You primarily want non-personalized trending content (use Rising Popularity)

Use cases

  • Session-based recommendations on small or medium-sized sites
  • Next-click or next-view prediction using only a few recent interactions
  • Baseline models for evaluating more sophisticated sequential approaches
  • Educational or low-traffic environments where interpretability and simplicity are preferred

References

  • Concept based on N-gram language models. See: Jurafsky, D., & Martin, J. H. (2009). Speech and Language Processing. Prentice Hall.
  • Wikipedia: Word n-gram language model