Ravindra BagaleCourses & study guides English Track your progress

51. What Is Natural Language Processing (NLP)

Data Science

51.4 Bag-of-Words Intuition

51.4 Bag-of-words intuition

Order बाजूला — word counts चा simple bag. TF-IDF ची foundation.

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

Why. Order बाजूला ठेवून word counts चा simple bag — TF-IDF आणि classical classifiers ची foundation.

हे काय आहे?

What. Bag-of-words = प्रत्येक document साठी “कोणता word किती वेळा”. Grammar deep parse नाही. Beginners साठी fast आणि explainable.

कसं करायचं?

How.

Manual mini bag

  1. दोन short reviews लिहा.
  2. Unique words list करा.
  3. प्रत्येक review साठी count vector कागदावर.
  4. y = sentiment label known from humans.
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer

docs = [
 "fresh pedha on time",
 "sour dahi late delivery",
 "fresh dahi good packing",
]
y = [1, 0, 1] # 1=positive, 0=negative (fictional)

vec = CountVectorizer()
X = vec.fit_transform(docs)
print(vec.get_feature_names_out())
print(X.toarray())
print("y:", y)

Ravindra Bagale's Tip

Bag-of-words only counts words, like items in a basket, and it ignores their order. So a phrase like "not good" is a hard case for it. Even so, it gives a strong baseline for many tasks such as tagging tickets, and TF-IDF in the next chapter makes it smarter.