Dimensionality Reduction: t-SNE. An overview of t-SNE as a… | by Amy Pajak | Jun, 2023


Introduction — Excessive Dimensional Knowledge

t-SNE was launched because of having a lot of excessive dimensional information that practitioners need to visualize. For instance:

  1. Monetary information: inventory costs, buying and selling volumes, and financial indicators, may be represented as high-dimensional information units.
  2. Medical imaging: applied sciences, comparable to MRI and CT scans, generate high-dimensional information with depth values representing completely different options.
  3. Genomics: DNA sequences of organisms are represented as high-dimensional information units with every gene or nucleotide base representing a dimension. The human genome has roughly 3 billion base pairs, which correspond to three billion options within the DNA.
  4. Picture and video information the place every pixel represents a dimension.
  5. Social media platforms with consumer profiles, posts, likes, feedback, and different interactions.
  6. Textual content information, comparable to information articles, tweets, and buyer opinions, may be represented with every phrase or token as a dimension.
  7. Robotics: generates information with sensory inputs from cameras, microphones, and different sensors used of their management programs.

Excessive dimensional information is all over the place. We have to visualize and discover the advanced information in a extra intuitive and comprehensible means. t-SNE serves as a strong device to attain this by successfully remodeling the high-dimensional information right into a low-dimensional illustration with out dropping vital info.

MNIST Digits

An effective way to clarify t-SNE is to indicate the way it works on the MNIST Digits dataset.

A Survey of Handwritten Character Recognition with MNIST and EMNIST (2019)

It is a publicly accessible labeled dataset of 60,000 28×28 grayscale coaching photos of handwritten digits from 0–9, together with a take a look at set of 10,000 photos.

We are going to use this dataset to display completely different dimensionality discount strategies.

Introducing Principal Part Evaluation (PCA)

One of many first, and hottest, dimensionality discount strategies is Principal Part Evaluation (PCA) which was printed in 1901 by Pearson, Karl et al. It’s a linear approach which finds a linear projection, or a brand new illustration, of the unique high-dimensional information factors onto a lower-dimensional subspace in a technique to maximize the variance of the info i.e. protect as a lot info as attainable. These projected axes/instructions are known as the principal elements of the info.

View the pocket book model here

If we visualize PCA on MNIST Digits the outcomes can be just like what we see above which is a visualization of round 5,000 photos. They’ve been specified by 2-dimensions the place every level corresponds to a digit within the dataset and its shade labels which digit the purpose is representing. What we see right here is that PCA captures a number of the construction of the info, for instance the purple factors on the proper kind a cluster of 0’s, and the oranges on the left kind a cluster of 1’s. This occurs to be the primary principal element! — so the primary variation between digits is between the 0’s and 1’s. That is sensible in pixel values the place 0’s and 1’s have only a few overlapping pixels.

The second precept element is on the highest of the visualization the place we see 4’s 7’s and 9’s clustered that are barely extra comparable by way of pixel values, and on the underside we’ve obtained 3’s, 5’s and eight’s clustered that are additionally extra comparable e.g. a 3 could have many overlapping pixels with an 8. In order that’s our second supply of most variation between the info.

We will see 4, 7, 9 and three, 5, 8 have comparable overlapping pixel construction

That is nice however an issue arises when the info is unlabelled — as we will see on the proper. The colour/labels can inform us some details about the relationships within the information, with out them we see no clear clusters however moderately simply many factors in a 2D house. So we run into an issue with unlabelled information inflicting us to be unable to interpret these outcomes.

Can we do higher?

Linear vs Non-Linear Knowledge

PCA is nice, but it surely’s a linear algorithm.

  • It can not symbolize advanced relationships between options
  • It’s involved with preserving giant distances within the map (to seize the utmost quantity of variance). However are these distances dependable?

Linear strategies deal with holding the low-dimensional representations of dissimilar information factors far aside (e.g. 0’s and 1’s we’ve simply seen). However is that what we would like in a visible illustration? And the way dependable is it?

Dimensionality reduction with t-SNE (2018)

If we take a look at the swiss-roll nonlinear manifold above (a), we will see {that a} Euclidean (straight-line) distance between two factors within the purple and blue clusters would recommend that the info factors are fairly shut.

If we think about all the construction to be represented in a 2D aircraft (i.e. rolling it out right into a flat 2D form), the purple factors would really be on the other finish to the blue factors — one must traverse all the size of the roll to get from one level to the opposite. PCA would try and seize the variance alongside the longest axis of the roll, basically flattening it out. This may fail to protect the spiral construction inherent to the Swiss roll information, the place factors which are shut collectively within the spiral (and thus ought to be shut collectively in a superb 2D illustration) find yourself being positioned far aside.

So we will see PCA doesn’t work very nicely for visualization of nonlinear information as a result of it preserves these giant distances and we not solely want to think about the straight-line distance, but additionally the encircling construction of every information level.

Introducing t-SNE

Stochastic Neighbor Embedding was first developed and printed in 2002 by Hinton et al, which was then modified in 2008 to what we’re in the present day — t-SNE (t-Distributed Stochastic Neighbor Embedding).

For anybody curious one other variation was printed in 2014 named Barnes-Hut t-SNE which improves on the effectivity of the algorithm by way of a tree-based implementation.

How t-SNE works

Dimensionality reduction with t-SNE (2018)

In a excessive dimensional house (left) we measure similarities between factors. We do that in a means in order that we solely take a look at native similarities, i.e. close by factors.

The purple dot within the excessive dimensional house is xi. We first middle a gaussian over this level (proven because the purple circle) after which measure the density of all the opposite factors underneath this gaussian (e.g. xj). We then renormalise all pairs of factors that contain the purpose xi (the denominator/backside a part of the fraction). This provides us the conditional chance pj|i, which principally measures the similarity between pairs of factors ij. We will consider this as a chance distribution over pairs of factors, the place the chance of selecting a selected pair of factors is proportional to their similarity (distance).

t-SNE (t-Distributed Stochastic Neighbor Embedding) Algorithm

This may be visualized as such. If two factors are shut collectively within the authentic excessive dimensional house, we’re going to have a big worth for pj|i. If to factors are dissimilar (far aside) within the excessive dimensional house, we’re going to get a small pj|i.


Trying on the similar equation, perplexity tells us the density of factors relative to a selected level. If 4 factors of comparable traits are clustered collectively, they’ll have greater perplexity than these not clustered collectively. Now, factors with much less density round them have flatter regular curves in comparison with curves with extra density. Within the determine under, the purple factors are sparse.

t-SNE (t-Distributed Stochastic Neighbor Embedding) Algorithm

We compute the conditional distribution between factors as this permits us to set a unique bandwidth (sigma i) for every level, such that the conditional distribution has a hard and fast perplexity. That is principally scaling the bandwidth of the gaussian in such a means {that a} mounted variety of factors fall within the vary of this Gaussian. We do that as a result of completely different elements of the house might have completely different densities, and this trick permits us to adapt to these completely different densities.

Mapping the decrease dimensional house

Subsequent we’re going to have a look at the low dimensional house which can be our ultimate map.


We begin by laying the factors out randomly on this map. Every excessive dimensional object can be represented by some extent right here.

We then repeat the identical course of beforehand – middle a kernel over the purpose yi and measure the density of all the opposite factors yj underneath that distribution. We then renormalise by dividing over all pairs of factors. This provides us a chance qij, which measures the similarity of two factors within the low dimensional map.

Now, we would like these chances in qij to replicate the similarities pij which we computed within the excessive dimensional house simply earlier than, as carefully as attainable. If the qij’s are equivalent to the pijs then apparently the construction of the map is similar to the construction of the info within the authentic excessive dimensional house.

We are going to measure the distinction between these pij values within the excessive dimensional house and the qij values within the low dimensional map through the use of Kullback–Leibler divergence.

Stochastic Neighbor Embedding

