Dimensionality Reduction Techniques: PCA and t-SNE Machine Learning
Welcome to this comprehensive, student-friendly guide on dimensionality reduction! 🎉 Whether you’re just starting out or looking to deepen your understanding, this tutorial is designed to make these concepts clear and engaging. We’ll explore two powerful techniques: Principal Component Analysis (PCA) and t-distributed Stochastic Neighbor Embedding (t-SNE). Don’t worry if this seems complex at first—by the end of this guide, you’ll have a solid grasp of these techniques and how to apply them in real-world scenarios.
What You’ll Learn 📚
- Understand the core concepts of dimensionality reduction
- Learn how PCA and t-SNE work, with practical examples
- Identify when and why to use these techniques
- Troubleshoot common issues and mistakes
Introduction to Dimensionality Reduction
In the world of data science, we often deal with datasets that have a large number of features. While more data can provide more insights, it can also make analysis complex and computationally expensive. This is where dimensionality reduction comes in! It helps simplify datasets by reducing the number of features while preserving important information.
Key Terminology
- Dimensionality Reduction: The process of reducing the number of random variables under consideration by obtaining a set of principal variables.
- Principal Component Analysis (PCA): A technique used to emphasize variation and bring out strong patterns in a dataset.
- t-distributed Stochastic Neighbor Embedding (t-SNE): A machine learning algorithm for visualization that is particularly good at reducing the dimensionality of data to two or three dimensions.
Principal Component Analysis (PCA)
Simple Example: Understanding PCA with a 2D Dataset
import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
# Create a simple 2D dataset
X = np.array([[2.5, 2.4],
[0.5, 0.7],
[2.2, 2.9],
[1.9, 2.2],
[3.1, 3.0],
[2.3, 2.7],
[2, 1.6],
[1, 1.1],
[1.5, 1.6],
[1.1, 0.9]])
# Apply PCA
pca = PCA(n_components=1)
X_reduced = pca.fit_transform(X)
# Plot the original and reduced data
plt.scatter(X[:, 0], X[:, 1], color='blue', label='Original Data')
plt.scatter(X_reduced, np.zeros_like(X_reduced), color='red', label='Reduced Data')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.title('PCA Example')
plt.show()
This example demonstrates PCA on a simple 2D dataset. We reduce the data to 1 dimension and visualize the transformation. Notice how the red points (reduced data) capture the essence of the blue points (original data) along one axis.

Progressively Complex Examples
Example 1: PCA on a 3D Dataset
# Import necessary libraries
from mpl_toolkits.mplot3d import Axes3D
# Create a simple 3D dataset
X_3d = np.random.rand(100, 3) * 100
# Apply PCA to reduce to 2D
pca_3d = PCA(n_components=2)
X_3d_reduced = pca_3d.fit_transform(X_3d)
# Plot the original 3D data
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X_3d[:, 0], X_3d[:, 1], X_3d[:, 2], color='blue', label='Original 3D Data')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.title('Original 3D Data')
plt.show()
# Plot the reduced 2D data
plt.scatter(X_3d_reduced[:, 0], X_3d_reduced[:, 1], color='red', label='Reduced 2D Data')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA Reduced 2D Data')
plt.legend()
plt.show()
Here, we take a 3D dataset and use PCA to reduce it to 2D. This is useful for visualizing high-dimensional data in a lower dimension.

Example 2: PCA on a Real Dataset
from sklearn.datasets import load_iris
# Load the Iris dataset
iris = load_iris()
X_iris = iris.data
# Apply PCA to reduce to 2D
pca_iris = PCA(n_components=2)
X_iris_reduced = pca_iris.fit_transform(X_iris)
# Plot the reduced data
plt.scatter(X_iris_reduced[:, 0], X_iris_reduced[:, 1], c=iris.target, cmap='viridis', label='Iris Data')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA on Iris Dataset')
plt.colorbar()
plt.show()
In this example, we use PCA on the famous Iris dataset to reduce its dimensionality from 4 to 2. This helps in visualizing the data while maintaining the structure.

t-distributed Stochastic Neighbor Embedding (t-SNE)
Understanding t-SNE with a Simple Example
from sklearn.manifold import TSNE
# Apply t-SNE to the Iris dataset
tsne = TSNE(n_components=2, random_state=42)
X_tsne = tsne.fit_transform(X_iris)
# Plot the t-SNE reduced data
plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=iris.target, cmap='viridis', label='t-SNE Data')
plt.xlabel('t-SNE Component 1')
plt.ylabel('t-SNE Component 2')
plt.title('t-SNE on Iris Dataset')
plt.colorbar()
plt.show()
t-SNE is particularly useful for visualizing high-dimensional data in 2D or 3D. Here, we apply t-SNE to the Iris dataset, which helps in identifying clusters more distinctly than PCA.

Common Questions and Answers
- What is the main difference between PCA and t-SNE?
PCA is a linear technique that reduces dimensions by finding the principal components, while t-SNE is a non-linear technique that focuses on preserving local relationships in data.
- When should I use PCA over t-SNE?
Use PCA when you need a quick, linear reduction and when interpretability of components is important. Use t-SNE for visualization and when capturing non-linear relationships is crucial.
- Why does t-SNE take longer to run than PCA?
t-SNE is computationally more intensive because it focuses on preserving local structures, which requires more calculations.
- Can PCA and t-SNE be used together?
Yes, it’s common to use PCA to reduce dimensions first, followed by t-SNE for visualization. This can speed up the t-SNE process.
Troubleshooting Common Issues
If your t-SNE visualization looks like a random scatter, try adjusting the perplexity parameter. It’s a common issue that can affect the quality of the visualization.
Remember, practice makes perfect! Try applying these techniques to different datasets to see how they perform. 🚀
Practice Exercises
- Apply PCA to the Wine dataset from scikit-learn and visualize the results.
- Experiment with different perplexity values in t-SNE and observe the changes in visualization.
For more information, check out the scikit-learn PCA documentation and the scikit-learn t-SNE documentation.