Ravindra BagaleCourses & study guides English Track your progress

52. Text Cleaning and TF-IDF in Python

Data Science

52.1 TF-IDF Intuition

52.1 TF-IDF intuition

Counts “the” आणि “refund” सारखे वागवतात. Rare meaningful words जास्त weight.

हे का शिकायचं?

Why. Counts “the” आणि “refund” ला सारखं वागवतात. Mauli reviews classify करताना rare, meaningful words जास्त weight घेतलं पाहिजे.

हे काय आहे?

What. TF-IDF = Term Frequency × Inverse Document Frequency. एका review मध्ये common पण corpus मध्ये rare → जास्त weight. sklearn चा TfidfVectorizer matrix बनवतो.

कसं करायचं?

How.

एक India story ने intuition

  1. “fresh” अनेक positive dairy reviews मध्ये → एकटं कमी distinctive.
  2. “sour” थोड्या reviews मध्ये → strong negative signal.
  3. TF-IDF boring common tokens down-weight; distinctive up-weight.
from sklearn.feature_extraction.text import TfidfVectorizer

docs = [
 "fresh pedha good packing",
 "sour dahi bad packing",
 "fresh dahi on time delivery",
]
vec = TfidfVectorizer()
X = vec.fit_transform(docs)
print(vec.get_feature_names_out())
print(X.toarray().round(2))

Fit on train only

Same rule as scalers (ch40): fit / fit_transform on training text; transform only on test text.