KL divergence is the usual measure of the gap between chance distributions / their similarity. Its proven under in the fee operate because the sum over all pairs of factors of pj|i occasions log pj|i over qj|i.

  • Similarity of knowledge factors in Excessive Dimension
  • Similarity of knowledge factors in Low Dimension

Our aim now’s to put out the factors within the low dimensional house such that the KL divergence is minimized i.e. as comparable as attainable to the excessive dimension values. With a view to do this we’re principally going to do gradient descent on this KL divergence, which is actually shifting the factors round in such a means that the KL divergence turns into small

Mapping the decrease dimensional house


Right here we will see the resultant mapping which has been rearranged to be as just like the upper dimension as attainable.

KL divergence is helpful because it measures the similarity between two chance distributions. It is usually symmetric (distance xi -> xj is similar as distance from xj -> xi).

t-SNE Algorithm

Let’s take a ultimate take a look at the general algorithm.


Combining what we’ve seen:

1. Calculate the pairwise affinities (conditional chances) within the high-dimensional house, utilizing a Gaussian distribution. The perplexity parameter defines the efficient variety of neighbors every level has and helps to stability the deal with native and world facets of the info.

2. Symmetrize the chances. Because of this every level considers the opposite level as its neighbor, and it’s achieved by taking the common of the conditional chances for every pair of factors. Then normalize by dividing by the full variety of factors.

3. Randomly initialize the place of every information level within the low-dimensional house, often by drawing from a traditional distribution with imply 0 and small variance.

4. Begin a loop that may iterate for a hard and fast variety of occasions T. Every iteration updates the place of the factors:

4.1. Calculate the pairwise affinities (similarities) within the low-dimensional house utilizing a t-Pupil distribution.

4.2. Calculate the gradient of the fee operate with respect to the place of the factors. The fee operate is often the Kullback-Leibler divergence between the high-dimensional and low-dimensional distributions.

4.3. Replace the place of the factors within the low-dimensional house. This replace step consists of three elements: the previous place Y(t-1), a time period proportional to the gradient that helps decrease the fee operate, and a momentum time period that helps speed up convergence and keep away from native minima.

5. Finish of the t-SNE algorithm. The ultimate positions of the factors within the low-dimensional house ought to now present a helpful visualization of the high-dimensional information.

Visible output of t-SNE utilized to 1000 MNIST Digits photos

If you wish to see this algorithm applied intimately as code please check out the unique authors github [3] or this nice step-by-step article.


“To take care of hyperplanes in a 14-dimensional house, visualize a 3D house and say ‘fourteen’ to your self very loudly. Everybody does it.” — Geoffrey Hinton, A geometrical view of perceptrons, 2018


  1. Visualization: t-SNE may also help visualize high-dimensional information that has non-linear relationships in addition to outliers
  2. Good for clustering: t-SNE is usually used for clustering and may also help establish teams of comparable information factors inside the information.


  1. Computational Complexity: t-SNE entails advanced calculations because it calculates the pairwise conditional chance for every level. Attributable to this, it takes extra time because the variety of information factors will increase. Barnes-Hut t-SNE was later developed to enhance on this.
  2. Non-Deterministic: As a result of randomness within the algorithm, though code and information factors are the identical in every iteration, we might get completely different outcomes throughout runs.


Within the following pocket book I exploit Python to implement PCA and t-SNE on the MNIST Digits dataset by way of the sklearn library:


Conclusion and Subsequent Steps

To conclude, t-SNE visualization is simply step one within the information evaluation course of. The insights gained from the visualization should be adopted up with additional evaluation to achieve a deeper understanding of the info or captured by an acceptable ML algorithm to construct predictive fashions, or statistical evaluation strategies to check particular hypotheses concerning the information.

Different common dimensionality discount strategies embrace:

  • Non-negative matrix factorization (NMF)
  • Kernel PCA
  • Linear discriminant evaluation (LDA)
  • Autoencoders
  • Uniform manifold approximation and projection (UMAP)

You’ll be able to learn extra about them here.

Source link


Please enter your comment!
Please enter your name here