Principal Component Analysis for Dimensionality Reduction
The row is what a reduction method receives.
Starting From a Surprising Plot
This article works in the opposite direction from most explanations of t-SNE. Instead of starting with the mathematics, you start with a small Python program that reduces a dataset of handwritten digits to two dimensions, and then you use the program's output to ask what the result means. Each step of the program is a question about the data: what does the input look like, what does a simpler method produce from the same input, and what does t-SNE produce that differs from it?
What do you think happens?
You have a fitted t-SNE model and a new digit image that was not in the original data. Can you call transform on the model to place the new image on the existing 2D map?
Reveal answer
No, the TSNE class has no transform method
The source states that t-SNE does not support transforming new data. The call that produces its two-dimensional output is fit_transform, which builds the model and returns the transformed data in one step.
The habit you will build here matters more than any single setting: keep track of where each move in the program happens. When a result surprises you, the location of the surprise tells you which step to examine. The program runs in three moves. First you load the images, then you reduce them with PCA as a baseline, and then you reduce them with t-SNE.
Loading Digits as Pictures and Rows
The digits dataset is included in scikit-learn, so you load it with a single function call. Every data point in it is an 8 by 8 grayscale image of a handwritten digit between 0 and 9. The dataset also carries a label for each image that tells you which digit it shows. Those labels are used only for coloring and checking the plots, because t-SNE itself never reads them.
Think of each data point as a picture first and a row of numbers second. The picture is what you look at when you draw a digit. The row is what a reduction method receives. Each image contributes 64 pixel values, and those values are what the methods work with. When PCA or t-SNE finishes, that same image has become a point with just two coordinates.
PCA as the Linear Baseline
Before trying the new method, the program builds a baseline with PCA, which is a linear dimensionality reduction technique. The PCA model is fitted on the digits data, and then the same data is transformed onto the first two principal components. Each image, which has 64 pixel values, becomes a point with two coordinates. Because the two output axes are principal components, you can keep working with the fitted model and use those axes again.
- 1Load the digits dataset so that
digits.dataholds the 64-value rows and digits.images holds the 8 by 8 pictures. - 2Fit the PCA model on
digits.data. - 3Transform the same
digits.dataonto the first two principal components. - 4Plot each resulting point as its digit's text label, colored by class, with the two principal components as the axes.
The plot is a scatter in which the labels themselves reveal the structure. Reading the source's interpretation: zero, six, and four are relatively well separated along the first two components, although they still overlap, and most of the other digits overlap significantly. Keep this observation in mind, because it is the baseline you will compare against.
Two Call Patterns Side by Side
The most visible difference between the two methods is the sequence of calls. PCA is used in two steps, a fit followed by a transform. The TSNE class does not support transforming new data, so you use fit_transform, which builds the model and immediately returns the transformed data in one step. The flowchart below traces both sequences.
Reading the t-SNE Map
Now apply t-SNE to the same dataset so the two results can be compared. The source describes the t-SNE output as remarkable. All the classes are quite clearly separated. The ones and nines are somewhat split up, but most classes form a single dense group. t-SNE is unsupervised: it has no knowledge of the class labels. It still finds a two-dimensional representation that clearly separates the classes, using only how close points are to one another in the original space. The labels were used only afterward, to color and identify the points on the plot.
Comparing the Two Maps
Put the two results next to each other and the differences become a set of questions you can answer from the code and the source text. PCA is a linear technique, so its output axes are principal components you can keep using with the fitted model. t-SNE is a non-linear technique, and its output comes from a single fit_transform call on the data you supplied. The diagram and table below summarize these differences.
| Aspect | PCA | t-SNE |
|---|---|---|
| Technique type | Linear | Non-linear |
| Calls used | fit, then transform | fit_transform only |
| Can project new data | Yes, with transform | No transform method |
| Output axes | Principal components | Two-dimensional embedding |
| Class separation on digits | Partial overlap; zero, six, and four somewhat separated | Classes clearly separated, with ones and nines somewhat split |
Differences between the two methods as the source and the program describe them
Perplexity and Early Exaggeration
The t-SNE algorithm has tuning parameters, and two of them are perplexity and early_exaggeration. The source explains that the algorithm often works well with its default settings, so the program does not change either one. The source invites you to experiment with both, but it also warns that their effects are usually minor.
This is useful to know before you start. If you change perplexity or early_exaggeration and see only a small shift in the picture, that is consistent with what the source describes. A large change would be unexpected from these settings alone. In that case, check the rest of the code first, such as the input passed to fit_transform, before blaming the tuning parameters.
Mistakes Beginners Make
Common mistake Drawing or fitting from the wrong attribute
What it looks likePlotting digits.data as if it were pictures, or feeding digits.images into the reduction model
Why it failsdigits.images holds the 8 by 8 pictures used for display, while
digits.datais the input table the models read fromDo this insteadDraw from digits.images and pass digits.data to PCA and t-SNE
Common mistake Calling transform on a TSNE object
Why it failsThe source states that t-SNE does not support transforming new data, so the TSNE class has no transform method
Do this insteadUse fit_transform on the data you have, and treat the result as the only 2D output for that data
Common mistake Expecting the labels to shape the t-SNE layout
Why it failst-SNE is unsupervised and never reads the labels; they are used only to color and identify points afterward
Do this insteadUse the labels for coloring and checking, and understand that the separation comes from how close points are
Common mistake Treating a small perplexity change as a bug
Why it failsThe source says the effects of perplexity and
early_exaggerationare usually minor, so a small shift is expectedDo this insteadIf the picture changes a lot, inspect the input to fit_transform before tuning the parameters further
Common mistake Reading the PCA plot as if the classes were fully separated, when the source reports partial overlap
Practice: Diagnosing a Surprising Plot
Suppose your t-SNE plot of the digits looks completely different after you change perplexity by a small amount, and the clusters are scattered without the usual structure. Using the three moves of the program (load, PCA baseline, t-SNE), decide which step you would examine first and explain why. Name the attribute you would check and state what the source says about the expected size of a perplexity change.
Hints
- Start by asking whether the input to
fit_transformis the expected data, since the source points there when a change is larger than expected - The source says the tuning effects are usually minor, so a large change is a signal to check the call, not only the setting
- Recall that
digits.datais the input table the models read
Key Takeaways
- The digits dataset holds 8 by 8 grayscale images of handwritten digits, and each image contributes 64 pixel values to the input table; labels are used only for coloring the plot.
- PCA is a linear baseline used with fit and then transform, and its two components show partial overlap between digit classes.
- t-SNE is non-linear and unsupervised; it is called with
fit_transformbecause it has no transform method for new data. - Perplexity and
early_exaggerationusually have minor effects, so the defaults are often enough, and a large change points to checking the input first.