# Clustering Methods and Comparison | Sklearn Tutorial

Scikit-learn is a Python-based machine learning library that uses SciPy and is licensed under 3-Clause BSD. It was initiated by David Cournapeau as a Google Summer of Code project in 2007 and has since been developed by a group of contributors.

The core contributors can be viewed on the About Us page. Scikit-learn is mainly built in Python and relies heavily on NumPy for fast array operations and linear algebra.

To enhance performance, certain algorithms are written in Python, such as the Python wrapper for LIBSVM that implements support vector machines and the LIBLINEAR wrapper for linear support vector machines and logistic regression.

The use of Python makes it possible to implement these methods beyond what is achievable with just Python.

Scikit-learn works well with other Python libraries such as SciPy, Matplotlib, plotly, Pandas data frames, NumPy, etc. In this article, we’ll cover SkLearn Clustering.

## What is Clustering?

Clustering is a method of grouping data. More specifically, clustering is a data grouping method used to identify clusters resulting from grouping smaller elements based on similarities.

The similarity that forms the basis for grouping is not universal, so the researchers or analyzers must first define the similarities.

Clustering is a method of grouping data that is often used as one of the data mining methods or data mining.

Clustering is partitioning a set of data objects into subsets called clusters. Therefore, this clustering method is useful for finding unknown groups in the data.

In business intelligence, clustering can group many customers into several groups. For example, grouping customers into several clusters with strong characteristics in common.

Clustering is also known as data segmentation because it divides many data sets into groups based on similarity.

## Clustering Methods

Some of the clustering methods that are a part of Sci-kit learn are as follows:

### 1. Mean Shift Clustering

Mean Shift is a machine learning algorithm that is used to find blobs in smooth sample density data. It assigns data points to clusters by iteratively moving points to higher-density data points, without the need to specify the number of clusters beforehand.

The algorithm is implemented in the Scikit-learn library through the MeanShift module.

### 2. KMeans Clustering

In KMeans, centroids are computed and iterated until the best centroid is found. It requires specifying the number of clusters beforehand and its main goal is to cluster data by reducing the inertia criteria.

The algorithm is implemented in the Scikit-learn library through the KMeans module, which allows for adjusting the sample weight for individual samples.

### 3. Hierarchical Clustering

This algorithm creates nested clusters by successively merging or breaking clusters, represented as a tree or dendrogram. It can be divided into two categories: Agglomerative hierarchical algorithms and Divisive hierarchical algorithms.

The Agglomerative Clustering module in the Scikit-learn library’s sklearn.cluster package is used for performing Agglomerative Hierarchical Clustering.

### 4. BIRCH Clustering

BIRCH stands for Balanced Iterative Reducing and Clustering with Hierarchies, a tool for hierarchical clustering on large data sets.

It creates a tree called the Characteristics Feature Tree (CFT) which stores the required information for clustering, reducing the need to store the complete input data in memory.

The Birch module in the Scikit-learn library’s sklearn.cluster package is used for performing BIRCH clustering.

### 5. Spectral Clustering

Spectral Clustering is a machine learning algorithm that executes dimensionality reduction in a lesser number of dimensions by using the eigenvalues, or spectrum, of the data’s similarity matrix.

It is not recommended when there are a significant number of clusters. The algorithm is implemented in the Scikit-learn library through the Spectral Clustering module.

### 6. Affinity Propagation

This algorithm employs the concept of “message passing” between pairs of samples until convergence. It does not require the pre-specification of the number of clusters. Its main disadvantage is its temporal complexity, which is of the order of O(N2T).

In Scikit-learn, the Affinity Propagation algorithm is implemented in the sklearn.cluster package. To apply Affinity Propagation, use the AffinityPropagation module.

### 7. OPTICS

Short for Ordering Points To Identify the Clustering Structure, is a technique for finding density-based clusters in spatial data. It works similarly to the DBSCAN algorithm.

By arranging the database points such that the spatially closest points become neighbors in the ordering, OPTICS addresses the challenge of identifying meaningful clusters in data with changing density, a limitation of the DBSCAN algorithm.

OPTICS clustering is implemented in the sklearn.cluster package of Scikit-learn. To perform OPTICS clustering, use the OPTICS module.

### 8. DBSCAN

DBSCAN, or Density-Based Spatial Clustering of Applications with Noise, is based on the concepts of “clusters” and “noise”. It states that clusters are areas of higher density surrounded by regions of lower density data points.

DBSCAN clustering is implemented in the sklearn.cluster package of Scikit-learn. To perform DBSCAN clustering, use the DBSCAN module.

The algorithm requires two crucial parameters, min_samples and eps, to define density. The greater the value of min_samples or the lower the value of eps, the higher the density of data points required to form a cluster.

## Comparison of Clustering Methods Based on Parameters, Scalability, and Metric

Let us compare the Sklearn clustering methods to get a clearer understanding of each. The comparison has been summarized in the table below:

### Scalability

1. Mean-Shift Bandwidth Distance between points Not scalable and has n samples
2. Hierarchical Clustering Cluster numbers or Distance threshold Distance between points Large n samples and large n clusters
3. BIRCH Branching factor and Threshold Euclidean distance between points Large n samples and large n clusters
4. Spectral Clustering Cluster numbers Graph Distance A small level of scalability with n clusters and a medium level of scalability with n samples
5. Affinity Propagation Damping Graph Distance It is not scalable and has n samples.
6. K-Means Cluster numbers Distance between points Very large n samples
7. OPTICS Minimum cluster membership Distance between points Large n clusters and very large n samples
8. DBSCAN Neighborhood size. Medium n clusters and very large n samples Nearest point distance

Implementation of Algorithms

• LNKnet Software: A public domain software offered by MIT Lincoln Laboratory.
• Cluster and Tree View Software: Programs that offer a graphical and computational environment to analyze data from various datasets.
• These software options are popular for implementing multiple data clustering algorithms.