sklearn.manifold.TSNE (2023)

class sklearn.manifold.TSNE(n_components=2, *, perplexity=30.0, early_exaggeration=12.0, learning_rate='auto', n_iter=1000, n_iter_without_progress=300, min_grad_norm=1e-07, metric='euclidean', metric_params=None, init='pca', verbose=0, random_state=None, method='barnes_hut', angle=0.5, n_jobs=None, square_distances='deprecated')[source]

T-distributed Stochastic Neighbor Embedding.

t-SNE [1] is a tool to visualize high-dimensional data. It convertssimilarities between data points to joint probabilities and triesto minimize the Kullback-Leibler divergence between the jointprobabilities of the low-dimensional embedding and thehigh-dimensional data. t-SNE has a cost function that is not convex,i.e. with different initializations we can get different results.

It is highly recommended to use another dimensionality reductionmethod (e.g. PCA for dense data or TruncatedSVD for sparse data)to reduce the number of dimensions to a reasonable amount (e.g. 50)if the number of features is very high. This will suppress somenoise and speed up the computation of pairwise distances betweensamples. For more tips see Laurens van der Maaten’s FAQ [2].

Read more in the User Guide.

n_componentsint, default=2

Dimension of the embedded space.

perplexityfloat, default=30.0

The perplexity is related to the number of nearest neighbors thatis used in other manifold learning algorithms. Larger datasetsusually require a larger perplexity. Consider selecting a valuebetween 5 and 50. Different values can result in significantlydifferent results. The perplexity must be less than the numberof samples.

early_exaggerationfloat, default=12.0

Controls how tight natural clusters in the original space are inthe embedded space and how much space will be between them. Forlarger values, the space between natural clusters will be largerin the embedded space. Again, the choice of this parameter is notvery critical. If the cost function increases during initialoptimization, the early exaggeration factor or the learning ratemight be too high.

learning_ratefloat or “auto”, default=”auto”

The learning rate for t-SNE is usually in the range [10.0, 1000.0]. Ifthe learning rate is too high, the data may look like a ‘ball’ with anypoint approximately equidistant from its nearest neighbours. If thelearning rate is too low, most points may look compressed in a densecloud with few outliers. If the cost function gets stuck in a bad localminimum increasing the learning rate may help.Note that many other t-SNE implementations (bhtsne, FIt-SNE, openTSNE,etc.) use a definition of learning_rate that is 4 times smaller thanours. So our learning_rate=200 corresponds to learning_rate=800 inthose other implementations. The ‘auto’ option sets the learning_rateto max(N / early_exaggeration / 4, 50) where N is the sample size,following [4] and [5].

Changed in version 1.2: The default value changed to "auto".

n_iterint, default=1000

Maximum number of iterations for the optimization. Should be atleast 250.

n_iter_without_progressint, default=300

Maximum number of iterations without progress before we abort theoptimization, used after 250 initial iterations with earlyexaggeration. Note that progress is only checked every 50 iterations sothis value is rounded to the next multiple of 50.

New in version 0.17: parameter n_iter_without_progress to control stopping criteria.

min_grad_normfloat, default=1e-7

If the gradient norm is below this threshold, the optimization willbe stopped.

metricstr or callable, default=’euclidean’

The metric to use when calculating distance between instances in afeature array. If metric is a string, it must be one of the optionsallowed by scipy.spatial.distance.pdist for its metric parameter, ora metric listed in pairwise.PAIRWISE_DISTANCE_FUNCTIONS.If metric is “precomputed”, X is assumed to be a distance matrix.Alternatively, if metric is a callable function, it is called on eachpair of instances (rows) and the resulting value recorded. The callableshould take two arrays from X as input and return a value indicatingthe distance between them. The default is “euclidean” which isinterpreted as squared euclidean distance.

metric_paramsdict, default=None

Additional keyword arguments for the metric function.

New in version 1.1.

init{“random”, “pca”} or ndarray of shape (n_samples, n_components), default=”pca”

Initialization of embedding.PCA initialization cannot be used with precomputed distances and isusually more globally stable than random initialization.

Changed in version 1.2: The default value changed to "pca".

verboseint, default=0

Verbosity level.

random_stateint, RandomState instance or None, default=None

Determines the random number generator. Pass an int for reproducibleresults across multiple function calls. Note that differentinitializations might result in different local minima of the costfunction. See Glossary.

method{‘barnes_hut’, ‘exact’}, default=’barnes_hut’

By default the gradient calculation algorithm uses Barnes-Hutapproximation running in O(NlogN) time. method=’exact’will run on the slower, but exact, algorithm in O(N^2) time. Theexact algorithm should be used when nearest-neighbor errors needto be better than 3%. However, the exact method cannot scale tomillions of examples.

New in version 0.17: Approximate optimization method via the Barnes-Hut.

anglefloat, default=0.5

Only used if method=’barnes_hut’This is the trade-off between speed and accuracy for Barnes-Hut T-SNE.‘angle’ is the angular size (referred to as theta in [3]) of a distantnode as measured from a point. If this size is below ‘angle’ then it isused as a summary node of all points contained within it.This method is not very sensitive to changes in this parameterin the range of 0.2 - 0.8. Angle less than 0.2 has quickly increasingcomputation time and angle greater 0.8 has quickly increasing error.

n_jobsint, default=None

The number of parallel jobs to run for neighbors search. This parameterhas no impact when metric="precomputed" or(metric="euclidean" and method="exact").None means 1 unless in a joblib.parallel_backend context.-1 means using all processors. See Glossaryfor more details.

New in version 0.22.

square_distancesTrue, default=’deprecated’

This parameter has no effect since distance values are always squaredsince 1.1.

Deprecated since version 1.1: square_distances has no effect from 1.1 and will be removed in1.3.

embedding_array-like of shape (n_samples, n_components)

Stores the embedding vectors.


Kullback-Leibler divergence after optimization.


Number of features seen during fit.

New in version 0.24.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen during fit. Defined only when Xhas feature names that are all strings.

New in version 1.0.


Effective learning rate.

New in version 1.2.


Number of iterations run.

See also


Principal component analysis that is a linear dimensionality reduction method.


Non-linear dimensionality reduction using kernels and PCA.


Manifold learning using multidimensional scaling.


Manifold learning based on Isometric Mapping.


Manifold learning using Locally Linear Embedding.


Spectral embedding for non-linear dimensionality.


[1] van der Maaten, L.J.P.; Hinton, G.E. Visualizing High-Dimensional Data

Using t-SNE. Journal of Machine Learning Research 9:2579-2605, 2008.

[2] van der Maaten, L.J.P. t-Distributed Stochastic Neighbor Embedding

[3] L.J.P. van der Maaten. Accelerating t-SNE using Tree-Based Algorithms.

Journal of Machine Learning Research 15(Oct):3221-3245, 2014.

[4] Belkina, A. C., Ciccolella, C. O., Anno, R., Halpert, R., Spidlen, J.,

& Snyder-Cappione, J. E. (2019). Automated optimized parameters forT-distributed stochastic neighbor embedding improve visualizationand analysis of large datasets. Nature Communications, 10(1), 1-12.

[5] Kobak, D., & Berens, P. (2019). The art of using t-SNE for single-cell

transcriptomics. Nature Communications, 10(1), 1-14.


>>> import numpy as np>>> from sklearn.manifold import TSNE>>> X = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]])>>> X_embedded = TSNE(n_components=2, learning_rate='auto',...  init='random', perplexity=3).fit_transform(X)>>> X_embedded.shape(4, 2)



Fit X into an embedded space.


Fit X into an embedded space and return that transformed output.


Get parameters for this estimator.


Set the parameters of this estimator.

fit(X, y=None)[source]

Fit X into an embedded space.

X{array-like, sparse matrix} of shape (n_samples, n_features) or (n_samples, n_samples)

If the metric is ‘precomputed’ X must be a square distancematrix. Otherwise it contains a sample per row. If the methodis ‘exact’, X may be a sparse matrix of type ‘csr’, ‘csc’or ‘coo’. If the method is ‘barnes_hut’ and the metric is‘precomputed’, X may be a precomputed sparse graph.



X_newarray of shape (n_samples, n_components)

Embedding of the training data in low-dimensional space.

fit_transform(X, y=None)[source]

Fit X into an embedded space and return that transformed output.

X{array-like, sparse matrix} of shape (n_samples, n_features) or (n_samples, n_samples)

If the metric is ‘precomputed’ X must be a square distancematrix. Otherwise it contains a sample per row. If the methodis ‘exact’, X may be a sparse matrix of type ‘csr’, ‘csc’or ‘coo’. If the method is ‘barnes_hut’ and the metric is‘precomputed’, X may be a precomputed sparse graph.



X_newndarray of shape (n_samples, n_components)

Embedding of the training data in low-dimensional space.


Get parameters for this estimator.

deepbool, default=True

If True, will return the parameters for this estimator andcontained subobjects that are estimators.


Parameter names mapped to their values.


Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects(such as Pipeline). The latter haveparameters of the form <component>__<parameter> so that it’spossible to update each component of a nested object.


