Clustering
Automatic classification, also known as segmentation or clustering, is the most widespread technique in unsupervised learning.
It is used when we have a large volume of data in which we seek to distinguish homogeneous subsets, susceptible to differentiated treatments and analyses.
Therefore, it is an unsupervised technique often used as a preliminary step ( pre-processing ) before applying a supervised technique to improve prediction performance.
As a reminder, there are two types of learning : supervised and unsupervised. Until now, we have dealt with supervised techniques, meaning techniques from which we make predictions ( classification or estimation ).
Although the term « classement » in French is translated as « classification » in English and refers to a supervised technique, the term « classification » in French is translated as « clustering » in English.
- Clustering (EN) − Classification (FR) : dividing the entire dataset into relatively homogeneous subgroups, maximizing the similarity of records within the group and minimizing the similarity outside the group.
- Classification (EN) – Classement (FR) : assigning each record to a class, among several predefined classes, based on the characteristics of the record described as explanatory variables ( predictors ). The result allows each individual to be assigned to the best class.
Since we are addressing unsupervised techniques, it is important to recall a fundamental difference from supervised learning : in unsupervised learning, there are only ; no , and all records are used - there is no partition for a training phase -. The technique is applied to the entire dataset, and we, as data scientists, must interpret the result.
However, there is a connection between these two families : most predictive algorithms struggle with too many variables due to correlations between them, which hinder their predictive power. Clustering allows the formation of homogeneous groups that can be described by a small number of variables specific to each group. Moreover, sometimes it is useful for a predictive algorithm to handle missing values without substituting them with a priori values like the average of the records. One solution is to apply a clustering technique beforehand to group individuals with missing values into clusters and then impute the average of those groups to replace the missing value.
Thus, we want to segment a heterogeneous population into homogeneous subgroups or clusters. The qualities of a cluster are :
- strong intra-class similarity
- low inter-class similarity

K-means
The K-means method is one of the main partitioning methods, aimed at partitioning individuals for whom we have measurements into different classes.
We seek to group individuals that are as similar as possible ( from the point of view of the measurements we have ) while separating the classes as much as possible from one another.

Steps
Step 1 : we choose individuals as initial class centers ( we define the number of classes and the individuals considered as the centers of these classes randomly ) - in reality, we will see later that the algorithm « tests » many configurations to identify the best possible distribution of the data -.
Example

Step 2 : we calculate the distances between each individual and each center from the previous step. We assign each individual to the closest center, which defines classes.

Step 3 : We replace the initial class centers with the barycenters of the classes. The new centroids are calculated by taking the average of the coordinates of all the points belonging to each cluster. ( These barycenters are not necessarily existing data points ).
For example, in cluster 2 identified in step , if we obtain the values and for this cluster, in step 3, the new center will be , which equals .

Step 4: we calculate the distances between each individual and each center, and we assign each individual to the closest center.
Steps 3 and 4 are repeated in iterations until the centers remain stable enough ( comparing their displacement to the distances between the initial centers ).

Distance Calculation
Distances can be defined in several ways, but generally, the following properties are required :
- a distance is non-negative :
- self-proximity principle : (the distance of a record from itself is 0)
- Symmetry principle :
- triangle inequality principle : ( the distance between any pair cannot exceed the sum of the distances between two other pairs )
As in the K-NN Nearest Neighbors technique, the concept of distance between two records ( proximity – similarity ) is calculated based on quantitative variables. Therefore, it is important in some cases to transform the data to be able to apply distance calculations. If the variables are ordinal qualitative, they must be numerized. If the variables are nominal qualitative, we must perform one-hot encoding.
The three most commonly used distances are Euclidean distance, Manhattan distance, and Minkowski distance.
Euclidean Distance
Euclidean distance is the most commonly used method for calculating the distance between two records because it is considered the least computationally intensive. It is simply the shortest distance between point A and point B.

The formula for Euclidean distance is :
or
For the following example :
| ID | ||
|---|---|---|
| 01 | 2.04 | 3.2 |
| 02 | 1.98 | 3.8 |
The distance is calculated as follows :
The distance is calculated for all records .
Euclidean distance is heavily influenced by the scale of each variable. Therefore, it is common practice to normalize the data to convert them to the same scale. Moreover, it does not account for possible relationships between variables ( correlation ) and is sensitive to outliers.
Manhattan Distance
If the data contain outliers and, for specific reasons, these outliers are not removed, we favor the Manhattan distance, which focuses on the absolute value of the differences rather than the square of the differences.

This measure is named after the city of Manhattan because, in two dimensions, it represents the distance a taxi would travel in a city where streets are either parallel or perpendicular to one another.
The formula for Manhattan distance is :
or
For the following example :
| ID | ||
|---|---|---|
| 01 | 2.04 | 3.2 |
| 02 | 1.98 | 3.8 |
The distance is calculated as follows :
The distance is calculated for all records .
Minkowski Distance
Minkowski distance generalizes Euclidean and Manhattan distances.

The formula for Minkowski distance is :
or