Embedding Support Example
Input
DROP TABLE input_table_udt;
CREATE MULTISET TABLE input_table_udt(id INTEGER, array_col Vector32) PRIMARY
INDEX ( id );
INSERT INTO input_table_udt values(1, '-0.06944,0.080352,0.045963,0.006985,-0.000496');
INSERT INTO input_table_udt values(2, '-0.025461,0.045302,-0.028107,0.031248,0.00077');
INSERT INTO input_table_udt values(3, '-0.028107,0.016781,-0.023147,-0.068779,-0.07936');
INSERT INTO input_table_udt values(4, '-0.030091,0.027611,-0.047616,-0.025461,0.029595');
input_table_udt
| id |
array_col |
| 1 |
-0.06944,0.080352,0.045963,0.006985,-0.000496 |
| 2 |
-0.025461,0.045302,-0.028107,0.031248,0.00077 |
| 3 |
-0.028107,0.016781,-0.023147,-0.068779,-0.07936 |
| 4 |
-0.030091,0.027611,-0.047616,-0.025461,0.029595 |
DROP TABLE initial_centroids_table_udt;
CREATE MULTISET TABLE initial_centroids_table_udt AS (SELECT * FROM input_table_udt WHERE id in (2,4)) with data;
initial_centroids_table_udt
| id |
array_col |
| 2 |
-0.025461,0.045302,-0.028107,0.031248,0.00077 |
| 4 |
-0.030091,0.027611,-0.047616,-0.025461,0.029595 |
Query (InitialCentroidsTable not provided)
SELECT td_clusterid_kmeans, cast(array_col as Varchar(100)) AS array_col, td_size_kmeans, td_withinss_kmeans, id, td_modelinfo_kmeans
FROM TD_KMeans (
ON input_table_udt as InputTable
USING
IdColumn('id')
TargetColumns('array_col')
NumClusters(2)
Seed(0)
StopThreshold(0.0395)
MaxIterNum(3)
EmbeddingSize(5)
UseSIMD('t')
)as dt;
Output
| td_clusterid_kmeans |
array_col |
td_size_kmeans |
td_withinss_kmeans |
id |
td_modelinfo_kmeans |
| 0 |
-0.06944,0.080352,0.045963,0.006985,-0.000496 |
1 |
0.00000000000000E 000 |
NULL |
NULL |
| 1 |
-0.0278863333333333,0.029898,-0.0329566666666667,-0.0209973333333333,-0.0163316666666667 |
3 |
1.21668890066667E-002 |
NULL |
NULL |
| NULL |
NULL |
NULL |
NULL |
NULL |
Converged : True |
| NULL |
NULL |
NULL |
NULL |
NULL |
Number of Iterations : 2 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Number of Clusters : 2 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Total_WithinSS : 1.21668890066667E-02 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Between_SS : 8.65080482533333E-03 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Method for InitialCentroids : Random |
Query (InitialCentroidsTable is provided)
SELECT td_clusterid_kmeans, cast(array_col as Varchar(100)) as array_col, td_size_kmeans, td_withinss_kmeans, id, td_modelinfo_kmeans FROM TD_KMeans (
ON input_table_udt as InputTable
ON initial_centroids_table_udt as InitialCentroidsTable DIMENSION
USING
IdColumn('id')
TargetColumns('array_col')
StopThreshold(0.0395)
MaxIterNum(3)
EmbeddingSize(5)
UseSIMD('t')
)as dt;
Output
| td_cluster_kmeans |
array_col |
td_ksize_kmeans |
td_withinss_kmeans |
id |
td_modelinfo_kmeans |
| 0 |
-0.0474505,0.062827,0.008928,0.0191165,0.000137 |
2 |
4.61965788300000E-003 |
NULL |
NULL |
| 1 |
-0.029099,0.022196,-0.0353815,-0.04712,-0.0248825 |
2 |
7.23379913300000E-003 |
NULL |
NULL |
| NULL |
NULL |
NULL |
NULL |
NULL |
Converged : True |
| NULL |
NULL |
NULL |
NULL |
NULL |
Number of Iterations : 2 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Number of Clusters : 2 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Total_WithinSS : 1.18534570160000E-02 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Between_SS : 8.96423681600000E-03 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Method for InitialCentroids : Externally supplied InitialCentroidsTable |
Query (OutputClusterAssignment is set to true)
Select * from TD_KMeans (
on input_table_udt as InputTable
on initial_centroids_table_udt as InitialCentroidsTable DIMENSION
using
IdColumn('id')
TargetColumns('array_col')
StopThreshold(0.0395)
MaxIterNum(3)
OutputClusterAssignment('true')
EmbeddingSize(5)
UseSIMD('t')
)as dt;
Output
| id |
td_clusterid_kmeans |
| 1 |
0 |
| 2 |
0 |
| 3 |
1 |
| 4 |
1 |
Query (OutputClusterAssignment is set to true, and ModelTable (OUT TABLE clause) is provided)
SELECT * FROM TD_KMeans (
ON input_table_udt as InputTable
ON initial_centroids_table_udt AS InitialCentroidsTable DIMENSION
OUT TABLE ModelTable(kmeans_model_udt)
USING
IdColumn('id')
TargetColumns('array_col')
StopThreshold(0.0395)
MaxIterNum(3)
OutputClusterAssignment('true')
EmbeddingSize(5)
UseSIMD('t')
)as dt;
Output
| id |
td_clusterid_kmeans |
| 1 |
0 |
| 2 |
0 |
| 3 |
1 |
| 4 |
1 |
ModelTable: kmeans_model
| td_clusterid_kmeans |
array_col |
td_size_kmeans |
td_withinss_kmeans |
id |
td_modelinfo_kmeans |
| 0 |
-0.0474505,0.062827,0.008928,0.0191165,0.000137 |
2 |
4.61965788300000E-003 |
NULL |
NULL |
| 1 |
-0.029099,0.022196,-0.0353815,-0.04712,-0.0248825 |
2 |
7.23379913300000E-003 |
NULL |
NULL |
| NULL |
NULL |
NULL |
NULL |
NULL |
Converged : True |
| NULL |
NULL |
NULL |
NULL |
NULL |
Number of Iterations : 2 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Number of Clusters : 2 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Total_WithinSS : 1.18534570160000E-02 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Between_SS : 8.96423681600000E-03 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Method for InitialCentroids : Externally supplied InitialCentroidsTable |
Example (InitialCentroidsMethod: 'kmeans++')
Input
DROP TABLE input_table_1_udt;
CREATE TABLE input_table_1_udt(id INTEGER, array_col Vector32) Primary Index(id);
INSERT INTO input_table_1_udt values(1, '-0.06944,0.080352,0.045963,0.006985,-0.000496');
INSERT INTO input_table_1_udt values(2, '-0.025461,0.045302,-0.028107,0.031248,0.00077');
INSERT INTO input_table_1_udt values(3, '-0.028107,0.016781,-0.023147,-0.068779,-0.07936');
INSERT INTO input_table_1_udt values(4, '-0.030091,0.027611,-0.047616,-0.025461,0.029595');
INSERT INTO input_table_1_udt values(5, '0.024635,-0.025461,-0.029926,0.046294,-0.065142');
INSERT INTO input_table_1_udt values(6, '-0.019013,0.037366,-0.008019,0.065472,-0.054891');
INSERT INTO input_table_1_udt values(7, '-0.009507,0.022816,0.009341,-0.041995,0.022651');
INSERT INTO input_table_1_udt values(8, '0.028603,0.059851,0.047286,0.014467,0.002118');
INSERT INTO input_table_1_udt values(9, '0.016616,-0.009383,-0.001643,0.015624,0.002831');
input_table_1_udt
| id |
array_col |
| 1 |
-0.06944,0.080352,0.045963,0.006985,-0.000496 |
| 2 |
-0.025461,0.045302,-0.028107,0.031248,0.00077 |
| 3 |
-0.028107,0.016781,-0.023147,-0.068779,-0.07936 |
| 4 |
-0.030091,0.027611,-0.047616,-0.025461,0.029595 |
| 5 |
0.024635,-0.025461,-0.029926,0.046294,-0.065142 |
| 6 |
-0.019013,0.037366,-0.008019,0.065472,-0.054891 |
| 7 |
-0.009507,0.022816,0.009341,-0.041995,0.022651 |
| 8 |
0.028603,0.059851,0.047286,0.014467,0.002118 |
| 9 |
0.016616,-0.009383,-0.001643,0.015624,0.002831 |
Query
SELECT * FROM TD_KMeans (
ON input_table_1_udt AS InputTable
USING
IdColumn('id')
TargetColumns('array_col')
InitialCentroidsMethod('kmeans++')
NumClusters(3)
Seed(10)
StopThreshold(0.0395)
MaxIterNum(3)
EmbeddingSize(5)
UseSIMD('t')
)as dt;
Output
| td_cluster_kmeans |
array_col |
td_ksize_kmeans |
td_withinss_kmeans |
id |
td_modelinfo_kmeans |
| 0 |
-0.00203114285714286,0.022586,-0.00838342857142857,0.0150927142857143,-0.00886685714285715 |
7 |
3.17929683728571E-002 |
NULL |
NULL |
| 1 |
-0.06944,0.080352,0.045963,0.006985,-0.000496 |
1 |
0.00000000000000E 000 |
NULL |
NULL |
| 2 |
-0.028107,0.016781,-0.023147,-0.068779,-0.07936 |
1 |
0.00000000000000E 000 |
NULL |
NULL |
| NULL |
NULL |
NULL |
NULL |
NULL |
Converged : True |
| NULL |
NULL |
NULL |
NULL |
NULL |
Number of Iterations : 1 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Number of Clusters : 3 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Total_WithinSS : 3.17929683728571E-02 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Between_SS : 2.10916162209206E-02 |
| NULL |
NULL |
NULL |
NULL |
NULL |
Method for InitialCentroids : Externally supplied InitialCentroidsTable |