What is cosine similarity and how to calculate it?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

What is cosine similarity and how to calculate it?

What is cosine similarity and how to calculate it?

This recipe explains what is cosine similarity and how to calculate it

0

Recipe Objective

Cosine similarity gives us the sense of cos angle between vectors. When vector are in same direction, cosine similarity is 1 while in case of perpendicular, it is 0. It is given by (1- cosine distance).

So this recipe is a short example on what cosine similarity is and how to calculate it. Let's get started.

Step 1 - Import the library

from scipy import spatial

Let's pause and look at these imports. We have imported spatial library from scipy class Scipy contains bunch of scientific routies like solving differential equations.

Step 2 - Setup the Data

x=[1,2,3] y=[-1,-2,-3]

Let us create two vectors list.

Step 3 - Calculating cosine similarity

z=1-spatial.distance.cosine(x,y)

We have first calucated cosine distance and the subtracting it from 1 has given us cosine similarity

Step 4 – Printing results

print(z)

Simply use print function to print new appended list.

Step 5 - Let's look at our dataset now

Once we run the above code snippet, we will see:

-1.0

Relevant Projects

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.