53. Sentiment Analysis Project in Python
53.4 Traps and Guardrails
Why. Real mistakes: fitting TF-IDF on all text, skipping stratify, celebrating 90% accuracy on 10 rows, or shipping Hinglish noise without a baseline check.
What. Guardrails from earlier chapters still apply: split first, metrics beyond accuracy when classes skew, and honest demo size.
How.
Avoid these traps
- Never
fitthe vectoriser on train+test together. - Keep
random_statefor reproducible demos. - On tiny sets, treat metrics as smoke tests.
- Add more labelled lines before business decisions.
# BAD: leaks test vocabulary into the vectoriser
# vec.fit_transform(docs) # then split — do not do this
# GOOD: split first, then fit on X_train only (see 53.2)
print("Remember: split → fit vectoriser on train → transform test")