kmeans
kmeans · K-means clustering window function for PostgreSQL
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100132 | kmeans | kmeans | 1.1.0 | RAG | PostgreSQL | C |
PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18
Usage
Sources:
kmeans version 1.1.0 implements K-means clustering as a PostgreSQL window function. It returns a zero-based cluster number for every input row and is intended for relatively small, in-database vector classifications.
Core Workflow
All vectors in a window partition must be one-dimensional and have the same length. The second argument is the requested number of clusters.
For reproducible initialization, supply centroids explicitly:
The third argument may be a two-dimensional centroid array or a flat array whose length equals cluster count times vector dimension.
Behavior and Caveats
kmeans(float[], int) returns int: choose initial centroids from input vectors after interpolating between per-dimension minima and maxima.kmeans(float[], int, float[]) returns int: use caller-supplied initial centroids.
K-means results depend on initialization and scale. Normalize dimensions when appropriate, supply fixed centroids when repeatability matters, and test NULLs and degenerate partitions. This upstream release dates from 2011 and was designed for PostgreSQL 8.4-era APIs; verify source compatibility and results on the target PostgreSQL version. No preload is required.