Estimator parameters.

selfestimator instance

Estimator instance.

Examples using sklearn.manifold.TSNE

sklearn.manifold.TSNE (1)

Comparison of Manifold Learning methods

Comparison of Manifold Learning methods

sklearn.manifold.TSNE (2)

Manifold Learning methods on a severed sphere

Manifold Learning methods on a severed sphere

sklearn.manifold.TSNE (3)

Manifold learning on handwritten digits: Locally Linear Embedding, Isomap…

Manifold learning on handwritten digits: Locally Linear Embedding, Isomap...

sklearn.manifold.TSNE (4)

Swiss Roll And Swiss-Hole Reduction

Swiss Roll And Swiss-Hole Reduction

sklearn.manifold.TSNE (5)

t-SNE: The effect of various perplexity values on the shape

t-SNE: The effect of various perplexity values on the shape

sklearn.manifold.TSNE (6)

Approximate nearest neighbors in TSNE

Approximate nearest neighbors in TSNE


What is the maximum number of iterations in t-SNE? ›

The maximum number of iterations for the optimization. Default is 1000, and the range is 250 - 9999999.

Why not to use t-SNE? ›

One of the main disadvantages of t-sne is that it is computationally intensive and therefore relatively slow. This means that it is not appropriate for very large datasets with many observations. Part of the reason for this is that pairwise distances need to be calculated between all of the points in the dataset.

What is the sample size for t-SNE? ›

By default, FCS Express uses a sample size of 3000. However, a user can specify any number for the subset of cells to be included in the transformation. However, the larger the sample size, the more time it will take to calculate the transformation.

Why is t-SNE better than PCA? ›

PCA vs t-SNE: t-SNE differs from PCA by preserving only small pairwise distances or local similarities whereas PCA is concerned with preserving large pairwise distances to maximize variance. PCA is a linear dimension reduction technique that seeks to maximize variance and preserves large pairwise distances.

What is the maximum number of iterations in a loop? ›

There is no any limitation on iteration for loops. For example your loop can be infinite.

What is the drawback of t-SNE? ›

Problems with t-SNE arise when intrinsic dimensions are higher i.e. more than 2-3 dimensions. t-SNE has the tendency to get stuck in local optima like other gradient descent based algorithms. The basic t-SNE algorithm is slow due to nearest neighbor search queries.

What is the alternative to t-SNE? ›

UMAP is like t-SNE, but faster and more general-purpose.

The most tried-and-true technique is PCA, which stands for Principle Component Analysis. PCA has been around for over a century. It is fast, deterministic, and linear. Being deterministic and linear means that it's also reversible.

Is t-SNE good for visualization? ›

T-Distributed Stochastic Neighbor Embedding (t-SNE) is another technique for dimensionality reduction, and it's particularly well suited for the visualization of high-dimensional data sets. Contrary to PCA, it's not a mathematical technique but a probabilistic one.

How much sample size is enough? ›

Most statisticians agree that the minimum sample size to get any kind of meaningful result is 100. If your population is less than 100 then you really need to survey all of them.

What is the minimum number of samples for t-test? ›

In most studies, a sample size of at least 40 can guarantee that the sample mean is approximately normally distributed, and the one-sample t-test can then be safely applied. It is used to know whether the unknown means of two populations are different from each other based on independent samples from each population.

Is sample size of 20 enough? ›

Generally, a sample size of 20 is too small for statistical analysis. For research purposes, it is generally recommended that a sample size of at least 30 be used to ensure statistically significant results.

Can t-SNE be used for dimensionality reduction? ›

t-SNE is a technique for dimensional analysis or reduction that is a short form of T-distributed Stochastic Neighbor Embedding. As the name suggests it is a nonlinear dimensionality technique that can be utilized in a scenario where the data is very high dimensional.

What is the difference between t-SNE and multidimensional scaling? ›

Multidimensional scaling aims to preserve the distances between pairs of data points, focusing on pairs of distant points in the original space. Differently, t-SNE focuses on maintaining neighborhood data points. Data points that are close in the original data space will be tight in the t-SNE embeddings.

What is the difference between t-SNE and Isomap? ›

Isomap and LLE are best use to unfold a single, continuous, low-dimensional manifold. On the other hand, t-SNE focuses on the local structure of the data and attempts to 'extract' clustered local groups instead of trying to 'unroll' or 'unfold' it.

Which loop can iterate infinitely? ›

An infinite loop -- sometimes called an endless loop -- is a piece of code that lacks a functional exit so that it repeats indefinitely.

Can there be maximum of 9 loops within a loop? ›

