clustering

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package clustering is a require()-able host module exposing unsupervised clustering algorithms (k-means, DBSCAN, agglomerative/hierarchical, and mean shift) to .lsc code. Data is passed as an array of points, where a point is itself an array of numbers; results report 1-based cluster ids so they index naturally from Lua.

Index

Constants

View Source
const (
	Noise = 0
)

DBSCAN labels. Cluster ids start at 1; Noise marks points that fall in no dense region, and undefined is the internal "not yet visited" state.

Variables

This section is empty.

Functions

func DBSCAN

func DBSCAN(data []Point, eps float64, minPts int) []int

DBSCAN performs density-based spatial clustering. A point is a core point if at least minPts points (including itself) lie within eps; clusters grow by transitively absorbing the neighbourhoods of core points. Points reachable from no core point are labelled Noise (0).

Returned labels are 1-based cluster ids, with 0 reserved for noise. Unlike k-means, DBSCAN discovers the cluster count on its own and finds arbitrarily shaped clusters.

func Hierarchical

func Hierarchical(data []Point, k int, linkage Linkage) []int

Hierarchical performs bottom-up agglomerative clustering: every point starts as its own cluster and the two closest clusters are merged repeatedly until exactly k clusters remain. The pairwise point-distance matrix is precomputed once; linkage decides how cluster-to-cluster distance is derived from it.

This is an O(n^3) implementation suited to small/medium inputs. Returned labels are 1-based cluster ids.

func RegisterClusteringPreload

func RegisterClusteringPreload(v *vm.VM)

RegisterClusteringPreload installs the loader under package.preload so the module table is built lazily on the first require("clustering").

Types

type KMeansResult

type KMeansResult struct {
	Centers     []Point
	Assignments []int
	Iterations  int
}

KMeansResult bundles the converged cluster centers with the per-point cluster assignment. Assignments are 0-based indices into Centers.

func KMeans

func KMeans(data []Point, k, maxIter int, delta float64, rng *rand.Rand) KMeansResult

KMeans partitions data into k clusters using Lloyd's algorithm with k-means++ seeding. It iterates until no center moves more than delta or maxIter is reached. Empty clusters keep their previous center rather than collapsing to NaN.

type Linkage

type Linkage int

Linkage selects how the distance between two clusters is measured during agglomerative merging.

const (
	// SingleLinkage uses the closest pair of members (min distance).
	SingleLinkage Linkage = iota
	// CompleteLinkage uses the farthest pair of members (max distance).
	CompleteLinkage
	// AverageLinkage uses the mean over all cross-cluster member pairs.
	AverageLinkage
)

func ParseLinkage

func ParseLinkage(s string) Linkage

ParseLinkage maps a string name to a Linkage, defaulting to average.

type MeanShiftResult

type MeanShiftResult struct {
	Centers     []Point
	Assignments []int
	Iterations  int
}

MeanShiftResult bundles the discovered modes (cluster centers) with the per-point assignment. Like DBSCAN, mean shift discovers the number of clusters on its own — here it is governed by the bandwidth.

func MeanShift

func MeanShift(data []Point, bandwidth float64, maxIter int) MeanShiftResult

MeanShift is a centroid-seeking, non-parametric clusterer. Every point is iteratively shifted toward the weighted mean of its neighbourhood (using a Gaussian kernel parameterised by bandwidth) until convergence; points that settle on the same mode form a cluster.

Smaller bandwidths yield more, tighter clusters. Returned assignments are 1-based cluster ids.

type Point

type Point struct {
	Entry []float64
}

Point is a single observation in feature space. Entry holds the coordinates; every Point fed to an algorithm is expected to share the same dimensionality.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL