# Clustering

## Train with customised number of clusters

This section is for training models where the number of clusters is already defined manually, or automatically when using the following algorithms:

* Mean Shift Clustering
* Density Based Spatial Clustering
* OPTICS Clustering
* Affinity propagation

Step 1 : click **`Train and Evaluate`** button , you get the following output:

![Clustering model training with Kmeans](https://1577378233-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MT0DqusrMJSGtXvrnXz%2F-MVaneIdQO3Trs-onoQ9%2F-MVaqXl9BdfIR92N-bur%2Fimage.png?alt=media\&token=3905edcf-e076-45ca-be3f-56f4d33fdf0f)

Step 2 : assign different clusters to your data, click **`Assign clusters`** you will get the following output:

![Assign clusters](https://1577378233-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MT0DqusrMJSGtXvrnXz%2F-MVaneIdQO3Trs-onoQ9%2F-MVarVelci6Zs3EQCQ8_%2Fimage.png?alt=media\&token=b524b9c4-bd24-4c98-af1a-e750c98fa41d)

{% hint style="info" %}

* the model will be save as Clustering\_Model\_2.pkl
* Assigned data will be save as Predicted\_data\_Assigned\_2.csv

in this case 2 is the session ID number
{% endhint %}

## Retrain Using elbow method to determine an optimal number of clusters

This section is for getting an optimal number of clusters using the elbow method, for that you click&#x20;

**`Get an optimal number of clusters`** button, and you will get this graph.

![Getting the number of clusters using elbow method](https://1577378233-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MT0DqusrMJSGtXvrnXz%2F-MVaneIdQO3Trs-onoQ9%2F-MVava3Il0qs2Sg4nQTj%2Fimage.png?alt=media\&token=f108c308-8079-421a-925f-e7070d3eff32)

From the image above , we notice that k = 4 is the optimized number of clusters.

**`Enter the elbowatk number shown in the image above`** which 4 in our case and click **`Step 1 : Retrain and Evaluate`** button.

![Clustering using optimized number of clusters](https://1577378233-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MT0DqusrMJSGtXvrnXz%2F-MVaneIdQO3Trs-onoQ9%2F-MVaxrWsUyUL4LLeUz90%2Fimage.png?alt=media\&token=66c70e65-a19f-4557-8890-be6c7efe4972)

{% hint style="info" %}

* The model will be saved as Clustering\_Model\_2\_elbow\.pkl
* The assigned data will be saved as Predicted\_data\_2\_elbow\.csv

in this case 2 is the session ID number
{% endhint %}

## Train and tune the number of clusters with data containing labled target column

You can use this section if you do have already labled data, and you want to tune the number of clusters,&#x20;

{% hint style="warning" %}
Only the following models could be tuned:

* K-Means Clustering
* Spectral Clustering
* Agglomerative Clustering
* Birch Clustering
* K-Modes Clustering
  {% endhint %}

if you have not selected one of the above models, then you have to start the experiment from the begining, which means choose the right model, process data then come back to training section. if you did choose one of them, then go ahead and fill the following multiselect boxes.

**`Select the target column containing labels`** : Name of the target column containing labels.

**`Select type of task (Automatically inferred when None)`**: Choose from the list

if **Classification**:

* ‘ Logistic Regression (Default)
* K Nearest Neighbour
* &#x20;Naive Bayes
* Decision Tree Classifier
* SVM - Linear Kernel
* SVM - Radial Kernel
* Gaussian Process Classifier
* &#x20;Multi Level Perceptron
* &#x20;Ridge Classifier
* &#x20;Random Forest Classifier
* &#x20;Quadratic Discriminant Analysis
* &#x20;Ada Boost Classifier
* &#x20;Gradient Boosting Classifier
* &#x20;Linear Discriminant Analysis
* &#x20;Extra Trees Classifier
* &#x20;Extreme Gradient Boosting
* &#x20;Light Gradient Boosting
* &#x20;CatBoost Classifier

if **Regression**:

* Linear Regression (Default)
* Lasso Regression
* Ridge Regression
* Elastic Net
* Least Angle Regression
* Lasso Least Angle Regression
* Orthogonal Matching Pursuit
* Bayesian Ridge
* Automatic Relevance Determ.
* Passive Aggressive Regressor
* Random Sample Consensus
* TheilSen Regressor
* Huber Regressor
* Kernel Ridge
* Support Vector Machine
* K Neighbors Regressor
* Decision Tree
* Random Forest
* Extra Trees Regressor
* AdaBoost Regressor
* Gradient Boosting
* Multi Level Perceptron
* Extreme Gradient Boosting
* Light Gradient Boosting
* CatBoost Regressor

**`Select the evaluation metric`**: For Classification tasks: Accuracy, AUC, Recall, Precision, F1, Kappa (default = ‘Accuracy’), For Regression tasks: MAE, MSE, RMSE, R2, RMSLE, MAPE (default = ‘R2’).

**`Select the type of your custom grid`** : By default, a pre-defined number of clusters is iterated over to optimize the supervised objective. To overwrite default iteration, pass a list of number of clusters to iterate over in **`Select a list of number of clusters to iterate over`** multiselect box.

**`Select the number of folds to be used in cross validation`** : Number of folds to be used in Kfold CV. Must be at least 2.

By clicking **`Step 1 : Tune num_clusters and Evaluate`** button, you will get the following output:

![Tuning number of clusters using supervised learning ](https://1577378233-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MT0DqusrMJSGtXvrnXz%2F-MVaneIdQO3Trs-onoQ9%2F-MVbACSyoThAjQYroHkX%2Fimage.png?alt=media\&token=e51ce4f9-9d21-4bfc-a924-304e5167c6cc)

As you see below where indexes is the number of clusters, that 4 clusters is the best number of clusters for this sepecific data.

if you click step 2 : to assign clusters, you will get the following output.

![Assingning clusters to data.](https://1577378233-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MT0DqusrMJSGtXvrnXz%2F-MVaneIdQO3Trs-onoQ9%2F-MVbBVLpqxMYQo29E4qb%2Fimage.png?alt=media\&token=c226a06d-b4fe-4486-acc4-687498b2e338)

{% hint style="info" %}

* The model will be saved as Clustering\_Model\_2\_tuned.pkl
* All your Assigned data will be save as Predicted\_data\_2\_tuned.pkl&#x20;

in this case 2 is the session ID number
{% endhint %}
