Counting With CountVectorizer
Turn documents into a count matrix.
Counting With CountVectorizer is a free NLP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet CountVectorizer
The CountVectorizer from scikit-learn builds your vocabulary and counts words for you in just a few lines. 🚀
from sklearn.feature_extraction.text import CountVectorizerCreate the Vectorizer
First make an instance. With no arguments it uses sensible defaults for tokenizing and lowercasing text.
vectorizer = CountVectorizer()Fit Learns the Vocabulary
Calling fit scans your documents and discovers every unique word, building the vocabulary automatically.
vectorizer.fit(docs)Transform Produces Counts
Then transform turns each document into its count vector, giving you a numeric matrix of word frequencies.
X = vectorizer.transform(docs)Fit and Transform Together
The shortcut fit_transform does both steps at once on your training text, which is the common workflow.
X = vectorizer.fit_transform(docs)See the Vocabulary
Inspect the learned words and their indices with get_feature_names_out. These are your matrix columns.
print(vectorizer.get_feature_names_out())Output Is a Sparse Matrix
To save memory, the result is a sparse matrix that stores only the non-zero counts, not every zero slot.
Peek at the Numbers
Convert to a dense array to actually read the counts. Use toarray for small examples only.
print(X.toarray())Lowercasing Is Automatic
By default it lowercases text and splits on word boundaries, so The and the count as the same token.
Built-In Cleaning Options
You can pass stop_words, min_df, or max_features to prune the vocabulary right inside the vectorizer.
CountVectorizer(stop_words="english", max_features=1000)Reuse on New Text
Never refit on test data. Call only transform so new documents use the exact same vocabulary you trained.
X_new = vectorizer.transform(new_docs)Quick Check
Which call learns the vocabulary and counts in one step?
Recap: CountVectorizer
You used CountVectorizer to fit a vocabulary, transform text into counts, inspect features, and reuse it on new data. 🎉
Frequently asked questions
Is the “Counting With CountVectorizer” lesson free?
Yes — the full text of “Counting With CountVectorizer” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Counting With CountVectorizer”?
Turn documents into a count matrix. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NLP Academy?
No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Counting With CountVectorizer” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NLP Academy lesson?
Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Models Need Numbers, Not Words
- Building a Vocabulary
- Counting With CountVectorizer
- Reading the Document-Term Matrix