Ravindra BagaleCourses & study guides English Track your progress

52. Text Cleaning and TF-IDF in Python

Data Science

52.2 TfidfVectorizer Beginner Knobs

52.2 TfidfVectorizer beginner knobs

Stop words, n-grams, min_df — knobs quality बदलतात.

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

Why. Vectoriser settings quality बदलतात: stop words, n-grams, min document frequency. Drown न होता knobs माहीत असाव्यात.

हे काय आहे?

What. Useful knobs: stop_words="english", bigrams साठी ngram_range=(1,2) (“not good”), ultra-rare typos काढायला min_df, tiny laptop वर columns cap करायला max_features.

कसं करायचं?

How.

Safer beginner config try करा

  1. Unigrams + bigrams ने सुरू.
  2. English-heavy text साठी English stop words drop.
  3. Practice data वर features cap (उदा. 500).
  4. Always split train/test before fitting the vectoriser.
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split

docs = [
 "fresh pedha good packing",
 "not good packing torn box",
 "sour dahi late delivery",
 "fresh dahi on time",
 "bad smell sour tray",
 "sweet pedha excellent",
]
y = [1, 0, 0, 1, 0, 1]

X_train, X_test, y_train, y_test = train_test_split(
 docs, y, test_size=0.33, random_state=42, stratify=y
)

vec = TfidfVectorizer(
 stop_words="english",
 ngram_range=(1, 2),
 min_df=1,
 max_features=500,
)
X_train_m = vec.fit_transform(X_train)
X_test_m = vec.transform(X_test)
print("Train shape:", X_train_m.shape, "Test shape:", X_test_m.shape)
print("Sample features:", list(vec.get_feature_names_out())[:8])