Understanding Similarity Measures / Metrics in Data Science
From the publisher’s feed, collected in the ZeroLabs newsroom.
I’m writing this blog to give you a quick glimpse of the Similarity Metrics that we use in industry covering about why we need them, when do we use, what’s the intuition behind each metric and ending with some bonus tip to easily remember them.To Start with, have you ever wondered how platforms like Netflix know exactly what to recommend next or how online stores suggest products that fit your taste? How does the anomalies are detected in manufacturing Industries? The secret lies in understanding the similarity between data points.Why we need to use similarity Metrics?Let’s start with the Netflix example itself, suppose you have watched a movie and you have liked it and you have rated it highly, based on the rating the Netflix measures how likely you may like other movies in the same genre, and it calculates the similarity between the movie you liked to the other movies and bring the closest one’s popping up to you which you may likely to enjoy.Similarly, in the financial domain, number of fraudulent transactions are prevalent these days. Companies like Goldman Sachs uses similarity metrics along with other ML, DL techniques to detect the fraudulent transactions, by comparing new transactions against the historical transaction data with the strategy of identifying anomalies or close patterns that resemble known fraud cases.The other use case would be, assume you are a owner of a big retail store and you want to identify what all different types of customers you have. So, based on the transactional history and financial behaviour of them you would like to categorize or segment the customers. In this case, you would really make use of the similarity metrics between the transactional vectors of your clients which will basically help you to identify the customers needs and plan accordingly. This could be used in your personal project as well where you think you can segment some information which could drive some insights which basically are similar.At times when I search something in google, I have observed that most relevant information is presented first. These search engines also make use of similarity metrics to rank documents by relevance, ensuring that the most relevant information is presented first. This can be applied in your personal projects as well to rank your documents based on similarity.One of my side projects involves trying to figure out how the Deep Learning model behaves when the instances it has are very identical to one another — that is, can it still generate something new? And now it is up to you how you can broaden the frontiers and make it really useful in your projects or studies.What kind of distance metrics are available?Euclidean distance, Manhattan distance, Cosine Similarity, Minkowski Distance, Jaccard Distance, Mahalanobis, Chebyshevs, Jenson Shannon, Hamming, Spearman, Chisquare.How to use and when to use?Let’s dive a little deeper into how and when to use these than the ones described above. I’m not at all focusing on formulae since you can be able to Google them as well.Euclidean Distance: When the data is continuous numeric and is low dimensional and you just care how far is one point from the other, its better to prefer Euclidean distance. Intuition: Think of this metric as just the straight line distance between two points that you would measure with a ruler when the points have some reference(when they lie in a plane).Formula for Euclidean DistanceInuitive Image of Euclidean Formula2. Manhattan Distance: useful for grid-like structure and is high-dimensional and you care how distant it is from this point. Intuition: Think of this metric as grid-like or a Taxicab distance, representing the total length of the path a taxicab would take on a city grid or the total distance you would walk if you could only move along the grid lines of the city of Manhattan or some blaw blaw.Formula of Manhattan DistanceIntuitive Image of Manhattan Formula3. Cosine Similarity: This is the most famous metric after Euclidean which is widely used in high-dimensional data, may be you can say text or image or vectors with many dimensions and you really care about the angle between both the vectors; that is, the direction of the vectors is just as important as their magnitude(size). Intuition: You can think of it like how close the mindsets of the couple were aligned rather than how much they achieve in their lives. It measures the angle between two vectors, telling us how similar their directions are, irrespective of their magnitude.Formula of Cosine SimilarityMathematical Intuitive Image of Cosine Similarity4. Minkowski Distance: Its the generalization of both Euclidean and Manhattan Distances. It has some parameter p based on which the metric changes to Euclidean or Manhattan.Formula of Minkowski DistanceMathematical Intuitive understanding image of Minkowski Distance5. Jaccard Distance: This could ideally be used for binary or categorical data. Intuition: Think of this metric as a measure of how different two sets are. This measure clearly calculates the proportion of non-overlapping elements relative to the total number of elements in both sets.Formula of Jaccard SimilaritySo, Jaccard distance is 1- Jaccard Simiality.Intuitive Image of Jaccard Distance6. Mahalanobis distance: This measure is suitable for data with correlation between variables. Measuring the distance between a point and a distribution, rather than between two points. It’s like how unusual a data point is within a set of correlated variables. The best thing about this metric considers the data spread and correlations to clearly understand how many standard deviations a point is from the distribution’s mean. Intuition: Think of assessing how far a student’s test score is from the average class score or class test distribution considering all the variations in the scores and the associations between different sections of the test.Formula of Mahalanobis DistanceIf we calculate the Mahalanobis distances for each of these points and shade them according to their distance values then,Intuitive Image of Mahalanobis DistanceWe see the shaded points turn to clear elliptical patterns as shown in the image above.7. Chebyshev’s Distance: We use this when the data is single dimensional and you want to observe the maximum coordinate distance. It is the maximum single-step difference that you would like to see from one point to other along any single dimension. Intuition: Think of a situation where you want to understand the minimal number of moves to be taken for a king to travel from one square to another in chess, assuming he can move horizontally, vertically, or diagonally. This is kind of measuring the greatest change or the maximum change in any one dimension, giving a clear image of how far you’d have to travel in any direction.Formula of Chebyshev’s DistanceMathematical Intuitive Image of Chebyshevs Distance8. Kullback-Leibler(KL) divergence: This is used when we want to compare the similarity between two probability distributions. We can also understand that how much information is lost when we use one distribution to approximate another. Intuition: Think of it to be a measure of how much surprised a person is. Imagine two people communicating and one person encodes information assuming the distribution QQQ, but the actual distribution is PPP. This metric measures how shocked you would be when you expected the distribution to be Q when the actual distribution is P, and it can measure the inefficiency or extra cost incurred in this communication due to the wrong assumption about the distribution. The direction of divergence is also important, this is important when we wanted to compare the predicted distribution of a model against the actual distribution of data like which is usually less relevant because we generally want to understand how well our model (the approximation) represents the true data distribution, not the other way around.Formula of KL divergenceMathematical Intuitive Image9. Jenson-Shannon divergence: similar to KL divergence, it is suitable when you want to compare two probability distributions but when you need a symmetric measure of similarity, that is, a bounded and normalized measure (between 0 and 1) is required.Formula of Jenson ShannonIntuitive Image of Jenson Shannon10. Hamming Distance: This is purely used for Binary or Categorical Data. Intuition: Think of it as a measure, suppose you have two strings and you want to measure how many single-character changes are needed to turn one string to other. We cannot purely say like Anomaly detection but we can say it is useful in error detection and correction. example, comparing the words “kitten” and “sitting,” the Hamming distance is 3, because the first, fourth, and sixth letters are different, so, 3 changes are needed to turn one string into another.Formula of hamming DistanceMathematical Intuitive Image of Hamming Distance11. Spearman Correlation: when the data is ordered and you want to identify the relationship between those two ordered data you can use this. Intuition: measure of how well the relationship between two variables can be described, whether an increase in one variable tends to be associated with an increase (or decrease) in another when the variables are ranked.Spearmen Ranks CorrelationIntuitive Image of Spearmen Rank Correlation12. Chi-Square Distance: It is used when you want to drive some insights from a Categorical data. It is specifically designed for categorical data as measure of the divergence between observed and expected frequencies of different categories. Intuition: Imagine you have a survey where people can choose their favorite fruit, think of this measure where you want to see if the distribution of choices differs from what you would expect based on previous surveys. It’s commonly used in hypothesis testing to determine or statistically prove if there is a significant difference between the expected and observed data.Formula of Chi-SquareIntuitive Image of Chi-SquaredBonus:If you could have read this blog properly then you would understand which is the similarity measure and non similarity measure. And not only these you can create your own similarity metrics or a true distance metric, but you can say it to be a true distant metric when it follows the four qualities:If the value of that metric (eg. distance) is non negative.The metric should be Symmetric in nature.The direct distance from ppp to qqq should be less than or equal to the distance from ppp to rrr plus the distance from rrr to qqq, which is the Triangle Inequality.The distance between ppp and qqq is zero if and only if ppp and qqq are the same point, which is called as Identity of Indiscernibles.In the next blog, I will focus onto real-time industry examples of how these metrics are applied.Understanding Similarity Measures / Metrics in Data Science was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.