sciBASIC# knot logo sciBASIC#

03 Tutorial — UMAP manifold

Project 60,000 handwritten digits onto a 2-D UMAP manifold, colored by label.

Module DataMining · UMAP Dataset MNIST train · 60,000 × 784 Embedding 2-D · 128 neighbors Output PNG · 300 dpi

The MNIST training split — 60,000 grayscale 28 × 28 digits — is read straight from the original idx3/idx1 binary files, wrapped as ClusterEntity rows and pushed through the sciBASIC# UMAP implementation: 2 target dimensions, 128 nearest neighbors. After InitializeFit reports the epoch schedule, a single Step(n_epochs) optimizes the embedding, and the resulting manifold is rendered as a Nature-themed scatter in which every point carries its digit label as color.

02 Pipeline

From idx binaries to a labeled manifold

Step 1

Load

MNIST reads train-images-idx3-ubyte + train-labels-idx1-ubyte; ExtractDataSet(Of ClusterEntity) yields labeled 784-D feature rows.

Step 2

Configure

new umap(dimensions:=2, numberOfNeighbors:=128) builds the fuzzy simplicial set over the 784-dimensional space.

Step 3

Fit

InitializeFit(dataset) constructs the graph and returns the epoch schedule n_epochs.

Step 4

Embed

umap.Step(n_epochs) optimizes the low-dimensional layout; GetEmbedding() returns one 2-D coordinate per image.

Step 5

Plot

A ScatterPlot (800 × 600, PlotTheme.Nature) draws UMAP1 vs UMAP2 with each point colored by its digit label.

01 The Script

Full demo source

The complete script exactly as executed by the sciBASIC# script engine (vbs.exe) — nothing elided.

mnist_umap.vb · 40 linesDownload mnist_umap.vb
#include "Microsoft.VisualBasic.DataMining.UMAP.dll"
#include "Microsoft.VisualBasic.MachineLearning.DataStorage.dll"
#include "Microsoft.VisualBasic.DataMining.Framework.dll"
#include "Microsoft.VisualBasic.Drawing.dll"
#include "Microsoft.VisualBasic.Data.DataPlot.dll"
#include "Microsoft.VisualBasic.Math.Randomizer.dll"

imports Microsoft.VisualBasic.MachineLearning.DataStorage
imports Microsoft.VisualBasic.DataMining.ComponentModel.EntityModels
imports Microsoft.VisualBasic.DataMining.UMAP
imports Microsoft.VisualBasic.DataMining
imports Microsoft.VisualBasic.Scripting.Runtime
imports microsoft.visualbasic.data.plots
imports microsoft.visualbasic.drawing

dim mnist as new MNIST(
    "G:\GCModeller\src\R-sharp\test\demo\machineLearning\umap\mnist_dataset\train-images-idx3-ubyte", 
    "G:\GCModeller\src\R-sharp\test\demo\machineLearning\umap\mnist_dataset\train-labels-idx1-ubyte")
dim dataset = mnist.ExtractDataSet(of ClusterEntity)().toarray()
dim umap as new umap(dimensions := 2,numberOfNeighbors := 128 )
dim n_epochs As Integer = umap.InitializeFit(dataset)
dim number = dataset.ClassId().ascharacter().toarray()

Call umap.Step(n_epochs)

dim manifold = umap.GetEmbedding()
dim x = from v as double() in manifold select v(0)
dim y = from v as double() in manifold select v(1)

call SkiaDriver.Register()

Using plt As New ScatterPlot(800, 600, PlotTheme.Nature())
    plt.Title = "MNIST dataset UMAP embedding"
    plt.SubTitle = "UMAP manifold of 2 dimensions"
    plt.XLabel = "UMAP1"
    plt.YLabel = "UMAP2"
    plt.Plot(DataSerials(x:=x.toarray(),y:=y.toarray(), number).tolist())
    plt.SavePng("Z:/mnist-umap.png", 300)
End Using

03 Results

The 2-D manifold

UMAP embedding of the MNIST training split, points colored by digit label
Fig. 1 — UMAP embedding of the MNIST training split; every point is one handwritten digit, colored by its label. The manifold resolves the handwriting styles into well-separated islands, and structurally similar digits settle into neighboring lobes of the embedding.
UMAP preserves local neighborhood structure: images that look alike stay adjacent after the 784-D → 2-D projection, so the digit classes emerge as islands without any use of the labels during optimization — labels only provide the colors in the final scatter.