Clustering in SQLFlow to Analyze Patterns in Data

ClusterModel Introduction

Most of time when businessman and analyst faced the data, they need not only the supervised learning model to perform classification and prediction, but also unsupervised learning to catch hidden patterns. This can help analysts to draw inferences from datasets consisting of input data without labeled responses, such as grouping users by their behavioral characteristics.

This design document introduced how to support the Cluster Model in SQLFlow.

The figure below demonstrates the overall workflow for cluster model training, which include both the pre_train autoencoder model and the clustering model.(Reference https://www.dlology.com/blog/how-to-do-unsupervised-clustering-with-keras/)

Clustering in SQLFlow to Analyze Patterns in Data - 图1

  1. The first part is used to load a pre_trained model. We use the output of the trained encoder layer as the input to the clustering model.
  2. Then, the clustering model starts training with randomly initialized weights, and generate clusters after multiple iterations.
  3. The overall train process ultimately outputs an unsupervised clustering model.

How to Implement a ClusterModel in SQLFlow

User Interface in SQLFlow

In this scenario, we focus on the extraction of data patterns in unsupervised learning.

So, the user can use TO TRAIN keyword to training a model. The user can also specify the training hyper-parameters with the keyword WITH and determine whether to use pre-trained model by USING. The training and predicting syntax looks like:

TO TRAIN SQL:

  1. SELECT * FROM input_table
  2. TO TRAIN clusterModel
  3. WITH
  4. model.encode_units = [100, 7]
  5. model.n_clusters = 5
  6. model.run_pretrain = false
  7. COLUMN m1, m2, m3, m4, m5, m6, m7, m8, m9, m10
  8. USING existed_pretrain_model
  9. INTO my_cluster_model;

TO PREDICT SQL:

  1. SELECT *
  2. FROM input_table
  3. TO PREDICT output_table.group_id
  4. USING my_cluster_model;

where:

  • input_table is the high-dimensional table to be clustered.
  • model.encode_units is the autoencoder model layer’s encoder units, the decode_units can reverse encode_units directly.
  • model.n_clusters is the number of patterns after clustering.
  • my_cluster_model is the trained cluster model.
  • run_pretrain is used to determine if autoencoder pre_train needs to be run, default true.
  • existed_pretrain_model is used to specify an existing pretrain_model
  • output_table is the clustering result for input_table, which is adding the group_id column predicted by the cluster model to the input_table. The group_id is the category label predicted by the cluster model.

Code Details

  • sqlflow_models/clusterModel.py
  1. class clusterModel(tf.keras.Model):
  2. def pre_train(dataset):
  3. ...
  4. self.autoencoder.fit(dataset)
  5. pretrainmodel.save("/tmp/ae_pretrain.h5"
  6. def target_distribution():
  7. ...
  8. def sqlflow_train_loop():
  9. for ite in range(int(maxiter)):
  10. if ite % update_interval == 0:
  11. q = model.predict(x, verbose=0)
  12. p = target_distribution(q) # update the auxiliary target distribution p
  13. y_pred = q.argmax(1)
  14. idx = index_array[index * batch_size: min((index+1) * batch_size, x.shape[0])]
  15. loss = model.train_on_batch(x=x[idx], y=p[idx])
  16. index = index + 1 if (index + 1) * batch_size <= x.shape[0] else 0
  • template_tf.go

    1. if hasattr(classifier, 'pre_train'):
    2. classifier.pre_train(...)
    3. if hasattr(classifier, 'sqlflow_train_loop'):
    4. classifier.sqlflow_train_loop(...)

Note

  • The user can choose whether to run pre_train before the cluster model, ie run_pretrain=true. And the user can also choose to load the already trained model by loading the existed_pretrain_model.

Therefore, there are four cases in total:

  1. model.run_pretrain = true & User do not use USING keyword in this situation.

    Autoencoder Pre_train + Random initialization weights for cluster. (Note that model.encode_units “does work” at this time.)

  2. model.run_pretrain = true & Using existed_pretrain_model. existed_pretrain_model Pre_train + Random initialization weights for cluster. (Note that model.encode_units “does not work” at this time.)

  3. model.run_pretrain = false & User do not use USING keyword in this situation. Random initialization weights for cluster. (Note that model.encode_units “does not work” at this time.)

  4. model.run_pretrain = false & Using existed_pretrain_model. existed_pretrain_model Pre_train + Random initialization weights for cluster. (Note that model.encode_units “does not work” at this time.)

  • In the first stage of the clustering model on SQLFlow, we plan to achieve the first case. We will achieve the other cases in the later.

  • Users can use the trained cluster model in ` TO PREDICT SQL` to predict the group of input_table to get output_table.

  • Finally, the user can perform a combined aggregation operation on the output_table based on the SQL statement to obtain a result_table, which can be saved to the local dataframe and then analyzed according to his own needs.

Sometimes, analysts will compare the mean of each feature in each group of users, this helps them to understand the difference of behavioral characteristics in each group.

  1. %%sqlflow
  2. select
  3. group_id
  4. , avg(m1) as avgm1
  5. , avg(m2) as avgm2
  6. , avg(m3) as avgm3
  7. , avg(m4) as avgm4
  8. , avg(m5) as avgm5
  9. , avg(m6) as avgm6
  10. , avg(m7) as avgm7
  11. , avg(m8) as avgm8
  12. , avg(m9) as avgm9
  13. , avg(m10) as avgm10
  14. from output_table
  15. group by group_id
  1. _.to_dataframes(result_table)
  • The example of result_table:
group_idm1m2m3m4m5m6m7m8m9m10
00.0170.0150.0130.0120.010.010.0090.0080.0080.008
10.1950.1730.1540.1380.1240.1110.10.0910.0830.076
20.0140.0120.0110.010.0090.0080.0070.0050.0050.004
30.0050.0030.0030.0020.0010.0010.0010.00.00.0
40.3110.2910.2740.2570.240.2240.2090.1960.1850.175