← Nikhil G. Sathe

Book Recommendation System | Content Based Filtering using Cosine Similarity.

This is project that demonstrate content based filtering system.

Checkout the Project Notebook on Github

What is Content Based Filtering System?

It is a recommendation system technique. Recommends items to users based on content or characteristics related to item itself. In case of books Contents, Preface, Description, Authour, etc. can be used. In this demonstration we have only used Description of Book.

How it works?

Book 1: "The Art of Baking Bread"
This book is a comprehensive guide on how to bake various types of bread at home.

Book 2: "The Art of Making Pasta"
This book is a comprehensive guide on how to make different types of pasta from scratch.

Book 3: "The Secrets of Astrophysics"
Explore the fascinating world of astrophysics, uncovering the mysteries of the universe and its celestial objects.

If we look at description of all books, Book 1 and Book 2 are similar 'cause they have common words such as [comprehensive, guide, types]. Where as Book 3 does not have any common words.

Another Example:
Book 1: "The Hunger Games"
A thrilling adventure of a young girl who fights for survival in a cruel and oppressive society.

Book 2: "Divergent"
In a divided society, a young girl discovers her unique abilities and fights against a corrupt system.

Book 3: "The Hitchhiker's Guide to the Galaxy"
A comedic science fiction classic that follows the misadventures of an unwitting human and his alien friend as they travel the universe.

Using this type of analysis of common words we can recommend based on content of item.
There are lot of methods with their pros and cons. but using frequency of common words is one of the simple method to do that.

We can do different types of Arthimatic Procedures to find most and least similar item.

For this project I have use "Cosine Similarity".

What is Cosine Similarity and How it Works?

This Video of Codebasics explains it really.
Codebasics on Youtube
StatQuest with Josh Starmer

Simple explaination:

Take lots of sentences:
Let's say we have a collection of sentences, like:

Sentence 1: "I like cats."
Sentence 2: "I love dogs."
Sentence 3: "Cats and dogs are cute."

Make a set of words used in sentences:
Create a set of all the unique words used in these sentences:

Word set: {I, like, cats, love, dogs, and, are, cute}

Count the frequency of each word for each sentence:
For each sentence, count how many times each word from the word set appears in that sentence:

Sentence 1 word frequency: {I: 1, like: 1, cats: 1, love: 0, dogs: 0, and: 0, are: 0, cute: 0}
Sentence 2 word frequency: {I: 1, like: 0, cats: 0, love: 1, dogs: 1, and: 0, are: 0, cute: 0}
Sentence 3 word frequency: {I: 0, like: 0, cats: 1, love: 0, dogs: 1, and: 1, are: 1, cute: 1}

Calculate the cosine similarity:
To find the cosine similarity between two sentences, we need to compare their word frequencies using angles. We calculate the cosine similarity using the formula:

Cosine Similarity = (Dot product of word frequency vectors) / (Product of their magnitudes)

The dot product of two vectors is the sum of the products of their corresponding elements. The magnitude of a vector is the square root of the sum of the squares of its elements.

For example, to find the cosine similarity between Sentence 1 and Sentence 2:

Dot product of vectors = (1 * 1) + (1 * 0) + (1 * 0) + (0 * 1) + (0 * 1) + (0 * 0) + (0 * 0) + (0 * 0) = 1
Product of magnitudes = √(1² + 1² + 1² + 0² + 0² + 0² + 0² + 0²) * √(1² + 0² + 0² + 1² + 1² + 0² + 0² + 0²) = √3 * √2

Cosine Similarity = 1 / (√3 * √2)

Repeat this process for other pairs of sentences to find their cosine similarities.

The resulting value ranges from -1 to 1, where 1 means the sentences are identical, 0 means they are completely dissimilar, and -1 means they are opposite in terms of word usage.