# K-Prototypes Clustering With Numerical Example

Clustering algorithms are unsupervised machine learning algorithms used to segment data into various clusters. In this article, we will discuss the K-Prototypes clustering algorithm with a numerical example. We will also discuss the advantages and disadvantages of the k-prototypes clustering algorithm.

## What is K-Prototypes clustering?

K-Prototypes clustering is a partitioning clustering algorithm. We use k-prototypes clustering to cluster datasets that have categorical as well as numerical attributes. The K-Prototypes clustering algorithm is an ensemble of k-means clustering and k-modes clustering algorithm. Hence, it can handle both numerical and categorical data.

To understand the k-prototypes clustering in a better way, I would first suggest you read k-means clustering with a numerical example and k-modes clustering with a numerical example.

In k-prototypes clustering, we select k-prototypes randomly at the start. After that, we calculate the distance between each data point and the prototypes. Accordingly, all the data points are assigned to clustering associated with different prototypes.

After assigning data points to the clusters, we calculate the new prototype for the current cluster using the method discussed in the next sections. After that, we recalculate the distance of prototypes from the data points and reassign the clusters. This process is continued until the clusters converge.

Before getting into the numerical example for k-prototypes clustering, Let us first discuss the distance measures and calculation of prototypes in the k-prototypes clustering algorithm.

## Distance Measures in K-Prototypes Clustering

As the k-prototypes clustering algorithm deals with data having numerical as well as categorical attributes, it uses different measures for both data types.

For numerical data, the k-prototypes clustering uses squared euclidean distance as the distance measure. For instance, if we are given two data points (1,2,3) and (4, 3, 3), the distance between these two data points will be calculated as (1-4)^2+(2-3)^2+(3-3)^2 which is equal to 13.

For categorical attributes, the k-prototypes clustering algorithm follows matching dissimilarity. If you have two records (A, B, C, D) and (A, D, C, C) with categorical attributes, the matching dissimilarity is the number of different values at each position in the records. In the given records, values are different at the two positions only. Hence, the matching dissimilarity between the records will be 2.

For records having mixed attributes, we calculate the distance between categorical and numerical attributes separately. After that, we use the sum of the dissimilarity scores as the distance between two records. For instance, consider that we have two records [‘A’, ‘B’, ‘F’, 155, 53] and [‘A’, ‘A’, ‘M’, 174, 70].

To find the distance between these two records, we will first find the dissimilarity score between  [‘A’, ‘B’, ‘F’] and  [‘A’, ‘A’, ‘M’]. The score is 2 as two attributes out of three have different values.

Next, we will calculate the square euclidean distance between [155, 53] and [174, 70]. Here, (155-174)^2 + (53-70)^2 which is equal to 650.

Now, we can directly calculate the total dissimilarity score as the sum of the dissimilarity score between categorical attributes and the square euclidean distance of numerical attributes. Here, the sum will be equal to 650+2=652.

Observe that the matching dissimilarity score of categorical attributes is almost negligible compared to the square euclidean distance between numerical attributes. Hence, the categorical attributes will have little or no effect on clustering.

To solve this problem, we can scale the values in numeric attributes within a range of say 0 to 5. Alternatively, we can take a weighted sum of the matching dissimilarity scores and the square euclidean distance. In this article, we will discuss a numerical example of the k-prototypes clustering algorithm by scaling the values in numeric attributes within a range of 0 to 5.

## Choice of New Prototypes in K-Prototypes Clustering

Once a cluster is formed, we need to calculate a new prototype for the cluster using the data points in the current cluster.

To calculate the new prototype for any given cluster, we will take mode of categorical attributes of the data points in the cluster. For numerical attributes, we will use the mean of the values to calculate new prototype for the cluster. For example, suppose that we have the following data points in a cluster.

In the above cluster, we will take the mode of values in the EQ Rating, IQ Rating, and Gender attributes. For the attributes Height and Weight, we will take the mean of the values to calculate the new prototype. Hence, the prototype for the above cluster will be [B, A , F, 4.71978,3.999999 ].

Here, B is the mode of values in the EQ Rating column. A is the mode of the values in the IQ Rating column, and F is the mode of the values in the Gender column. Similarly, 4.71978 and 3.999999 are mean of the Height and Weight attributes respectively.

## The K-Prototypes Clustering Algorithm

The K-prototypes clustering algorithm is similar to the k-means clustering algorithm. The difference lies in how we calculate the distance between two data points and how we select new prototypes for clusters. The steps in the k-prototypes clustering algorithm are discussed below.

1. First, we select K data points from the input dataset as initial prototypes.
2. We then find the distance of each data point from the current prototypes. The distances are calculated as discussed in the previous sections.
3. After finding the distance of each data point from the prototypes, we assign data points to clusters. Here, each data point is assigned to the cluster with the prototype nearest to the data point.
4. After assigning data points to the clusters, we calculate new prototypes for each cluster. To calculate the prototypes, we take the mean of numeric attributes and the mode of categorical attributes as discussed previously.
5. If the new prototypes are the same as the previous prototypes, we say that the algorithm has converged. Hence, the current clusters are finalized. Otherwise, we go to 2.

Now that we have introduced you to the k-prototypes clustering algorithm, let us discuss a numerical example of k-prototype clustering with a small sample dataset.

## K-Prototypes Clustering Numerical Example

Following is the dataset we will use to understand the k-prototypes clustering algorithm using the numerical example. It contains EQ Rating, IQ rating, Gender, Height and Weight of students in a given class.

The above table isn’t normalized. Due to this, the dissimilarity score of categorical attributes is negligible compared to the difference between numerical attributes. Due to this, the clustering will be biased on the basis of height and weight. To avoid this bias, we will normalize the dataset as shown in the following table.

In the above table, we have normalized the Height and Weight attributes in the range 0 to 5. Now, the distance between the numeric attributes will not be very large compared to the dissimilarity between the categorical attributes.

Let us now discuss the numerical for k-prototypes clustering using the normalized dataset having mixed data types.

## K-Prototypes Clustering With Numerical Example Iteration 1

Suppose that we want to classify the input dataset into 3 clusters. Hence, we will select three data points as initial prototypes. Here, we will select Student 5, Student 9, and Student 13 as initial prototypes. Hence,

• prototype1= [‘B’, ‘A’, ‘M’, 4.175824, 4.470588]
• prototype2= [‘A’, ‘C’, ‘M’, 4.945055, 5.0]
• prototype3=[‘A’, ‘B’, ‘M’, 4.395604, 3.6470589]

Now, let us calculate the distance of each data point with the prototypes. The distance is calculated using the matching dissimilarity and squared distance measure discussed in the previous sections. The distance has been calculated and tabulated below.

In the above table, we have first calculated the distance of each data point from the initial prototypes. Then, we have assigned the data points to the nearest prototype.

After creating the clusters, we will calculate the new prototypes for each cluster using the mode of categorical attributes and mean of the numerical attributes.

In cluster 1, we have the following points.

The prototype for this cluster will be [B, A, F, 4.656593, 3.970588].

We have the following points in the second cluster.

Hence, the prototype for the second cluster will be [A, C, M, 4.615384, 4.411764].

Cluster 3 has the following points.

Hence, the prototype for cluster 3 is [A, B, F, 4.580062, 3.773109].

After iteration 1, we have the following prototypes.

• prototype1= [B, A, F, 4.656593, 3.970588]
• prototype2= [A, C, M, 4.615384, 4.411764]
• prototype3=[A, B, F, 4.580062, 3.773109]

You can observe that the current prototypes are not the same as the initial prototypes. Hence, we will calculate the distance of the data points in the dataset to these prototypes and reassign the points to the clusters.

## K-Prototypes Clustering With Numerical Example: Iteration 2

The distance of each point from the prototypes calculate in the first iteration are tabulated below.

In the above table, we have reassigned the data points to the clusters based on their distance from the prototypes that were calculated in iteration 1. Now, we will again calculate the prototype for each cluster.

In cluster 1, we have the following points.

The prototype for the above cluster is [B, A , F, 4.71978,3.999999].

In cluster 2, we have the following points.

The prototype for cluster 2 is [A, C, M, 4.648351, 4.3529412].

In cluster 3, we have the following data points.

