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
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
# 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
# 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
# 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
# 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}
}),
]
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX