liveBook · Manning

Projection

As explained in Natural Language Processing in Action, Second Edition

A projection is the mapping of a vector onto a specified line or axis to obtain its component along that direction.

A projection maps a vector onto a chosen line or axis, preserving only its component along that direction. In practical machine learning, this turns a high-dimensional vector into a score that can support comparison or classification. section 4.2.1

For two-class linear discriminant analysis, the useful direction runs between the toxic and nontoxic class centroids. Projecting each TF–IDF vector onto that centroid axis produces a toxicity score; normalizing the scores and applying a cutoff converts them into predictions. section 4.2.1

The same operation appears in attention systems:

  • A linear projection is a dot product or matrix multiplication that transforms embedding vectors into query, key, and value vectors. section 9.1.2
  • Dot products between queries and keys create attention scores; scaling by the key dimension improves numerical stability before softmax produces weights. section 9.1.2

Projecting comments onto a toxicity axis

Compute toxic and nontoxic TF–IDF centroids, subtract them to form an axis, and calculate every comment's score with one vectorized dot product. A MinMaxScaler and a 0.5 cutoff then produce manual predictions. The resulting classifier illustrates both the usefulness and limitation of the approach: training accuracy reaches 0.999, while held-out accuracy falls to 0.554. section 4.2.1 section 4.2.1

>>> mask = comments.toxic.astype(bool).values    #1
>>> toxic_centroid = tfidf_docs[mask].mean(axis=0)    #2
>>> nontoxic_centroid = tfidf_docs[~mask].mean(axis=0)   #3
>>> centroid_axis = toxic_centroid - nontoxic_centroid
>>> toxicity_score = tfidf_docs.dot(centroid_axis)     #4
>>> toxicity_score.round(3)
array([-0.008, -0.022, -0.014, ..., -0.025, -0.001, -0.022])

Inspect the NumPy calculation that builds the centroid axis and projects all TF–IDF vectors into toxicity scores. section 4.2.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines projection and demonstrates its computation, while deferring the underlying LDA mathematics and focusing attention on operational linear algebra.
Examples
Demonstrates centroid-axis toxicity scores for comments and query, key, and value transformations in self-attention.
What it assumes
Assumes vectors, TF–IDF features, centroids, and dot products, while explaining projection through classification and attention examples.
What you can do after
Apply it via NumPy dot products to score vectors, or understand how attention creates query, key, and value representations.
How it connects
Connects projection with LDA, TF–IDF, cosine distance, centroids, self-attention, query-key scoring, softmax, and value vectors.

Also explained in

Compare how 6 books explain projection →

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.