The prototype for cluster 3 is [A, B, F, 4.461538, 3.635294].

After iteration 2, we have the following prototypes.

• prototype1= [B, A , F, 4.71978,3.999999].
• prototype2= [A, C, M, 4.648351, 4.3529412].
• prototype3=[A, B, F, 4.461538, 3.635294].

You can observe that the current prototypes are not the same as the prototypes calculated after iteration 1. Hence, we will calculate the distance of the data points in the dataset to these prototypes and reassign the points to the clusters.

## K-Prototypes Clustering With Numerical Example: Iteration 3

The following table contains the distance of each data point from the prototypes calculated in iteration 2.

In the above table, we have reassigned the points to different clusters. Now, let us recalculate the prototype of each cluster.

In cluster 1, we have the following data points.

The prototype for cluster 1 will be [B, A , F, 4.71978,3.999999].

In cluster 2, we have the following data points.

The prototype for cluster 2 will be [A, C, M, 4.606227, 4.235294].

In cluster 3, we have the following data points.

The prototype for the above cluster will be [A, B, F, 4.478022, 3.632353].

After iteration 2, we have the following prototypes.

• prototype1= [B, A , F, 4.71978,3.999999].
• prototype2= [A, C, M, 4.606227, 4.235294].
• prototype3=[A, B, F, 4.478022, 3.632353].

You can observe that the current prototypes are not the same as the prototypes calculated after iteration 2. Hence, we will calculate the distance of the data points in the dataset to these prototypes and reassign the points to the clusters.

## K-Prototypes Clustering With Numerical Example: Iteration 4

The following table contains the distance of points from the prototypes calculated in iteration 3 along with the reassigned clusters.

You can observe that the the points in the clusters haven’t changed. Hence, the prototype in each cluster will also not change. Thus, our algorithm has converged and the clusters calculated in the previous iteration are the final output clusters.

• The first advantage of k-prototypes clustering is that it can be used for datasets having mixed data types. As we are able to cluster datasets with numerical and categorical attributes, the usability of the k-prototypes clustering algorithm becomes high in real-life situations.
• K-Prototypes clustering algorithm is easy to implement. It uses simple mathematical constructs and is an iterable algorithm. Hence, it becomes easy to understand and implement this algorithm.
• K-prototypes clustering is a scalable clustering algorithm. You can use it for smaller as well as large datasets.
• The K-Prototypes clustering algorithm is guaranteed to converge. Hence, it is sure that we will get the output clusters if we execute the algorithm.
• The K-Prototypes clustering algorithm isn’t restricted to a particular domain. As long as we have structured datasets with numeric and categorical attributes, we can use the k-prototypes clustering algorithm in any domain.
• The number of iterations and the shape of clusters in K-Prototypes clustering are highly dependent on the initial prototypes. The algorithm allows us to warm-start the choice of clusters. Hence after data preprocessing and exploratory analysis, you can choose suitable prototypes initially so that the number of iterations can be minimized while executing the algorithm.

• The K-prototypes clustering algorithm, just like the k-means and k-modes clustering algorithm, faces the curse of dimensionality. With an increasing number of attributes in the input dataset, the dissimilarity between the data points starts to become almost the same. In such a case, we need to define a mechanism to specify which cluster a data point is assigned to if the data points have the same dissimilarity with two prototypes.
• In K-prototypes clustering, we don’t know the optimal number of clusters. Due to this, we need to try different numbers of clusters to find the optimal k for clustering data into k clusters.
• The number of iterations in the k-prototypes clustering for convergence depends on the choice of initial prototypes. Due to this, if the prototypes aren’t selected in an efficient way, the runtime for the algorithm will be longer.
• The shape of the clusters in k-prototypes clustering depends on the initial prototypes. Hence, the k-prototypes algorithm might give different results for each run.

## Conclusion

The k-prototypes cluster algorithm finds its applications in various real life situations due to its ability to handle mixed data types. You can use k-prototypes clustering in loan classification, customer segmentation, cyber profiling, and other situations where we need to group data into various clusters.

In this article, we have discussed the k-prototypes clustering algorithm with a numerical example. We have also discussed the advantages and disadvantages of the algorithm.