The goal is to expand the functionality of the “DamianCalin/SQLOperators” package to include certain machine learning capabilities. https://resources.wolframcloud.com/PacletRepository/resources/DamianCalin/SQLOperators/​This post introduces “findClustersSQL,” which serves as a wrapper for the Wolfram “FindClusters” function when applied to SQL tables.​The syntax for the new function is as follows:​findClustersSQL [Input table , {column list } , OptionsPattern ]​​​findClustersSQL [Input table , { column list } , Association ] ​​findClustersSQL [Input table , {cluster column list } , { Association list} ]​​The function is overloaded, and all its usages will be demonstrated in the following section.

Demo

Install SQL Paclet

Install the last version of “DamianCalin/SQLOperators” paclet.
https://resources.wolframcloud.com/PacletRepository/resources/DamianCalin/SQLOperators/
In[]:=
pclt=PacletInstall["DamianCalin/SQLOperators"];​​Needs["DamianCalin`SQLOperators`"]​​

Get sample data

We’ll use the Fisher’s Irises dataset, which is a record of 150 specimens of irises and some of their characteristics:
In[]:=
tbIrises=ResourceData["Sample Data: Fisher's Irises"]//​​datasetAsTableSQL[#]&;​​​​tbIrises//​​tableSQLAsDataset​​
Out[]=
Species
SepalLength
SepalWidth
PetalLength
PetalWidth
setosa
5.1cm
3.5cm
1.4cm
0.2cm
setosa
4.9cm
3.cm
1.4cm
0.2cm
setosa
4.7cm
3.2cm
1.3cm
0.2cm
setosa
4.6cm
3.1cm
1.5cm
0.2cm
setosa
5.cm
3.6cm
1.4cm
0.2cm
setosa
5.4cm
3.9cm
1.7cm
0.4cm
setosa
4.6cm
3.4cm
1.4cm
0.3cm
setosa
5.cm
3.4cm
1.5cm
0.2cm
setosa
4.4cm
2.9cm
1.4cm
0.2cm
setosa
4.9cm
3.1cm
1.5cm
0.1cm
rows 1–10 of 150

Obtaining clusters based on specific columns selected from a table

We want to cluster data based on petal length and sepal width
The basic use of function use default clustering options and returns a list of tables with same structure as input table
In[]:=
clusters=findClustersSQL[tbIrises,{PetalLength,SepalWidth}];
Display tables
In[]:=
tableSQLAsDataset/@clusters
Out[]=

Species
SepalLength
SepalWidth
PetalLength
PetalWidth
setosa
5.1cm
3.5cm
1.4cm
0.2cm
setosa
4.9cm
3.cm
1.4cm
0.2cm
setosa
4.7cm
3.2cm
1.3cm
0.2cm
setosa
4.6cm
3.1cm
1.5cm
0.2cm
setosa
5.cm
3.6cm
1.4cm
0.2cm
setosa
5.4cm
3.9cm
1.7cm
0.4cm
setosa
4.6cm
3.4cm
1.4cm
0.3cm
setosa
5.cm
3.4cm
1.5cm
0.2cm
setosa
4.4cm
2.9cm
1.4cm
0.2cm
setosa
4.9cm
3.1cm
1.5cm
0.1cm
rows 1–10 of 49
,
Species
SepalLength
SepalWidth
PetalLength
PetalWidth
setosa
4.5cm
2.3cm
1.3cm
0.3cm
versicolor
7.cm
3.2cm
4.7cm
1.4cm
versicolor
6.4cm
3.2cm
4.5cm
1.5cm
versicolor
6.9cm
3.1cm
4.9cm
1.5cm
versicolor
5.5cm
2.3cm
4.cm
1.3cm
versicolor
6.5cm
2.8cm
4.6cm
1.5cm
versicolor
5.7cm
2.8cm
4.5cm
1.3cm
versicolor
6.3cm
3.3cm
4.7cm
1.6cm
versicolor
4.9cm
2.4cm
3.3cm
1.cm
versicolor
6.6cm
2.9cm
4.6cm
1.3cm
rows 1–10 of 101

The automatic function FindClusters identified two clusters, one with 49 examples and one with 101 examples.
Various options are available to modify the clustering behavior. You can explore the complete list and details of these options in the official Wolfram Language documentation for FindClusters.
clusters=findClustersSQL[tbIrises,{PetalLength,SepalWidth},3,Method->"KMeans",FeatureExtractor->"Minimal"];​​tableSQLAsDataset/@clusters
Out[]=

