Sub topic 3
Lowercasing and Stop Words
Assume you know basic Python, NumPy vectors, and how a text vectorizer creates features. This chapter focuses on two preprocessing choices: lowercasing text and removing stop words.
Lowercasing text
Lowercasing maps uppercase and lowercase forms to the same representation. Without it, Movie, movie, and MOVIE become separate vocabulary terms. Lowercasing usually reduces the number of features and prevents capitalization from splitting evidence across columns.
For ordinary sentiment or topic classification, this is often useful. However, case can carry meaning. US and us, or product codes such as AB12, may need to remain distinct. Use lowercase=False when capitalization is meaningful.
Stop words
Stop words are frequent words that often contribute little to a particular task. English examples include the, a, is, and on. Removing them can reduce vocabulary size and computation.
Stop-word removal is not automatically beneficial. A word can be common but still important. Removing not can change the meaning of a sentiment phrase:
goodexpresses positive sentiment.not goodexpresses negative sentiment.
The right list depends on the task, language, and tokenizer. Inspect the vectorizer's learned vocabulary instead of assuming that removal improved the features.
from sklearn.feature_extraction.text import TfidfVectorizer
docs = [
"The movie was not good",
"A good movie can still be surprising",
]
for remove in [None, "english"]:
vectorizer = TfidfVectorizer(
lowercase=True,
stop_words=remove,
)
X = vectorizer.fit_transform(docs)
print(f"stop_words={remove!r}")
print(vectorizer.get_feature_names_out())
print(X.toarray().round(2))With lowercase=True, The and the share one feature. With stop_words="english", common English words are removed. Check whether words such as not remain suitable for your version and task. For sentiment work, consider a custom list that preserves negation words.
How the choices affect features
| Choice | Benefit | Main risk |
|---|---|---|
| Lowercase text | Merges case variants | Loses case-based meaning |
| Remove stop words | Fewer features and less noise | Removes useful words or negation |
Before choosing either option, compare validation results and inspect errors. A smaller vocabulary is not automatically a better representation.
Exercise
Why might removing every common English word hurt a sentiment classifier?
Show answer
It may remove meaningful words, especially negation terms such as “not,” so phrases like “not good” can lose their negative meaning.