Manifold - swissroll - Various methods

using Jchemo, JchemoData
using JLD2, DataFrames, CairoMakie, GLMakie
using LinearAlgebra, Random
using ManifoldLearning

Data simulation

n = 1000
noise = .5  # "vertical" noise (axis3)
#noise = 2
segments = 4
hlims = (-10.0, 10.0)  # seems to impact only axis2
rng = TaskLocalRNG()
#rng = MersenneTwister(1234)
Xt, L = ManifoldLearning.swiss_roll(n, noise; segments, hlims, rng)
@head X = Xt'
3×3 Matrix{Float64}:
  4.36871  -2.06454   5.53998
  9.65313  -4.66786   8.61663
 10.4242    5.63459  -4.81184
... (1000, 3)
labs = vec(L)
tab(labs)
OrderedCollections.OrderedDict{Int64, Int64} with 4 entries:
  0 => 140
  1 => 342
  2 => 351
  3 => 167
CairoMakie.activate!()  
#GLMakie.activate!()    # for interactive axe-rotation
mks = 10
i = 1
plotxyz(X[:, i], X[:, i + 1], X[:, i + 2], labs; size = (700, 500), markersize = 10, 
    xlabel = string("x", i), ylabel = string("x", i + 1), zlabel = string("x", i + 2), 
    title = "Swiss roll").f

Pca

nlv = 2
model = pcasvd(; nlv)
fit!(model, X)
@head T = model.fitm.T
3×2 Matrix{Float64}:
  -5.80096   0.677087
 -11.2634   -2.15523
  -1.10007  -9.33504
... (1000, 2)
i = 1
plotxy(T[:, i], T[:, i + 1]; color = labs,
    xlabel = string("LV", i), ylabel = string("LV", i + 1), title = "Pca").f

Umap

nlv = 2
n_neighbors = 15; min_dist = .5 
model = umap(; nlv, n_neighbors, min_dist)
fit!(model, X)
@head T = model.fitm.T
3×2 Matrix{Float64}:
 -7.04982  -1.34897
  9.14001  -5.14501
  5.37099  -1.10469
... (1000, 2)
i = 1
plotxy(T[:, i], T[:, i + 1]; color = labs,
    xlabel = string("LV", i), ylabel = string("LV", i + 1), title = "Umap").f

Tsne

p = 30
maxoutdim = 2
M = ManifoldLearning.fit(TSNE, X'; p, maxoutdim) 
Tt =  ManifoldLearning.predict(M) 
@head T = Tt'
3×2 Matrix{Float64}:
  6.72839   28.8473
 20.2691   -32.4656
 -1.70889  -21.8654
... (1000, 2)
i = 1
plotxy(T[:, i], T[:, i + 1]; color = labs,
    xlabel = string("LV", i), ylabel = string("LV", i + 1), title = "t-SNE").f

Kpca

nlv = 2
kern = :krbf; gamma = 1e-2
model = kpca(; nlv, kern, gamma)
fit!(model, X)
@head T = model.fitm.T
3×2 Matrix{Float64}:
 -0.548349   0.0191093
 -0.44026    0.0750678
  0.0387215  0.494148
... (1000, 2)
i = 1
plotxy(T[:, i], T[:, i + 1]; color = labs,
    xlabel = string("LV", i), ylabel = string("LV", i + 1), title = "Kpca").f
nlv = 2
kern = :kpol
gamma = 1; degree = 3; coef0 = 10
model = kpca(; nlv, kern, degree, gamma, coef0)
fit!(model, X)
@head T = model.fitm.T  
i = 1
plotxy(T[:, i], T[:, i + 1]; color = labs,
    xlabel = string("LV", i), ylabel = string("LV", i + 1), title = "Kpca").f
3×2 Matrix{Float64}:
  -136.682    65.3735
 -1764.36   1289.13
   100.173  -166.453
... (1000, 2)