APPLY_KMEANS

Assigns each row of an input relation to a cluster center from an existing k-means model.

Syntax

APPLY_KMEANS ( input‑columns 
       USING PARAMETERS model_name = 'model‑name' [, match_by_pos = match‑by‑position] )

Arguments

input‑columns Comma-separated list of columns to use from the input relation, or asterisk (*) to select all columns.

Parameters

model_name Name of the model (case-insensitive).
match_by_pos

Boolean value that specifies how input columns are matched to model features:

  • false (default): Match by name.
  • true: Match by the position of columns in the input columns list.

Privileges

Non-superusers: model owner, or USAGE privileges on the model

Examples

The following example creates k-means model myKmeansModel and applies it to input table iris1. The call to APPLY_KMEANS mixes column names and constants. When a constant is passed in place of a column name, the constant is substituted for the value of the column in all rows:

=> SELECT KMEANS('myKmeansModel', 'iris1', '*', 5
USING PARAMETERS max_iterations=20, output_view='myKmeansView', key_columns='id', exclude_columns='Species, id');
           KMEANS
----------------------------
 Finished in 12 iterations

(1 row)	
=> SELECT id, APPLY_KMEANS(Sepal_Length, 2.2, 1.3, Petal_Width 
USING PARAMETERS model_name='myKmeansModel', match_by_pos='true') FROM iris2;
 id  | APPLY_KMEANS
-----+--------------
   5 |            1
  10 |            1
  14 |            1
  15 |            1
  21 |            1
  22 |            1
  24 |            1
  25 |            1
  32 |            1
  33 |            1
  34 |            1
  35 |            1
  38 |            1
  39 |            1
  42 |            1	
…
 (60 rows)

See Also