liveBook · Manning

Vectorization

As explained in Natural Language Processing in Action, Second Edition

A vectorizer is a function or component that transforms text into numerical vectors.

In natural language processing, a vectorizer (or encoder) transforms text into numerical vectors that computers can process. Vectorized operations also replace explicit Python loops with arithmetic on arrays or data frames. section 1.1.1 section 3.2.2

Text representations support different kinds of NLP work:

  • Token-count and term-frequency vectors represent word occurrence and can support keyword search, full-text search, and toxic-message detection. section 1.1.1
  • Embeddings represent semantics, supporting search based on what a query means rather than only which words occur. section 1.1.1

For numerical work, NumPy and pandas can perform array and data-frame arithmetic without a Python loop. This can greatly reduce runtime even when the algorithm’s big-O complexity is unchanged. section 3.2.2

Listing 3.5 Increasing the vectorized math in your code
>>> v1 = np.arange(5)         #1
>>> v2 = pd.Series(reversed(range(5)))

>>> slow_answer = sum([4.2 * (x1 * x2) for x1, x2 in zip(v1, v2)])
>>> slow_answer
42.0

>>> faster_answer = sum(4.2 * v1 * v2)    #2
>>> faster_answer
42.0

>>> fastest_answer = 4.2 * v1.dot(v2)      #3
>>> fastest_answer
42.0

Compare a loop, array arithmetic, and a dot product: each computes the same result, with increasingly vectorized code. section 3.2.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines text encoding and names count, term-frequency, and embedding vectors; demonstrates vectorized arithmetic but not vectorizer internals.
Examples
Compares loop-based arithmetic, array operations, and a dot product, then benchmarks vectorized and looping searches over HPR episodes.
What it assumes
Assumes Python, NumPy, and pandas for arithmetic examples while defining text encoding from raw text.
What you can do after
Apply NumPy and pandas arithmetic to compute dot products and replace explicit loops.
How it connects
Connects text vectors to NLP, NLU, embeddings, keyword search, full-text search, and semantic search.

Also explained in

Compare how 18 books explain vectorization →

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.