Skip to content

Instantly share code, notes, and snippets.

@simon-anders
Created November 4, 2019 16:26
Show Gist options
  • Save simon-anders/c72a947184deea8ba2f91a05951c3a79 to your computer and use it in GitHub Desktop.
Save simon-anders/c72a947184deea8ba2f91a05951c3a79 to your computer and use it in GitHub Desktop.
# Swiss roll and Sleepwalk
n <- 10000
# Draw random position on a rectangle
x0 <- runif( n, 0, 2*pi )
y0 <- runif( n, 0, 6*pi )
# Roll up the rectangle to form a Swiss roll
x <- x0 + rnorm( n, 0, .3 )
y <- y0 * cos( y0 ) + rnorm( n, 0, .3 )
z <- y0 * sin( y0 ) + rnorm( n, 0, .3 )
# Plot an x-z cut to show the roll's cross section
plot( y, z, asp=1, col=rje::cubeHelix(n*1.2)[rank(y0)] )
# Calculate a dimension-reduced embedding
ump <- uwot::umap( cbind( x, y, z ) )
# Plot the UMAP embedding
plot(ump, asp=1, col=rje::cubeHelix(n*1.2)[rank(y0)] )
# Visualize the embedding with Sleepwalk
sleepwalk::sleepwalk( ump, cbind( x, y, z ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment