OpenBI Docs Buy

KMEANS OpenBI

Each row's k-means cluster, its distance to the centre and its silhouette.

Group: Many variables at once. OpenBI’s own: DAX has no function of this name.

Syntax

KMEANS ( <table>, <k>, <key>, <name>, <expression> [, <name>, <expression>] … )
ParameterTypeDescription
tableTableThe rows to cluster.
kWhole numberHow many clusters: 1 to 100.
keyExpression
evaluated for each row
What names each row in the result, such as a shop's name.
nameText
repeatable
A variable's name, a text in quotes; one or more variables.
expressionExpression
repeatable, evaluated for each row
That variable's number for each row.

Returns

Table. A row for each row of the table that has every variable: Key, Cluster (1 the largest cluster), Distance (to the cluster's centre, in standard deviations) and Silhouette.

Remarks

  • The clusters are k-means: the best of 20 starts, each seeded with centres spread far apart (the usual k-means seeding), then improved until no single row would lower the scatter by changing cluster. The starts come from a fixed seed, so the same rows always give the same clusters.
  • Clusters are numbered by size, 1 the largest.
  • Silhouette runs from −1 to 1: how much nearer a row is to its own cluster than to the next nearest. Near 1 it sits well; near 0 it is on a border; below 0 it may belong elsewhere. It is blank with k = 1, and for more than 10,000 rows.
  • Every variable is first put on one scale: how many standard deviations each row lies above or below that variable's mean. Liters and kroner then weigh alike, and a change of units changes nothing.
  • A row where any variable is blank is left out. A variable must be a number on every row (text is an error, even text that reads as a number), and one that is the same on every row is an error too.
  • An aggregate as a variable, such as SUM ( Sales[Liters] ), is the whole table's on every row and is refused as the same everywhere: use a measure, or wrap it in CALCULATE, so each row gets its own.
  • A k larger than the number of different rows is an error.

Compared with DAX

An OpenBI function: DAX has none like it, so a measure or query that uses it works in OpenBI only, not in Power BI or Analysis Services.

Examples

Small shops and big shops

EVALUATE
VAR Shops =
    DATATABLE (
        "Shop", STRING, "Liters", INTEGER, "Visits", INTEGER,
        {
            { "A", 100, 10 }, { "B", 110, 12 }, { "C", 95, 9 },
            { "D", 400, 30 }, { "E", 420, 33 }, { "F", 390, 28 }
        }
    )
RETURN
    KMEANS ( Shops, 2, [Shop], "Liters", [Liters], "Visits", [Visits] )
KeyClusterDistanceSilhouette
A10.03164240902489970.945033019082155
B10.1582120451244990.902028602979894
C10.1265696360995990.929043913284776
D20.03612733284256690.903882013835267
E20.2602437255392770.860735946174612
F20.2249012473134520.856925480179012

Three delivery areas, by location

EVALUATE
KMEANS ( Places, 3, Places[District], "Lat", Places[Lat], "Lon", Places[Lon] )
ORDER BY [Cluster], [Key]
KeyClusterDistanceSilhouette
Alnabru12.081503419304370.0419298219507738
Bjørvika10.9582342948753270.403153727958512
Grønland10.5227642663994650.514346805788568
Grünerløkka10.8007449434664590.0993110941193173
Kampen10.1027170071992160.577139862671335
Tøyen10.3739793106442410.51423434085857
Vålerenga10.5648311226968310.574874422651464
Nydalen20.7834901032338540.608326891800486
Sagene20.1333436653195440.650530967156477
St. Hanshaugen20.8664541280243320.10615557542419
Storo20.7942844005363250.588732402599997
Torshov20.408917062757960.546753273565163
Ullevål20.57517243827230.406503430425415
Frogner30.4242147610690170.59812725750929
Majorstuen30.535302954270090.384667052400157
Skøyen30.5708082062527380.614431194494613

KMEANS.CENTERS, KMEANS.ELBOW, PCA.SCORES, UMAP.

Last updated · OpenBI Studio 0.3.74