liveBook · Manning

Principal component analysis

As explained in Natural Language Processing in Action, Second Edition

Principal component analysis is a statistical technique that transforms correlated variables into a smaller set of orthogonal components capturing as much variance as possible.

A dataset can have many measurements that partly repeat one another. When two features vary together, their covariance signals that some of their information may fit along a single direction. Principal component analysis (PCA) finds new, perpendicular axes along which the data varies most, then projects the data onto a chosen number of those axes. The first component captures the greatest variance; the next captures the greatest remaining variance. Keeping fewer components makes the data easier to work with, but discards variation. section 4.3.1

Turn a point cloud into a plot

A three-dimensional point cloud can be projected onto two components with `PCA(n_components=2).fit_transform(df)` from scikit-learn. Plotting the resulting x and y columns gives a view aligned with the cloud’s two strongest directions of variation—like choosing an angle for a photograph, rather than simply dropping one original coordinate. section 4.3.1

Listing 4.7 PCA magic
>>> import pandas as pd
>>> from sklearn.decomposition import PCA
>>> import seaborn
>>> from matplotlib import pyplot as plt

>>> DATA_DIR = ('https://gitlab.com/tangibleai/nlpia/'
...             '-/raw/master/src/nlpia/data')

>>> df = pd.read_csv(DATA_DIR + '/pointcloud.csv.gz', index_col=0)
>>> pca = PCA(n_components=2)                                          #1
>>> df2d = pd.DataFrame(pca.fit_transform(df), columns=list('xy'))
>>> df2d.plot(kind='scatter', x='x', y='y')
>>> plt.show()

The listing loads a point cloud, fits two components, and plots the projected coordinates. section 4.3.1

Singular value decomposition (SVD) is the matrix procedure behind this approach. A full decomposition can reconstruct the original matrix; a truncated decomposition keeps only selected leading dimensions. For sparse bag-of-words or TF–IDF document matrices, scikit-learn’s `TruncatedSVD` is often a better practical choice than `PCA`: PCA centers columns first, while centering sparse data can make it dense, slower, and more memory-intensive. Centering before `TruncatedSVD` can produce the same results as PCA, but loses that sparse-data advantage. section 4.3.2 section 4.4.2

Map word-vector relationships

PCA can also project 300-dimensional city word vectors into a two-dimensional bubble plot. Nearby points can suggest relationships learned from language, not physical proximity: cities similar in culture may cluster despite being far apart. Treat the plot as a view of the vectors, not an accurate geographic map; a different training corpus may produce different relationships. section 6.4.5

Listing 6.11 Bubble chart of US cities
>>> from sklearn.decomposition import PCA
>>> pca = PCA(n_components=2)                   #1
>>> us_300D = get_data('cities_us_wordvectors')
>>> us_2D = pca.fit_transform(us_300D.iloc[:, :300])    #2

The listing applies scikit-learn PCA to the first 300 columns of a city-vector dataset to produce 2D coordinates. section 6.4.5

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains variance, covariance, ordered components, SVD, and the practical effect of centering; leaves detailed decomposition algorithms aside.
Examples
Uses a three-dimensional point cloud and a two-dimensional plot of 300-dimensional US city word vectors.
What it assumes
Builds from covariance and geometric axes while using Python data frames and assuming familiarity with document vectors for the NLP comparison.
What you can do after
Apply it via scikit-learn PCA to project data, and choose TruncatedSVD when centering a sparse TF–IDF matrix would be costly.
How it connects
Connects PCA to covariance, singular value decomposition, truncated SVD, TF–IDF, and word embeddings.

Also explained in

Compare how 10 books explain principal component analysis →

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.