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>] … )
| Parameter | Type | Description |
|---|---|---|
table | Table | The rows to cluster. |
k | Whole number | How many clusters: 1 to 100. |
key | Expression evaluated for each row | What names each row in the result, such as a shop's name. |
name | Text repeatable | A variable's name, a text in quotes; one or more variables. |
expression | Expression 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 inCALCULATE, 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] )
| Key | Cluster | Distance | Silhouette |
|---|---|---|---|
| A | 1 | 0.0316424090248997 | 0.945033019082155 |
| B | 1 | 0.158212045124499 | 0.902028602979894 |
| C | 1 | 0.126569636099599 | 0.929043913284776 |
| D | 2 | 0.0361273328425669 | 0.903882013835267 |
| E | 2 | 0.260243725539277 | 0.860735946174612 |
| F | 2 | 0.224901247313452 | 0.856925480179012 |
Three delivery areas, by location
EVALUATE
KMEANS ( Places, 3, Places[District], "Lat", Places[Lat], "Lon", Places[Lon] )
ORDER BY [Cluster], [Key]
| Key | Cluster | Distance | Silhouette |
|---|---|---|---|
| Alnabru | 1 | 2.08150341930437 | 0.0419298219507738 |
| Bjørvika | 1 | 0.958234294875327 | 0.403153727958512 |
| Grønland | 1 | 0.522764266399465 | 0.514346805788568 |
| Grünerløkka | 1 | 0.800744943466459 | 0.0993110941193173 |
| Kampen | 1 | 0.102717007199216 | 0.577139862671335 |
| Tøyen | 1 | 0.373979310644241 | 0.51423434085857 |
| Vålerenga | 1 | 0.564831122696831 | 0.574874422651464 |
| Nydalen | 2 | 0.783490103233854 | 0.608326891800486 |
| Sagene | 2 | 0.133343665319544 | 0.650530967156477 |
| St. Hanshaugen | 2 | 0.866454128024332 | 0.10615557542419 |
| Storo | 2 | 0.794284400536325 | 0.588732402599997 |
| Torshov | 2 | 0.40891706275796 | 0.546753273565163 |
| Ullevål | 2 | 0.5751724382723 | 0.406503430425415 |
| Frogner | 3 | 0.424214761069017 | 0.59812725750929 |
| Majorstuen | 3 | 0.53530295427009 | 0.384667052400157 |
| Skøyen | 3 | 0.570808206252738 | 0.614431194494613 |