Species
SepalLength
SepalWidth
PetalLength
PetalWidth
setosa
5.1cm
3.5cm
1.4cm
0.2cm
setosa
4.9cm
3.cm
1.4cm
0.2cm
setosa
4.7cm
3.2cm
1.3cm
0.2cm
setosa
4.6cm
3.1cm
1.5cm
0.2cm
setosa
5.cm
3.6cm
1.4cm
0.2cm
setosa
5.4cm
3.9cm
1.7cm
0.4cm
setosa
4.6cm
3.4cm
1.4cm
0.3cm
setosa
5.cm
3.4cm
1.5cm
0.2cm
setosa
4.4cm
2.9cm
1.4cm
0.2cm
setosa
4.9cm
3.1cm
1.5cm
0.1cm
rows 1–10 of 49
,
Species
SepalLength
SepalWidth
PetalLength
PetalWidth
setosa
4.5cm
2.3cm
1.3cm
0.3cm
versicolor
5.5cm
2.3cm
4.cm
1.3cm
versicolor
6.5cm
2.8cm
4.6cm
1.5cm
versicolor
5.7cm
2.8cm
4.5cm
1.3cm
versicolor
4.9cm
2.4cm
3.3cm
1.cm
versicolor
5.2cm
2.7cm
3.9cm
1.4cm
versicolor
5.cm
2.cm
3.5cm
1.cm
versicolor
6.cm
2.2cm
4.cm
1.cm
versicolor
5.6cm
2.9cm
3.6cm
1.3cm
versicolor
5.8cm
2.7cm
4.1cm
1.cm
rows 1–10 of 47
,
Species
SepalLength
SepalWidth
PetalLength
PetalWidth
versicolor
7.cm
3.2cm
4.7cm
1.4cm
versicolor
6.4cm
3.2cm
4.5cm
1.5cm
versicolor
6.9cm
3.1cm
4.9cm
1.5cm
versicolor
6.3cm
3.3cm
4.7cm
1.6cm
versicolor
6.6cm
2.9cm
4.6cm
1.3cm
versicolor
5.9cm
3.cm
4.2cm
1.5cm
versicolor
6.1cm
2.9cm
4.7cm
1.4cm
versicolor
6.7cm
3.1cm
4.4cm
1.4cm
versicolor
5.6cm
3.cm
4.5cm
1.5cm
versicolor
5.9cm
3.2cm
4.8cm
1.8cm
rows 1–10 of 54


Incorporate clusters to table as column

To integrate clustering into the table structure, you need to provide the following association as an additional parameter:
<|”ClusterName” -> “New Column Name” ,
“ClusterOptions”-> {Method->”KMeans” ...} |>
”ClusterName” key is mandatory .
The result will be a table containing new column containing cluster group values.
In[]:=
​​tbCluster=​​tbIrises//​​findClustersSQL#,{PetalLength,SepalWidth},​​ <|"ClusterName"->"ClusterKMeans",​​ "ClusterOptions"->{3,Method->"KMeans",FeatureExtractor->"Minimal"}​​ |>&;​​tbCluster//​​tableSQLAsDataset
Out[]=
Species
SepalLength
SepalWidth
PetalLength
PetalWidth
ClusterKMeans
setosa
5.1cm
3.5cm
1.4cm
0.2cm
1
setosa
4.9cm
3.cm
1.4cm
0.2cm
1
setosa
4.7cm
3.2cm
1.3cm
0.2cm
1
setosa
4.6cm
3.1cm
1.5cm
0.2cm
1
setosa
5.cm
3.6cm
1.4cm
0.2cm
1
setosa
5.4cm
3.9cm
1.7cm
0.4cm
1
setosa
4.6cm
3.4cm
1.4cm
0.3cm
1
setosa
5.cm
3.4cm
1.5cm
0.2cm
1
setosa
4.4cm
2.9cm
1.4cm
0.2cm
1
setosa
4.9cm
3.1cm
1.5cm
0.1cm
1
rows 1–10 of 150
Now that tbCluster we can use groupBy to explore cluster aggregates
Plot clusters

Define multiples clusters models on table

Multiple cluster models on the same table can be defined by specifying a list of associations containing cluster names and model options.
The outcome will be a table with as many new columns as there are cluster models.
In order to use groupBy and comparing the models we have to transpose or pivot on cluster columns.
Plot clusters multiple cluster models

Comments/Suggestions/Donations

CITE THIS NOTEBOOK

SQLOperators package: Introducing new function findClustersSQL​
by Damian Calin​
Wolfram Community, STAFF PICKS, November 29, 2023
​https://community.wolfram.com/groups/-/m/t/3073895