CLARANS (**C**lustering **L**arge **A**pplications based on **RAN**domized **S**earch) is a Data Mining algorithm designed to cluster spatial data. We have already covered K-Means and K-Medoids clustering algorithms in our previous articles. This article talks about another clustering technique called CLARANS along with its Pythonic demo code. CLARANS was introduced by Raymond T. Ng and Jiawei Han of IEEE Computer Society (research paper).

CLARANS is a partitioning method of clustering particularly useful in spatial data mining. We mean recognizing patterns and relationships existing in spatial data (such as distance-related, direction-relation or topological data, e.g. data plotted on a road map) by spatial data mining.

How To Start Your Career In Data Science?

**Why CLARANS algorithm?**

As mentioned in our K-Medoids algorithm’s article, the K-Medoids clustering technique can resolve the limitation of the K-Means algorithm of being adversely affected by noise/outliers in the input data. But K-Medoids proves to be a computationally costly method for considerably large values of ‘k’ (number of clusters) and large datasets.

The CLARA algorithm was introduced as an extension of K-Medoids. It uses only random samples of the input data (instead of the entire dataset) and computes the best medoids in those samples. It thus works better than K-Medoids for crowded datasets. However, the algorithm may give wrong clustering results if one or more sampled medoids are away from the actual best medoids.

CLARANS algorithm takes care of the cons of both K-Medoids and CLARA algorithms besides dealing with difficult-to-handle data mining data, i.e. spatial data. It maintains a balance between the computational cost and the influence of data sampling on clusters’ formation.

**Steps of CLARANS algorithm**

- Select ‘k’ random data points and label them as medoids for the time being.
- Select a random point say ‘a’ from the points picked in step (1), and another point say ‘b’ which is not included in those points.
- We would already have the sum of distances of point ‘a’ from all other points since that computation is required for selecting the points in step (1). Perform similar computation for point ‘b’.
- If the sum of distances from all other points for point ‘b’ turns out to be less than that for point ‘a’, replace ‘a’ by ‘b’.
- The algorithm performs such a randomized search of medoids ‘x’ times where ‘x’ denotes the number of local minima computed, i.e. number of iterations to be performed, which we specify as a parameter. The set of medoids obtained after such ‘x’ number of steps is termed as ‘local optimum’.
- A counter is incremented every time a replacement of points is made. The process of examining the points for possible replacement is repeated till the counter does not exceed the maximum number of neighbors to be examined (specified as a parameter).
- The set of medoids obtained when the algorithm stops is the best local optimum choice of medoids.

*Image source*: Research paper

**Practical Implementation**

Here’s a demonstration of using CLARANS algorithm on the sklearn library’s Breast Cancer Wisconsin dataset. Though the dataset is primarily used for binary classification tasks, we use it to show how CLARANS algorithm can form separate clusters of the constituent data points falling under one of the two target categories (‘malignant’ or ‘benign’). The pyclustering data mining library has been used here for Pythonic implementation of CLARANS. The code has been implemented using Google colab with **Python 3.7.10** and **pyclustering 0.10.1.2** versions. Step-wise explanation of the code is as follows:

- Install pyclustering library.

`!pip install pyclustering`

- Import required libraries and modules

#Class for implementing CLARANS algorithm from pyclustering.cluster.clarans import clarans #To execute a function with execution time recorded from pyclustering.utils import timedcall #sklearn package for using a toy dataset from sklearn import datasets #Class for plotting multi-dimensional data from pyclustering.cluster import cluster_visualizer_multidim

- Import the Breast Cancer dataset

bc_dataset = datasets.load_breast_cancer() #Display the dataset bc_dataset

**Sample condensed output:**

- Extract the data points from the loaded dataset.

`bc_data = bc_dataset.data`

Convert the dataset from a numpy array to a list because a list of lists is fed as an input to the CLARANS’ implementation of the pyclustering library.

`bc_data = bc_data.tolist()`

Display the data in the form of a list

`print(bc_data[:5])`

**Sample condensed output:**

- Instantiate the clarans class.

clarans_obj = clarans(bc_data, 2, 3, 5) “”” where, ‘bc_data’ is the input data fed as a list of list of objects to be clustered ‘2’ is number of clusters to be formed (since there are 2 target categories); ‘3’ is the number of obtained local minima and ‘5’ is the maximum number of neighboring data points examined “””

- process() method analyzes the clusters as per the CLARANS algorithm. We call the process() method and encapsulate it in the call to timedcall() function so that the time taken for executing process() method also gets recorded.

#timedcall() returns a tuple containing the execution time and result of executing the function (tks, res) = timedcall(clarans_obj.process); #Print the execution time print("Execution time : ", tks, "\n");

**Output:**

`Execution time : 80.50073736700006 `

- Get the clusters allocated by the algorithm

`clst = clarans_obj.get_clusters()`

- Get the list of medoids of the clusters allocated by the algorithm.

`med = clarans_obj.get_medoids()`

- Print the results

print("Index of clusters' points :\n",clst) print("\nLabel class of each point :\n ",bc_dataset.target) print("\nIndex of the best medoids : ",med)

**Sample condensed output:**

- Here, the input data has 30 features. Cluster_visualizer class of the pyclustering library can be used to visualize the 1D, 2D or 3D data. While for more than three-dimensional data, cluster_visualizer_multidim class can be used as follows:

vis = cluster_visualizer_multidim() “”” Append the list of clusters; specify the clusters list, list of data points, marker sign and marker size to be used for visualization “”” vis.append_clusters(clst,bc_data,marker="*",markersize=5) #Display the clusters formed in multiple dimensions vis.show(pair_filter=[[1,2],[1,3],[27,28],[27,29]],max_row_size=2) “”” pair_filter parameter specifies the list of feature pairs for which the clusters will be visualized. max_row_size parameter specifies the maximum number of rows across which the plot will be spread. “””

**Output:**

**Google colab notebook**of the above implementation is available here.

**References**

.

## Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.Join our Telegram Group. Be part of an engaging community

A zealous learner aspiring to advance in the domain of AI/ML. Eager to grasp emerging techniques to get insights from data and hence explore realistic Data Science applications as well.