Unraveling Data Patterns with t-SNE on MNIST Dataset Beyond Linearity

This walkthrough explains how t-SNE captures nonlinear relationships in high-dimensional data and shows a hands-on implementation on the MNIST digit dataset.

6:10 video3 min readWatch on YouTube

Some datasets simply do not compress well into a straight line. When the relationships between data points are complex and nonlinear, a linear method like PCA can miss the underlying structure entirely. This video picks up where an earlier PCA discussion left off and introduces t-SNE, a dimensionality reduction technique built specifically to handle that kind of complexity, then walks through implementing it on the MNIST handwritten digit dataset.

Why t-SNE goes beyond linear methods

t-SNE stands for t-distributed stochastic neighbor embedding. Like PCA, it is a dimensionality reduction method used for visualizing high-dimensional data, but the two approaches work very differently. PCA focuses on linear transformations, while t-SNE aims to capture more complex, nonlinear relationships between data points. That makes it particularly useful for datasets with intricate internal structure, where flattening the data along straight axes would lose important patterns.

How t-SNE actually works

The core mechanism starts by converting pairwise similarities between data points into joint probabilities, which represent how likely two points are to be neighbors once the data is mapped into a lower-dimensional space. From there, t-SNE minimizes the divergence, specifically the KL divergence, between the joint probabilities of the low-dimensional embedding and the original high-dimensional data. In simpler terms, it tries to preserve the relationships between points as closely as possible when squeezing the data down into fewer dimensions.

One important technical detail: t-SNE has a non-convex cost function, unlike PCA. That means different initializations can lead to different results, and it is why it is often recommended to run a linear method like PCA first to reduce dimensions before applying t-SNE on top of it.

The parameters that shape the result

Getting useful output from t-SNE means understanding a handful of key hyperparameters. Perplexity determines the number of nearest neighbors considered during optimization. Early exaggeration controls the spacing between natural clusters in the embedded space. The learning rate balances the tradeoff between preserving local structure and preserving global structure. And the maximum number of iterations limits how long the optimization process runs. Each of these affects how tight or spread out the resulting clusters appear.

Implementing t-SNE on MNIST

The hands-on portion loads the MNIST dataset from scikit-learn's datasets module, standardizes the input, and transforms it into scalar form. From there, the t-SNE model is created with two components, a perplexity of 30, a learning rate of 100, and a maximum of 1,000 iterations. After fitting the data, the results are visualized, with each of the 10 digit classes (0 through 9) represented by its own color in the final plot.

Where t-SNE is expensive and where it shines

t-SNE is computationally expensive, so the data should be pre-processed and the parameters, perplexity, number of iterations, and learning rate among them, chosen carefully before running it. Despite the cost, it is valuable for complex data structures. One example mentioned is applying t-SNE to movie transcripts to uncover hidden thematic clusters or patterns of interaction between characters, illustrating that its usefulness extends well beyond image datasets like MNIST.

Key takeaways

  • t-SNE captures nonlinear relationships between data points, unlike PCA's linear transformations.
  • It works by converting pairwise similarities into joint probabilities and minimizing KL divergence between high- and low-dimensional representations.
  • Its cost function is non-convex, so results can vary by initialization, and running PCA first is a common practice.
  • Key hyperparameters include perplexity, early exaggeration, learning rate, and maximum iterations.
  • On MNIST, t-SNE with two components clearly separates the 10 digit classes by color in the resulting visualization.
  • t-SNE can be applied well beyond images, including uncovering thematic clusters in text like movie transcripts.

Who this is for

This video suits learners who already have a basic grasp of PCA and want to understand when a nonlinear dimensionality reduction technique is the better tool. It is part of a broader Humanitarians AI series working through dimensionality reduction and clustering methods on the MNIST dataset.

Full transcript(auto-generated, with timestamps)

[0:01]Hello everyone previously we have discussed about dimensionality reduction and how to do it using PCA its working principle today let us focus on tfn which stands for p distributed stochastic nber embedding it is also a dimensionality reduction method where it is used for visualizing High dimensional data and unlike PCA which focuses on linear Transformations TSN aims to capture more complex and non- relation nonlinear relationships between data points it is particularly useful when dealing with intricate data uh data which has like which in the data which has intricate structures and uh let us understand how TSN works and what's the working that will behind it here first we have to

[1:10]Find the similarities to join probabilities so TS converts the pairwise similarities between data points into joint probabilities and these probabilities represent How likely is it for two points to be neighbors in the lower dimensional space and by using that it will minimize the Divergence where it minimizes the K Divergence between the joint probabilities of a low dimensional embedding and the original High dimensional data to keep it simpler it tries to preserve the relationship between points as closely as possible and it is a non-convex CA function which means unlike PCA TSN has a non-convex cost function which uh which is like it has like diff uh for

[2:18]Different initializations uh it can lead to varying results and therefore it is essential to use other dimensionality deduction method like PCA first and then reduce the dimensions using TS here the uh the hypop parameters in TN TSN are the perplexity which determines the number of nearest neighbors considered during the optimization and the only exag exaggeration which controls the spacing between natural clusters in the embeded space then comes the learning rate which balances the tradeoff between preserving local and Global structures and then the maximum it op

[3:31]Ations it limits the optimization process since we have understood what is TSN how does it works and what are the parameters to construct let us see how we can Implement that using python first input all the require libraries and then we are loading the data uh the MS data set from SK learn. data sets then we are uh standardizing the the input and uh transforming it into a scalar form from there we are creating the TSN model with the number of components are two and the perpect is 30 with our learning rate of 100 and the maximum iterations that this uh algorithm should run is defined as 1,000

[4:38]And then we have fit the data into uh the TSN model and finally we are visualizing the data and here uh since we already know we have like 10 classes each class uh like in the MS data the uh final classification classes are from 0 to 9 digits so we have 10 classes and here each color represent the uh identification of each class respectively and um finally like uh TSN is uh can be very valuable for complex data structures and TSN is very expensive so before doing that we have to uh keep in mind that we have to pre-process the data and like choose the

[5:34]Appropriate method the uh appropriate parameters like the pery the number of itations learning rate and all and then interpret our results there are like many use cases of uh TSN and one of which uh that I wanted to mention was uh by applying TSM to movie transcripts we can uncover the the hidden uh thematic uh clusters or the uh know interaction between the characters thank you

More videos

Humanitarians AI Lyrical Literacy Project