liveBook · Manning

Vector space

As explained in Natural Language Processing in Action, Second Edition

A vector space is a mathematical set of vectors that can be combined through operations such as addition and scalar multiplication.

A vector space is a space for representing vectors: ordered coordinates that encode position, direction, magnitude, distance, or features. A two-coordinate vector such as [latitude, longitude] belongs to a 2D vector space, while a document’s term-frequency vector occupies one dimension per chosen term. section 3.2.3

Coordinate order is part of the meaning. Swapping latitude and longitude, or rearranging entries in a term-frequency vector, changes the represented point and can invalidate later calculations. Rectilinear spaces use perpendicular dimensions, allowing Euclidean distance to measure the straight-line separation between vectors. section 3.2.3

Vector spaces make language representations computable:

  • Term-frequency and TF–IDF vectors place documents in spaces where dot products and distances support similarity calculations. section 3.2.2 section 3.2.3
  • Word2Vec maps token-count representations into a lower-dimensional continuous space, where nearby vectors often correspond to related words. section 6.3.1
  • Vector arithmetic can express analogies: Portland Timbers + Seattle − Portland produces a vector near Seattle Sounders, even when the result is not exactly in the vocabulary. section 6.3.1
Listing 3.8 2D term frequency vector examples
>>> from matplotlib import pyplot as plt
>>> import seaborn as sns

>>> palette = sns.color_palette("muted")    #1
>>> sns.set_theme()                              #2

>>> vecs = pd.DataFrame([[1, 0], [2, 1]], columns=['x', 'y'])
>>> vecs['color'] = palette[:2]
>>> vecs['label'] = [f'vec{i}' for i in range(1, len(vecs)+1)]

>>> fig, ax = plt.subplots()
>>> for i, row in vecs.iterrows():
...     ax.quiver(0, 0, row['x'], row['y'], color=row['color'],
...         angles='xy', scale_units='xy', scale=1)
...     ax.annotate(row['label'], (row['x'], row['y']),
...         color=row['color'], verticalalignment='top'
...         )
>>> plt.xlim(-1, 3)
>>> plt.ylim(-1, 2)
>>> plt.xlabel('X (e.g. frequency of word "vector")')
>>> plt.ylabel('Y (e.g. frequency of word "space")')
>>> plt.show()

Plotting [1, 0] and [2, 1] as arrows makes dimensions, direction, magnitude, and coordinate order visible before scaling to high-dimensional NLP vectors. section 3.2.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains coordinates, dimensionality, ordering, Euclidean geometry, dot products, and embedding analogies without developing formal axioms.
Examples
Uses latitude–longitude coordinates, graph-paper geometry, plotted term-frequency vectors, document vectors, and a sports-team analogy.
What it assumes
Builds from coordinates and familiar maps before using linear algebra operations, assuming no advanced formal treatment of vector spaces.
What you can do after
Apply vector representations to calculate similarity, preserve feature ordering, visualize low-dimensional points, and interpret embedding arithmetic.
How it connects
Connects vectors with linear algebra, dot products, Euclidean distance, TF–IDF, word embeddings, and Word2Vec.

Also explained in

Compare how 4 books explain vector space →

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.