Answer. In Nested loop, the innermost loop will be executed first then the outer loop. ... There can be maximum of 9 loops within a loop.

What is the maximum number of iterations needed to find the item? ›

The minimum number of iterations needed to find the maximum number is n-1. To store maximum and minimum, we declare and initialize variables max and min. We traverse the array from and compare each element to the minimum and maximum values.

What is the main difference between the SNE and t-SNE algorithms? ›

There is a significant difference between t-SNE and SNE in the scale of low dimension probability because t-SNE is using the t-distribution to compute the conditional probability in low dimensional space, so the projection of data has a wider spread, indicated from the length of the graph in iteration of 200 which has ...

Does t-SNE preserve global structure? ›

The introduction of a new metric to quantify the global structure preservation. Analysis of GPU t-SNE based methods in real-world applications with large datasets. PCA initialization in SWW-tSNE is fundamental to preserve global structures. UMAP and AtSNE does not preserve global structures better than SWW-tSNE.

What is the advantage of t-SNE? ›

The main advantage of t-SNE is the ability to preserve local structure. This means, roughly, that points which are close to one another in the high-dimensional data set will tend to be close to one another in the chart. t-SNE also produces beautiful looking visualizations.

Why is umap faster than tSNE? ›

Finally, UMAP uses the Stochastic Gradient Descent (SGD) instead of the regular Gradient Descent (GD) like tSNE / FItSNE, this both speeds up the computations and consumes less memory.

Where can I use tSNE? ›

It has become widely used in bioinformatics and more generally in data science to visualise the structure of high dimensional data in 2 or 3 dimensions. It is the best known of a group of algorithms called Manifold Learning which are used for non-linear dimensionality reduction.

Which visualization works best for multivariate analysis? ›

Scatter Plot Matrix

It's a very good tool for allowing quick comparison of similar data sets (as they are each arranged next to each other in vertical and horizontal directions).

Which language is best for interactive data visualization? ›

Python. Python today is one of the most popular simple universal languages for data visualization and even more. It is often the best choice for solving problems in Machine Learning, Deep Learning, Artificial Intelligence, and so on.

What is the best visual to show distribution? ›

Scatter plots are best for showing distribution in large data sets.

What is the 10 times rule for sample size? ›

The 10-times rule method

Among the variations of this method, the most commonly seen is based on the rule that the sample size should be greater than 10 times the maximum number of inner or outer model links pointing at any latent variable in the model (Goodhue et al., 2012).

Is a sample size of 30 statistically significant? ›

A sample size of 30 is fairly common across statistics. A sample size of 30 often increases the confidence interval of your population data set enough to warrant assertions against your findings.4 The higher your sample size, the more likely the sample will be representative of your population set.

Is 30 too small of a sample size? ›

“A minimum of 30 observations is sufficient to conduct significant statistics.” This is open to many interpretations of which the most fallible one is that the sample size of 30 is enough to trust your confidence interval.

What is an acceptable t-test value? ›

If a p-value reported from a t test is less than 0.05, then that result is said to be statistically significant. If a p-value is greater than 0.05, then the result is insignificant.

How many replicates are needed for t-test? ›

There is no minimum sample size required to perform a t-test. In fact, the first t-test ever performed only used a sample size of four. However, if the assumptions of a t-test are not met then the results could be unreliable.

What is a small sample t-test? ›

The t-test is the small sample analog of the z test which is suitable for large samples. A small sample is generally regarded as one of size n<30. A t-test is necessary for small samples because their distributions are not normal.

How do you know if a sample size is too large? ›

A sample is too large when it costs too much. Too much time, too much money, too much effort, too many graduate students, or too many slithy toves. From the point of view of determining the properties of the statistical population being sampled, more is better.

What is the rule of thumb for sample size? ›

Rule of Thumb #1: A larger sample increases the statistical power of the evaluation. Rule of Thumb #2: If the effect size of a program is small, the evaluation needs a larger sample to achieve a given level of power. Rule of Thumb #3: An evaluation of a program with low take-up needs a larger sample.

Is 50 a large enough sample size? ›

Often a sample size is considered “large enough” if it's greater than or equal to 30, but this number can vary a bit based on the underlying shape of the population distribution.

Should I use PCA or t-SNE before clustering? ›

One of the main limitations of t-SNE is its high computational costs. If you have a very large feature set it might be good to first use PCA to reduce the number of features to a few principal components and then use t-SNE to further reduce the data to 2 or 3 clusters.

Can t-SNE be used for clustering? ›

