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.
Setup
Code
Code
Generating 3D Gaussian Data
First, let’s create a 3D Gaussian distribution with a specific covariance structure:Code
Code
Visualizing 3D Data
Code
Code
Computing Principal Components via SVD
We can find the principal components by performing Singular Value Decomposition on the covariance matrix:Code
Code
2D Projection
Code
Code
Verifying Decorrelation
A key property of PCA is that the projected data has uncorrelated components:Code
Code
Visualizing the Principal Plane in 3D
We can visualize how the 2D projection relates to the original 3D space:Code
Code
Using Scikit-Learn PCA
We can also use scikit-learn’s PCA implementation:Code
Code
Visualizing Principal Axes
Code
Code
Input vs Principal Components Comparison
Code
Code
Key Insights
- Decorrelation: PCA transforms correlated variables into uncorrelated principal components
- Variance Maximization: The first principal component captures the direction of maximum variance
- Orthogonality: Principal components are orthogonal to each other
- Dimensionality Reduction: We can project high-dimensional data onto a lower-dimensional subspace while preserving maximum variance
References
- Python Data Science Handbook by Jake VanderPlas
- Scikit-Learn PCA Documentation
Found an error on this page? Report an issue.

