Challenge

Unsupervised Learning - Kmeans Week 11 Challenge

1. How does similarity is calculated if data is categorical in nature

 

To measure similarity between categorical data in K-means, we can use a distance metric such as Jaccard distance or Hamming distance. Jaccard distance measures the dissimilarity between two binary vectors, where each element in the vector represents the presence or absence of a particular categorical value. Hamming distance, on the other hand, measures the difference between two categorical data points by counting the number of elements in the two data points that are different.

Before using these distance metrics, categorical data needs to be transformed into numerical values. One common way of doing this is by creating dummy variables, also known as one-hot encoding, where each unique value in a categorical variable is converted into a binary variable that indicates whether that value is present or not.

When working with categorical data, similarity can be calculated using different methods depending on the nature of the data and the problem at hand. Here are some common approaches:

  1. Jaccard similarity coefficient: This is a measure of the similarity between two sets of categorical data. It is calculated as the size of the intersection of the sets divided by the size of the union of the sets. For example, if we have two sets of colors, {red, blue, green} and {red, blue, yellow}, the Jaccard similarity coefficient would be 2/4 or 0.5.

  2. Cosine similarity: This is a measure of the similarity between two vectors of categorical data. Each element in the vector represents a category, and its value is either 0 or 1 depending on whether that category is present or not. The cosine similarity is calculated as the dot product of the two vectors divided by the product of their magnitudes. For example, if we have two vectors representing the presence of colors in two sets, [1, 1, 1, 0, 0, 0] and [1, 1, 0, 0, 0, 1], the cosine similarity would be (11 + 11 + 00 + 00 + 00 + 01) / sqrt(33) * sqrt(22) = 0.67.

  3. Hamming distance: This is a measure of the difference between two categorical data points. It is calculated as the number of elements in the two data points that are different. For example, if we have two sets of colors, {red, blue, green} and {red, blue, yellow}, the Hamming distance would be 1 (since only green and yellow are different).

  4. Kappa statistic: This is a measure of agreement between two categorical data points. It is often used in cases where multiple raters or judges have assigned categorical labels to the same set of items, and we want to measure how consistent their judgments are. The Kappa statistic takes into account the possibility of agreement occurring by chance, and is therefore a more robust measure of inter-rater agreement than simply calculating the percentage of agreement.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.