liveBook · Manning

CountVectorizer

As explained in Natural Language Processing in Action, Second Edition

CountVectorizer is a scikit-learn transformer that converts a collection of documents into a matrix of token occurrence counts.

CountVectorizer turns documents into bag-of-words vectors: each vocabulary token receives a position, and each document records how often that token appears. The resulting matrix preserves token counts without depending on word position and uses sparse storage to avoid wasting memory on zeros. section 2.5.3

Use the transformer in three practical steps:

  • Create and configure an empty CountVectorizer instance. section 3.2.1
  • Fit it to the corpus so it learns the vocabulary and token document frequencies. section 3.2.1
  • Transform the corpus into a sparse matrix of integer token counts; use max_df or min_df to filter overly common or rare tokens. section 3.2.1

Compute word-count vectors

from sklearn.feature_extraction.text import CountVectorizer corpus = [doc.text for doc in docs] vectorizer = CountVectorizer() count_vectors = vectorizer.fit(corpus).transform(corpus) print(count_vectors) print(count_vectors.toarray()) A sample corpus produces a 16-by-240 sparse matrix with 376 stored elements. Convert it to an array when inspecting individual rows, not when memory efficiency matters. section 3.2.1

Listing 3.3 Using sklearn to compute word count vectors
>>> import numpy as np
>>> from sklearn.feature_extraction.text import CountVectorizer
>>> np.set_printoptions(edgeitems=8)      #1
>>> corpus = [doc.text for doc in docs]
>>> vectorizer = CountVectorizer()             #2
>>> vectorizer = vectorizer.fit(corpus)          #3
>>> count_vectors = vectorizer.transform(corpus)   #4
>>> count_vectors
<16x240 sparse matrix of type '<class 'numpy.int64'>'
    with 376 stored elements in Compressed Sparse Row format>

Open the runnable listing for the complete scikit-learn workflow, including imports, corpus preparation, fitting, transforming, and sparse-matrix inspection. section 3.2.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains fitting, transforming, document-frequency filtering, and sparse storage, but leaves TF–IDF as a subsequent representation.
Examples
Uses a 16-document corpus, a 240-token vocabulary, dense-array inspection, Counter conversion, and bigram autocomplete.
What it assumes
Assumes familiarity with manually counted token vectors and requires a Python environment with scikit-learn and its sparse-matrix dependencies.
What you can do after
Apply it via scikit-learn to fit vocabularies and generate memory-efficient document-count matrices.
How it connects
Connects bag-of-words, sparse matrices, Python Counter objects, Transformer APIs, and TF–IDF.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.