tSNE Parameter Tuning in R

Introduction

Welcome! Today's focus is on t-SNE parameter tuning using R and the Rtsne package. This lesson covers an understanding of critical t-SNE parameters, the practice of parameter tuning, and its impact on data visualization outcomes in R.

Preparing the data

Before delving into parameter tuning, let's quickly set up the dataset:

Here's a basic setup in R:

# Load required libraries
library(ggplot2)

# Generate a non-linearly separable dataset (two circles) like sklearn.make_circles
set.seed(42)
n_samples <- 500
factor <- 0.3
noise  <- 0.1

half <- n_samples / 2
theta_outer <- runif(half, 0, 2*pi)
theta_inner <- runif(half, 0, 2*pi)

r_outer <- 1 + rnorm(half, sd = noise)
r_inner <- factor + rnorm(half, sd = noise)

X <- rbind(
  cbind(r_outer * cos(theta_outer), r_outer * sin(theta_outer)),
  cbind(r_inner * cos(theta_inner), r_inner * sin(theta_inner))
)
X <- as.data.frame(X)
colnames(X) <- c("x1", "x2")
y <- as.factor(c(rep(0, half), rep(1, half)))

# Plot the dataset
plot <- ggplot(X, aes(x = x1, y = x2, color = y)) +
  geom_point(size = 2) +
  labs(title = "Original Data", color = "Class") +
  theme_minimal() + theme( panel.background = element_rect(fill = "white", color = NA), plot.background = element_rect(fill = "white", color = NA) )  + coord_equal()

Understanding t-SNE Parameters: Perplexity

We will now delve into the key parameters in R's t-SNE implementation (Rtsne). The first one is perplexity, which is loosely determined by the number of effective nearest neighbors. It strikes a balance between preserving the local and global data structure.

Understanding t-SNE Parameters: Early Exaggeration

The next parameter is exaggeration_factor (in Rtsne, this replaces early_exaggeration). It governs how tight natural clusters are in the embedded space. High values tend to make clusters denser.

Understanding t-SNE Parameters: Learning Rate

The final parameter, eta, modulates the step size for the gradient during the optimization process.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal