In today’s blog, we are going to discuss Cosine Similarity.
Cosine similarity is a measure of similarity between two vectors matrix. It is a metric that measures the perspective between two vectors. The cosine similarity can take values between -1 and 1.
A cosine similarity of 1 shows that the two vectors are the same, even as a cosine similarity of -1 shows that the two vectors are different from each other. Example to calculate cosine similarity in Python:
- Import Libraries: Numpy and Scikit Learn to calculate the cosine similarity
1 2 |
import numpy as np from sklearn.metrics.pairwise import cosine_similarity |
- Define two vectors sample
123456789vectors vector1 = np.array([2, 3, 1])vector2 = np.array([1, 2, 3])# Reshape the vectors to 2D arrays for compatibility with cosine_similarity functionvector1 = vector1.reshape(1, -1)vector2 = vector2.reshape(1, -1)# Calculate cosine similarity using sklearn's cosine_similarity functionsimilarity_score = cosine_similarity(vector1, vector2) - Reshape the vectors to 2D arrays for compatibility with the cosine_similarity function
12vector1 = vector1.reshape(1, -1)vector2 = vector2.reshape(1, -1) - Calculate cosine similarity using sklearn’s cosine_similarity function
1similarity_score = cosine_similarity(vector1, vector2) - The output is a 2D array, so we need to access the similarity score as follows:
12345678cosine_similarity_score = similarity_score[0][0]print("Vector 1:", vector1) print("Vector 2:", vector2)print("Cosine Similarity Score:", cosine_similarity_score)output:>>>Vector 1: [[2 3 1]]>>>Vector 2: [[1 2 3]]>>>Cosine Similarity Score: 0.7857142857142858 - The output for cosine similarity between two vectors is 78.57%, So these vectors are highly similar to each other.
Cosine Similarity in E-commerce:
By adding this user or customer experience can be improved. Cosine similarity helps e-commerce platforms.
Personalization: Cosine similarity can be used to customize consumer enjoyment on an e-trade internet site. For instance, the store should use cosine similarity to show distinctive merchandise to exceptional customers based totally on their hobbies.
Recommendation System: Cosine Similarity helps in the recommender system of e-commerce. Suppose that a customer visits an e-commerce site like bagisto and searches for some products, the search history will save and model use the cosine similarity method and show similar products to the customer.
For instance, if a user has bought a couple of shoes from an e-commerce platform like bagisto, the shop suggests different shoes which might be just like the ones that the person has already purchased.
Product Search: Cosine similarity can be used to enhance the outcomes of product searches. For instance, if a consumer searches for “black shoes,” the store may want to use cosine similarity to rank the outcomes primarily based on how comparable the products are to the person’s search query.
The Significance of Cosine Similarity in E-commerce:
Cosine similarity is an effective tool that can be used to improve the customer experience on an e-commerce internet site. By using cosine similarity, shops can advise products that can be much more likely to be of interest to their customers, enhance the results of product searches, and customize the person’s enjoyment.