TGViewer
Data science/ML/AI Data science/ML/AI @datascience_bds · 14K subscribers
Post #1219 1.44K
▎t-SNE(t-distributed Stochastic Neighbor Embedding): A Deep Dive into Dimensionality Reduction

▎What is t-SNE?

t-SNE is a machine learning algorithm that helps visualize high-dimensional data by reducing it to two or three dimensions. This technique is particularly useful for visualizing complex datasets, such as those found in image recognition, text analysis, and bioinformatics.

▎Why Use t-SNE?

When dealing with high-dimensional data (like images with thousands of pixels or text represented by numerous features), it can be challenging to understand the underlying structure and relationships within the data. t-SNE helps by:

1. Preserving Local Structure: It keeps similar data points close together in the lower-dimensional space, which makes it easier to identify clusters or groups.

2. Revealing Global Structure: While it focuses on local relationships, t-SNE can also help highlight the overall distribution of the data.

3. Intuitive Visualization: The result is often visually appealing and interpretable, making it easier for analysts to communicate findings.

▎How Does t-SNE Work?

The algorithm works in two main steps:

1. Probability Distribution in High Dimensions: For each data point, t-SNE computes probabilities that represent the similarity between points based on their distances. It uses a Gaussian distribution to model these probabilities.

2. Probability Distribution in Low Dimensions: It then tries to find a lower-dimensional representation of the data that maintains these similarities as closely as possible. This is done using a Student's t-distribution to compute probabilities in the lower-dimensional space.

The algorithm minimizes the divergence between the two probability distributions using a technique called gradient descent.

▎Key Parameters

• Perplexity: This parameter balances attention between local and global aspects of the data. A smaller perplexity focuses more on local structure, while a larger one considers more global relationships.

• Learning Rate: This controls how much to change the representation during each iteration. A learning rate that's too high can lead to erratic results, while one that's too low may slow down convergence.

▎Example: Using t-SNE in Python

Here's a simple example of how to use t-SNE with the popular scikit-learn library on the famous Iris dataset:

import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.manifold import TSNE

# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Apply t-SNE
tsne = TSNE(n_components=2, perplexity=30, random_state=42)
X_embedded = tsne.fit_transform(X)

# Plotting the results
plt.figure(figsize=(8, 6))
scatter = plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=y, cmap='viridis')
plt.title('t-SNE Visualization of Iris Dataset')
plt.xlabel('t-SNE Component 1')
plt.ylabel('t-SNE Component 2')
plt.colorbar(scatter, label='Species')
plt.show()


In this example, we load the Iris dataset, apply t-SNE to reduce its four dimensions down to two, and then visualize the results. The colors represent different species of iris flowers, showing how well t-SNE can separate them based on their features.

▎Limitations of t-SNE

While t-SNE is powerful, it has some limitations:

• Computationally Intensive: It can be slow for very large datasets due to its complexity.

• Non-Deterministic: Different runs can yield different results unless you set a random seed.

• Difficulty in Interpreting Distances: The distances in the lower-dimensional space do not have a direct interpretation; they are more about relative positioning than absolute distances.
  • ❤ 2
  • 🔥 2
More from @datascience_bds
  1. Oct 10, 2026document post
  2. Oct 9, 20268 RAG architectures for AI Engineers, visually explained:
  3. Oct 8, 2026document post
  4. Oct 7, 2026🧮 NumPy: Why axis=0 and axis=1 Feel Backwards You've probably seen: np.mean(X, axis=0) an…
  5. Oct 6, 2026document post
  6. Oct 5, 2026📊 Pandas Cheatsheet Every Data Analyst Should Save Pandas is one of the most important to…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →