pre-trained word embeddings
sklearn
machine learning
natural language processing
model training

How to make use of pre-trained word embeddings when training a model in sklearn?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

scikit-learn models expect fixed-size numeric feature vectors, while pre-trained word embeddings give you vectors per token. To use embeddings with sklearn, you usually transform each document into a single vector first, then feed those vectors into a normal classifier such as logistic regression, SVM, or a tree-based model.

Core Sections

The core limitation to understand

sklearn does not train neural embedding layers the way Keras or PyTorch does. It works with tabular feature matrices. That means pre-trained embeddings are used as feature engineering, not as a trainable embedding component inside the model.

A common pattern is:

  1. load a pretrained embedding model
  2. tokenize each document
  3. convert each token to a vector when available
  4. combine token vectors into one document vector
  5. train a normal sklearn estimator on those document vectors

A simple average-embedding baseline

The easiest document representation is the mean of all word vectors in the text.

python
1import numpy as np
2from sklearn.linear_model import LogisticRegression
3from sklearn.model_selection import train_test_split
4
5embeddings = {
6    "good": np.array([0.9, 0.1, 0.2]),
7    "bad": np.array([-0.8, 0.0, 0.1]),
8    "movie": np.array([0.1, 0.7, 0.3]),
9}
10
11def sentence_vector(text):
12    tokens = text.lower().split()
13    vectors = [embeddings[token] for token in tokens if token in embeddings]
14    if not vectors:
15        return np.zeros(3)
16    return np.mean(vectors, axis=0)
17
18texts = ["good movie", "bad movie", "good good movie"]
19y = [1, 0, 1]
20
21X = np.vstack([sentence_vector(text) for text in texts])
22X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
23
24model = LogisticRegression()
25model.fit(X_train, y_train)
26print(model.score(X_test, y_test))

This baseline is simple and often surprisingly strong for classification tasks.

A cleaner sklearn pipeline with a custom transformer

In real projects, wrap the embedding logic in a transformer so it fits naturally into Pipeline.

python
1import numpy as np
2from sklearn.base import BaseEstimator, TransformerMixin
3from sklearn.pipeline import Pipeline
4from sklearn.linear_model import LogisticRegression
5
6class MeanEmbeddingVectorizer(BaseEstimator, TransformerMixin):
7    def __init__(self, embeddings, dimension):
8        self.embeddings = embeddings
9        self.dimension = dimension
10
11    def fit(self, X, y=None):
12        return self
13
14    def transform(self, X):
15        rows = []
16        for text in X:
17            tokens = text.lower().split()
18            vectors = [self.embeddings[t] for t in tokens if t in self.embeddings]
19            if vectors:
20                rows.append(np.mean(vectors, axis=0))
21            else:
22                rows.append(np.zeros(self.dimension))
23        return np.vstack(rows)
24
25pipeline = Pipeline([
26    ("embed", MeanEmbeddingVectorizer(embeddings, 3)),
27    ("clf", LogisticRegression()),
28])

That keeps the embedding step reproducible and compatible with cross-validation.

Weighted averages are often better than plain means

Averaging all token vectors equally can make common words dominate the representation. A common improvement is to weight embeddings by TF-IDF scores so informative terms matter more.

The idea is:

  • fit a TfidfVectorizer
  • look up the IDF weight for each token
  • compute a weighted average of token embeddings

This still fits naturally into an sklearn transformer, and it often beats a naive mean without requiring a neural network.

Handle out-of-vocabulary words explicitly

Pretrained embeddings never cover every token in your corpus. Product names, typos, slang, and domain-specific jargon often fall outside the vocabulary. You need a policy:

  • ignore missing tokens
  • map them to zeros
  • use subword embeddings such as FastText

Ignoring the issue silently can produce empty document vectors for important examples.

When embeddings help and when they do not

Pre-trained embeddings help most when:

  • the training dataset is small
  • semantic similarity matters
  • synonyms should land near one another in feature space

They help less when the task depends heavily on word order, negation scope, or longer compositional structure. At that point, a neural model or transformer pipeline may be a better fit than forcing everything through fixed document vectors for sklearn.

Common Pitfalls

  • Expecting sklearn to train embedding layers directly the way a deep learning library would.
  • Averaging token vectors without deciding how to handle out-of-vocabulary words.
  • Forgetting that document vectors must have a fixed width for every sample.
  • Using pretrained embeddings but leaving tokenization inconsistent with how text was cleaned elsewhere in the pipeline.
  • Choosing embeddings for tasks where word order is critical and a bag-of-vectors summary is too weak.

Summary

  • In sklearn, pretrained embeddings are usually used as document-level features, not trainable layers.
  • The standard approach is to convert each text into one fixed-size vector, often by averaging token embeddings.
  • A custom transformer makes embedding-based features work cleanly with Pipeline and cross-validation.
  • Weighted averages and better OOV handling often outperform a naive plain mean.
  • If the task depends strongly on sequence structure, consider a neural NLP stack instead of forcing the problem into fixed vectors.

Course illustration
Course illustration

All Rights Reserved.