Skip to main content
This section contains an excerpt from the Python Data Science Handbook by Jake VanderPlas with additional 3D visualization examples. The text is released under the CC-BY-NC-ND license, and code is released under the MIT license.

Run in Google Colab

Open this tutorial in Google Colab to execute the code interactively.
This section demonstrates PCA using 3D data, showing how principal component analysis finds the directions of maximum variance and projects data onto lower-dimensional subspaces.

Setup

Generating 3D Gaussian Data

First, let’s create a 3D Gaussian distribution with a specific covariance structure:

Visualizing 3D Data

Computing Principal Components via SVD

We can find the principal components by performing Singular Value Decomposition on the covariance matrix:

2D Projection

Verifying Decorrelation

A key property of PCA is that the projected data has uncorrelated components:

Visualizing the Principal Plane in 3D

We can visualize how the 2D projection relates to the original 3D space:

Interactive 3D Visualization with Plotly

For interactive exploration, we can use Plotly:

Using Scikit-Learn PCA

We can also use scikit-learn’s PCA implementation:

Visualizing Principal Axes

Input vs Principal Components Comparison

Key Insights

  1. Decorrelation: PCA transforms correlated variables into uncorrelated principal components
  2. Variance Maximization: The first principal component captures the direction of maximum variance
  3. Orthogonality: Principal components are orthogonal to each other
  4. Dimensionality Reduction: We can project high-dimensional data onto a lower-dimensional subspace while preserving maximum variance

References