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}:
12.9293 -8.31783 0.672177
-8.07359 1.00253 4.28689
2.7955 7.80959 6.91284
... (1000, 3)
labs = vec(L)
tab(labs)
OrderedCollections.OrderedDict{Int64, Int64} with 4 entries:
0 => 155
1 => 339
2 => 335
3 => 171
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}:
-7.48938 -7.48381
3.81531 10.2805
-5.31822 3.6551
... (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}:
-9.62654 0.796588
3.01404 -4.81665
9.02768 -2.63106
... (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}:
11.8462 -39.3409
-28.3948 12.1073
-1.34849 39.8881
... (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.202924 0.193962
0.344041 -0.551969
-0.304295 -0.322843
... (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}:
-929.434 -1099.51
347.469 -189.924
-453.346 459.152
... (1000, 2)