Intermediate

Text Classification with spaCy

Build and train text classifiers for sentiment analysis, spam detection, and topic categorization using spaCy's built-in TextCategorizer component.

TextCategorizer Overview

spaCy provides two text classification architectures:

📊

textcat

Single-label classification. Each document gets exactly one label (e.g., positive OR negative).

🏷

textcat_multilabel

Multi-label classification. Documents can have multiple labels simultaneously (e.g., "sports" AND "politics").

Preparing Training Data

Python - Create training data for text classification
import spacy
from spacy.tokens import DocBin

nlp = spacy.blank("en")
db = DocBin()

TRAIN_DATA = [
    ("This product is absolutely amazing!", {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}),
    ("Terrible quality, waste of money.", {"cats": {"POSITIVE": 0.0, "NEGATIVE": 1.0}}),
    ("Best purchase I ever made!", {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}),
    ("Broke after one day. Very disappointed.", {"cats": {"POSITIVE": 0.0, "NEGATIVE": 1.0}}),
    ("Works exactly as described. Happy customer.", {"cats": {"POSITIVE": 1.0, "NEGATIVE": 0.0}}),
]

for text, annotations in TRAIN_DATA:
    doc = nlp.make_doc(text)
    doc.cats = annotations["cats"]
    db.add(doc)

db.to_disk("./train.spacy")

Configuration

Terminal - Generate and customize config
# Generate base config for text classification
python -m spacy init config config.cfg --lang en --pipeline textcat

# Fill in defaults
python -m spacy init fill-config config.cfg config.cfg

Training and Evaluation

Terminal - Train the classifier
# Train the model
python -m spacy train config.cfg \
    --output ./output \
    --paths.train ./train.spacy \
    --paths.dev ./dev.spacy

# Evaluate on test data
python -m spacy evaluate ./output/model-best ./test.spacy

Using the Trained Model

Python - Predict with trained classifier
# Load trained model
nlp = spacy.load("./output/model-best")

texts = [
    "I love this product, it works perfectly!",
    "Terrible experience, never buying again.",
    "It's okay, nothing special.",
]

for text in texts:
    doc = nlp(text)
    prediction = max(doc.cats, key=doc.cats.get)
    confidence = doc.cats[prediction]
    print(f"{text[:40]:42} => {prediction} ({confidence:.2f})")

Multi-Label Classification

Python - Multi-label topic classification
# For multi-label, use textcat_multilabel in config
# Each document can have multiple labels

TRAIN_DATA = [
    ("New AI chip breaks speed records", {
        "cats": {"TECH": 1.0, "BUSINESS": 0.5, "SPORTS": 0.0}
    }),
    ("Lakers win championship in overtime thriller", {
        "cats": {"TECH": 0.0, "BUSINESS": 0.0, "SPORTS": 1.0}
    }),
    ("Tech startup raises $50M for AI healthcare", {
        "cats": {"TECH": 1.0, "BUSINESS": 1.0, "SPORTS": 0.0}
    }),
]
When to use spaCy vs Transformers for classification: Use spaCy's TextCategorizer when you need fast inference, small model size, or are integrating with other spaCy pipeline components. Use Hugging Face Transformers when you need maximum accuracy on complex tasks.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.