Understanding Cosine Similarity And Its Application | by Richmond Alake | Towards Data Science

Learn the basics of AI and Deep Learning with TensorFlow and Keras in this Live Training Session hosted by Me.
A specific requirement within a project I’m currently undertaking required me to explore methods of quantifying the similarity between two or more objects.
The objects in this project were two images that contained humans performing certain poses; the objective was to quantify how similar the poses in the images were.
Trying to solve this problem led me to a world of maths — I’m not a massive fan of maths.
I stumbled upon a similarity measurement called Cosine Similarity. I was intrigued by the simplicity of this implementation, and more importantly, it was straightforward to understand.
In this article, I will explain the basics of cosine similarity. I’ll also provide several applications and domains where cosine similarity is leveraged, and finally, there will be a code snippet of the algorithm in the Swift programming language.
Cosine Similarity is a measurement that quantifies the similarity between two or more vectors. The cosine similarity is the cosine of the angle between vectors. The vectors are typically non-zero and are within an inner product space.
The cosine similarity is described mathematically as the division between the dot product of vectors and the product of the euclidean norms or magnitude of each vector.
Cosine similarity is a commonly used similarity measurement technique that can be found in widely used libraries and tools such as Matlab, SciKit-Learn, TensorFlow etc.
Below is a quick implementation of the cosine similarity logic in Swift.
func cosineSimilarity(A: [CGFloat?], B: [CGFloat?]) -> CGFloat {    let dotProduct: CGFloat    let magnitudeOfA: Float    let magnitudeOfB: Float    let cosineSimilarity: Float    dotProduct = (A[0]! + B[0]!) + (A[1]! + B[1]!)    magnitudeOfA = sqrtf(powf(Float(A[0]!), 2.0) + powf(Float(A[1]!), 2.0))    magnitudeOfB = sqrtf(powf(Float(B[0]!), 2.0) + powf(Float(B[1]!), 2.0))    cosineSimilarity = Float(dotProduct) / (magnitudeOfA * magnitudeOfB)    return CGFloat(cosineSimilarity)}
Cosine Similarity is a value that is bound by a constrained range of 0 and 1.
The similarity measurement is a measure of the cosine of the angle between the two non-zero vectors A and B.
Suppose the angle between the two vectors was 90 degrees. In that case, the cosine similarity will have a value of 0; this means that the two vectors are orthogonal or perpendicular to each other.
As the cosine similarity measurement gets closer to 1, then the angle between the two vectors A and B is smaller. The images below depict this more clearly.
Cosine similarity has its place in several applications and algorithms.
From the world of computer vision to data mining, there is lots of usefulness to comparing a similarity measurement between two vectors represented in a higher-dimensional space.
Let’s go through a couple of scenarios and applications where the cosine similarity measure is leveraged.
A scenario that involves the requirement of identifying the similarity between pairs of a document is a good use case for the utilisation of cosine similarity as a quantification of the measurement of similarity between two objects.
Quantification of the similarity between two documents can be obtained by converting the words or phrases within the document or sentence into a vectorised form of representation.
The vector representations of the documents can then be used within the cosine similarity formula to obtain a quantification of similarity.
In the scenario described above, the cosine similarity of 1 implies that the two documents are exactly alike and a cosine similarity of 0 would point to the conclusion that there are no similarities between the two documents.
Document 1: Deep Learning can be hard
Document 2: Deep Learning can be simple
Step 1: First we obtain a vectorised representation of the texts
Document 1: [1, 1, 1, 1, 1, 0] let’s refer to this as A
Document 2: [1, 1, 1, 1, 0, 1] let’s refer to this as B
Above we have two vectors (A and B) that are in a 6 dimension vector space
Step 2: Find the cosine similarity
Calculate the dot product between A and B: 1.1 + 1.1 + 1.1 + 1.1 + 1.0 + 0.1 = 4
Calculate the magnitude of the vector A: √12 + 12 + 12 + 12 + 12 + 02 = 2.2360679775
Calculate the magnitude of the vector B: √12 + 12 + 12 + 12 + 02+ 12 = 2.2360679775
Calculate the cosine similarity: (4) / (2.2360679775*2.2360679775) = 0.80 (80% similarity between the sentences in both document)
Let’s explore another application where cosine similarity can be utilised to determine a similarity measurement bteween two objects.
Pose matching involves comparing the poses containing key points of joint locations.
Pose estimation is a computer vision task, and it’s typically solved using Deep Learning approaches such as Convolutional Pose Machine, Stacked hourglass, PoseNet etc.
Pose estimation is the process where the position and orientation of the vital body parts and joints of a body are derived from an image or sequence of images.
In a scenario where there is a requirement to quantify the similarity between two poses in Image A and Image B, here is the process that would be taken:
Identify the pose information and derive the location key points(joints) in Image AIdentify the x,y location of all the respective joints required for comparison. Deep Learning solution to pose estimation usually provides information on the location of the joints within a particular pose, along with an estimation confidence score.Repeat step 1 and 2 for Image BPlace all x,y positions of Image A in a vectorPlace all x,y positions of Image B in a vectorEnsure the order of the x,y positions of each joint is the same in both vectorsPerform cosine similarity using both vectors to obtain a number between 0 and 1
Identify the pose information and derive the location key points(joints) in Image A
Identify the x,y location of all the respective joints required for comparison. Deep Learning solution to pose estimation usually provides information on the location of the joints within a particular pose, along with an estimation confidence score.
Repeat step 1 and 2 for Image B
Place all x,y positions of Image A in a vector
Place all x,y positions of Image B in a vector
Ensure the order of the x,y positions of each joint is the same in both vectors
Perform cosine similarity using both vectors to obtain a number between 0 and 1
There are other application domains you might find the utilisation of cosine similarity such as recommendation systems, plagiarism detectors, data mining, and it can even be used as a loss function when training neural networks.
The logic behind cosine similarity is easy to understand and can be implemented in presumably most modern programming languages.
One thing I learnt from exploring these little segments of maths was that most topics are often deemed complex since there are not enough resources to teach the topic adequately. If you have the appropriate learning resources and patience, then it is possible to grasp a large number of mathematics topics.
Learn the basics of AI and Deep Learning with TensorFlow and Keras in this Live Training Session hosted by Me.
Subscribe to get notified when I publish articlesSupport my writing by becoming a referred Medium memberConnect and reach me on LinkedIn
Subscribe to get notified when I publish articles
Support my writing by becoming a referred Medium member
Connect and reach me on LinkedIn