Text Vectorization Lab 2D: Bag-of-Words vs TF-IDF

Switch to 3D
Vectorizer
BoW counts a word; TF-IDF multiplies the count by idf(w) = ln(N/(1+df)) + 1, so common words weigh less.
Dividing every axis by its own maximum cancels any per-word constant, so TF-IDF then gives exactly the BoW result. Raw weights keep the idf effect.
Nearest-centroid on the chosen features; the plot is always the X/Y projection.
Result
Click a point to inspect the tweet, its vector and its distance to each class centroid.
Numerical checks
The science

Each tweet d becomes a vector of word weights. Bag-of-Words: xw(d) = count(w, d). TF-IDF: xw(d) = count(w, d) · idf(w), where df(w) is the number of the N = 72 tweets that contain w.

idf(w) = ln( N / (1 + df(w)) ) + 1 class centroid mu_c = mean of x(d) over the 24 tweets of class c predict(d) = argmin_c || x(d) - mu_c ||

Between two centroids the boundary is the straight line where the two distances are equal, a perpendicular bisector; the coloured regions are exactly these nearest-centroid cells (a Voronoi diagram of the three centroids), which is why they are only drawn when the classifier sees just the two plotted words.

What the 3D page hides. The 3D page rescales every axis to its own maximum before classifying. Multiplying one axis by a constant (the idf of that word) and then dividing by its new maximum gives back the BoW axis, so on that page BoW and TF-IDF always classify identically. With raw weights the idf scale does change distances, so TF-IDF can win or lose a few tweets; try the presets with both scaling buttons.

Simplifications. The corpus is 72 short synthetic tweets (24 per class), accuracy is measured on the same tweets the centroids are computed from (training accuracy, optimistic), there is no stemming, and the nearest-centroid rule is far simpler than the Random Forest or Logistic Regression used on real corpora. Real TF-IDF variants also differ in the idf smoothing and in length normalisation, which is not applied here.

Things to try
  • Choose the third preset and switch scaling: BoW and TF-IDF agree under per-axis max scaling and differ under raw weights.
  • Set the classifier to the whole vocabulary: accuracy jumps because far more words carry the class signal than any two axes.
  • Pick "today" and "work" as axes: these frequent words get a low idf and TF-IDF squeezes them on the plot.