Skip to content

Instantly share code, notes, and snippets.

@mikelove
Last active April 6, 2024 01:11
Show Gist options
  • Select an option

  • Save mikelove/74bbf5c41010ae1dc94281cface90d32 to your computer and use it in GitHub Desktop.

Select an option

Save mikelove/74bbf5c41010ae1dc94281cface90d32 to your computer and use it in GitHub Desktop.

Revisions

  1. mikelove revised this gist Jun 1, 2016. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions tsne.R
    Original file line number Diff line number Diff line change
    @@ -3,21 +3,22 @@ m <- 40
    set.seed(1)
    x <- runif(n, -1, 1)
    library(rafalib)
    bigpar(2,2)
    bigpar(2,2,mar=c(3,3,3,1))
    library(RColorBrewer)
    cols <- brewer.pal(11, "Spectral")[as.integer(cut(x, 11))]
    plot(x, rep(0,n), ylim=c(-1,1), yaxt="n", ylab="", col=cols, pch=20)
    plot(x, rep(0,n), ylim=c(-1,1), yaxt="n", xlab="", ylab="",
    col=cols, pch=20, main="underlying data")
    library(pracma)
    ortho <- rortho(m)
    X <- cbind(x, matrix(0,ncol=m-1,nrow=n)) %*% ortho
    plot(X[,1:2], asp=1, col=cols, pch=20, xlab="dim1", ylab="dim2")
    plot(X[,1:2], asp=1, col=cols, pch=20, xlab="", ylab="", main="embed in higher dim")
    pc <- prcomp(X)
    plot(pc$x[,1:2], asp=1, col=cols, pch=20)
    plot(pc$x[,1:2], asp=1, col=cols, pch=20, xlab="", ylab="", main="PC1 & PC2")
    library(tsne)
    res <- tsne(X)
    plot(res, col=cols, pch=20, xlab="", ylab="")
    plot(res, col=cols, pch=20, xlab="", ylab="", main="t-SNE")

    bigpar(2,2)
    bigpar(2,2,mar=c(3,3,1,1))
    for (i in 2:5) {
    set.seed(i)
    x <- runif(n, -1, 1)
  2. mikelove created this gist Jun 1, 2016.
    29 changes: 29 additions & 0 deletions tsne.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    n <- 200
    m <- 40
    set.seed(1)
    x <- runif(n, -1, 1)
    library(rafalib)
    bigpar(2,2)
    library(RColorBrewer)
    cols <- brewer.pal(11, "Spectral")[as.integer(cut(x, 11))]
    plot(x, rep(0,n), ylim=c(-1,1), yaxt="n", ylab="", col=cols, pch=20)
    library(pracma)
    ortho <- rortho(m)
    X <- cbind(x, matrix(0,ncol=m-1,nrow=n)) %*% ortho
    plot(X[,1:2], asp=1, col=cols, pch=20, xlab="dim1", ylab="dim2")
    pc <- prcomp(X)
    plot(pc$x[,1:2], asp=1, col=cols, pch=20)
    library(tsne)
    res <- tsne(X)
    plot(res, col=cols, pch=20, xlab="", ylab="")

    bigpar(2,2)
    for (i in 2:5) {
    set.seed(i)
    x <- runif(n, -1, 1)
    cols <- brewer.pal(11, "Spectral")[as.integer(cut(x, 11))]
    ortho <- rortho(m)
    X <- cbind(x, matrix(0,ncol=m-1,nrow=n)) %*% ortho
    res <- tsne(X)
    plot(res, col=cols, pch=20, xlab="", ylab="")
    }