Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 54

PCA for Data Visualization

Recommended resources

Hello! Welcome to the final lesson in our module on unsupervised learning.

In our last session, we dug into the concept of explained variance. You learned how to use scree plots and cumulative variance plots to determine how much information is retained after dimensionality reduction. This is a critical step to ensure that when we simplify our data, we don't lose its essential story.

Today, we'll complete the process. Our goal is to visualize high-dimensional data in two dimensions using PCA. We'll take what we learned about explained variance as our diagnostic check and then proceed to create, plot, and interpret the 2D representation of a complex dataset. This technique is a cornerstone of exploratory data analysis, allowing you to "see" the structure of data that would otherwise be impossible to visualize.

Let's get started.

1. The Goal: From High Dimensions to a 2D Plot

The core idea is to project data from a high-dimensional space (e.g., a dataset with 30, 64, or even thousands of features) onto a two-dimensional plane. This plane is defined by the first two principal components (PC1 and PC2), which, as we know, are the axes that capture the most variance in the data.

Principal Component Analysis (PCA) Transformation for Dimensionality Reduction
This image illustrates the process. The original data exists in a higher-dimensional space (left, shown in 3D). PCA finds the 2D plane (defined by PC1 and PC2) that best represents the data. The data points are then projected onto this plane, creating a 2D scatter plot (right) that we can easily visualize and interpret.

The workflow to achieve this is straightforward and builds on what we've already covered:

  1. Standardize the Data: As PCA is based on variance, you must scale your features so that each one has a mean of 0 and a standard deviation of 1. This prevents features with naturally large scales from dominating the analysis.
  2. Apply PCA: Instantiate a PCA object from scikit-learn, specifically requesting n_components=2.
  3. Transform: Use the .fit_transform() method on your scaled data to generate the new 2D coordinates for each data point.
  4. Visualize: Create a scatter plot of the two new dimensions (PC1 and PC2). To make the plot interpretable, we can color-code the points using their original labels (e.g., class categories), even though these labels were not used in the unsupervised PCA process itself.

2. A Practical Walkthrough: PCA on the Breast Cancer Dataset

Let's see this workflow in action. We'll watch a tutorial that applies PCA to a real-world dataset from scikit-learn. The breast cancer dataset has 569 samples and 30 features, making it impossible to visualize directly. We'll see how PCA can reduce it to just two dimensions.

Principle Component Analysis (PCA) using sklearn and python

This video from Krish Naik provides a concise, code-first demonstration of using PCA for visualization. It covers all the key steps from loading the data to interpreting the final plot.

Please watch the following segments: Introduction and Data Loading (00:00 - 03:23): Understand the dataset and its high dimensionality (30 features). Standard Scaling (04:48 - 06:15): Observe the critical step of scaling the features before applying PCA. Applying PCA (06:15 - 09:28): See how to initialize PCA(n_components=2) and transform the 30-dimensional data into a (569, 2) array. Plotting and Interpretation (10:08 - 11:45): Watch how the transformed 2D data is plotted and how class labels are used to color the points, revealing distinct clusters.

As you saw in the video, the final 2D plot effectively separates the two classes of tumors (malignant and benign). This is a powerful insight! It tells us that the original 30 features, when combined into two principal components, contain significant information that distinguishes between the classes.


3. A Second Example: Visualizing Handwritten Digits

To solidify your understanding, let's look at another classic example. The digits dataset contains images of handwritten digits (0 through 9). Each image is 8x8 pixels, which are flattened into a 64-dimensional vector. Our goal is to see if we can visualize the relationships between these 64-dimensional points in a 2D plot.

The following resource from the Python Data Science Handbook provides the code and a clear explanation.

In Depth: Principal Component Analysis | PCA for visualization: Hand-written digits

This text provides a clear, self-contained example of using PCA to visualize the high-dimensional digits dataset. It's a great reference for the code and the interpretation.

Please read the section titled 'PCA for visualization: Hand-written digits'. Pay close attention to the code that initializes PCA(2) and then uses fit_transform on the digits.data. Analyze the resulting scatter plot. Notice how the points are colored by their actual digit label (digits.target). Read the interpretation that follows the plot. It explains how PCA has found an optimal 2D projection for viewing the 64-dimensional data.


4. Interpreting Your PCA Visualization

Creating the plot is only half the battle. The real value comes from interpretation. When you look at a 2D PCA plot, you should be asking:

  • Are there clusters? Do data points with the same label group together? In both the breast cancer and digits examples, we saw clear clustering. This is a strong indicator that your features are useful for separating the classes.
  • How separated are the clusters? Clear separation (like in the breast cancer dataset) suggests that a machine learning classifier would likely perform well. Overlapping clusters (like between the digits 1 and 8, for example) suggest those classes are harder to distinguish based on the information captured by the first two principal components.
  • Are there outliers? A point that is far away from all clusters might be an outlier or a data entry error worth investigating.

Remember from our last lesson, before settling on a 2D visualization, you should always check the cumulative explained variance. If PC1 and PC2 together only explain, say, 30% of the variance, your 2D plot is a poor and potentially misleading representation of the data. If they explain 70-90% or more, you can be much more confident in the insights you draw from the visualization.


Conclusion

You've now mastered a powerful technique for data exploration. By combining PCA for dimensionality reduction with scatter plots for visualization, you can gain intuitive insights into the structure of high-dimensional data, a task that is fundamental to practical data science.

Key Takeaways:

  • The workflow for visualizing high-dimensional data is: Standardize Data → Apply PCA(n=2) → Transform Data → Create a Scatter Plot.
  • Coloring the points in the scatter plot by their original class labels helps you visually assess class separability.
  • The presence and separation of clusters in the 2D plot give you a strong initial hypothesis about whether your features can be used to build an effective classification model.
  • Always check the explained variance of your first two components to ensure your 2D plot is a meaningful representation of the data.

Preview of the next lesson:
Congratulations, you've completed the module on Unsupervised Learning! You've covered clustering with K-Means and dimensionality reduction with PCA.

In the next module, we'll begin your first End-to-End Data Science Project. You will apply everything you've learned so far—from EDA and preprocessing to model training and evaluation, including techniques like PCA—to solve a problem from scratch. It's time to put your foundational skills into practice