t-distributed Stochastic Neighborhood Embedding (t-SNE), a clustering and visualization method proposed by van der Maaten & Hinton in 2008, has rapidly become a standard tool in a number of natural sciences.

What is the difference between autoencoder and t-SNE? ›

More specifically, an autoencoder tries to minimize the reconstruction error, while t-SNE tries to find a lower dimensional space and at the same time it tries to preserve the neighborhood distances. As a result of this attribute, t-SNE is usually preferred for plots and visualizations.

Is t-SNE better to use than PCA for dimensionality reduction while working on a local machine with minimal computational power? ›

PCA always performs better than t-SNE for smaller-sized data.

Why does t-SNE use T distribution? ›

t-SNE uses a heavy-tailed Student-t distribution with one degree of freedom to compute the similarity between two points in the low-dimensional space rather than a Gaussian distribution. T- distribution creates the probability distribution of points in lower dimensions space, and this helps reduce the crowding issue.

How many dimensions are there in multidimensional scaling? ›

MDS comprises a group of techniques that create a map visualizing the relative positions of a number of objects based on the matrix of distances between them. The map may have one, two, or three dimensions. There are two broad categories in MDS: metric, or classical, MDS and nonmetric MDS.

Does t-SNE preserve distances? ›

t-SNE works by creating embeddings of points on a higher dimension to a lower dimension (2 dimensions for visualization purposes). Since its prime objective is preserving the local structure, it effectively takes care of neighbourhoods but fails to preserve distances between different neighbourhoods.

Will you use results from t-SNE to build a predictive model? ›

t-SNE is used primarily to compress data to 2 dimensions for visualization. It does not produce a predictive model such that unseen data can be mapped to 2-D.

Why is t-SNE non deterministic? ›

TSNE is non-deterministic, meaning you won't get exactly the same output each time you run it (though the results are likely to be similar. TSNE tends to cope better with non-linear signals in your data, so odd outliers tend to have less of an effect, and often the visible separation between relevant groups is improved.

Can we use t-SNE for clustering? ›

Remember t-SNE is a visualization tool first and a dimensionality reduction tool second. Finally, t-SNE calculates the similarity probability score in a low dimensional space in order to cluster the points together.

What are the shortcomings of UMAP? ›

One of the limitations of UMAP is that it can only be used for dimensionality reduction, which means it is not suitable for all types of data analysis tasks. For example, it cannot be used for classification or regression tasks, since it does not provide a predictive model.

What is the difference between multidimensional scaling and t-SNE? ›

Try Multidimensional Scaling

Whilst t-SNE preserves local neighbors, MDS takes a different approach to mapping. It has 2 main variants: Metric MDS minimizes the difference between distances in input and output spaces. Non-metric MDS aims to preserve the ranking of distances between input and output spaces.

What is the main difference between MDS and t-SNE? ›

Multidimensional scaling aims to preserve the distances between pairs of data points, focusing on pairs of distant points in the original space. Differently, t-SNE focuses on maintaining neighborhood data points. Data points that are close in the original data space will be tight in the t-SNE embeddings.

Why is UMAP better than t-SNE? ›

While both UMAP and t-SNE produce somewhat similar output, the increased speed, better preservation of global structure, and more understandable parameters make UMAP a more effective tool for visualizing high dimensional data.

Is UMAP better than PCA? ›

We can clearly see that the UMAP does a great job in separating the data points compared to t-SNE and PCA in terms of separation. However, there are no big clusters separating the sign sufficiently, There are similar data points agglomerated together in other parts too from a 2d prospective.

What is the math behind UMAP? ›

The principal mathematical result behind UMAP is that there is an adjunction between finite fuzzy simplicial sets and finite extended-pseudo-metric spaces. n i=0 ti = 1}. A 0-simplex is a single point; a 1-simplex an interval; a 2-simplex a triangle. {xi} is itself an (n − 1)-dimensional simplex.

What is perplexity in t-SNE? ›

Perplexity is a measure for information that is defined as 2 to the power of the Shannon entropy. The perplexity of a fair die with k sides is equal to k. In t-SNE, the perplexity may be viewed as a knob that sets the number of effective nearest neighbors.

What is the difference between t-SNE and UMAP clustering? ›

The main difference between t-SNE and UMAP is the interpretation of the distance between objects or "clusters". I use the quotation marks since both algorithms are not meant for clustering - they are meant for visualization mostly. t-SNE preserves local structure in the data.

Can UMAP be used for clustering? ›

Uniform Manifold Approximation and Projection (or UMAP) is a new dimension reduction technique that can be used to visualize patterns of clustering in high-dimensional data.


Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated: 12/17/2023

Views: 6037

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.