Nearly all the big websites use a recommendation system to suggest us more products, movies, search queries etc based on our choices and preferences. We can make our own simple recommendation system in Python and it's pretty easy. So let's begin!
There are many different approaches used by different websites to recommend relevant content to a user, but we will implement a technique called Content Based Filtering.
Content Based Filtering means suggesting products (movies,books etc) to a user that have properties similar to the ones user has already viewed/liked. For instance, if I viewed a book of Romantic genre, I may also like another book of the same genre.
Content Based Recommendation System in Python
For demonstration purposes, we will create a Book class having some attributes/values/tags. We will compare these tags to compute the "best match" for a book. That is, given a book X, we will try to find another book Y that is most similar to X based on its content. So, Y is likely to be read by a person who read X.
Here is the complete implementation in python:
#Book recommendation system using content based filtering class Book(object): def __str__(self): return self.data['name'] def __init__(self,name,romantic=False,thriller=False,hasSuperVillain=False,fiction=True,scifi=False): self.data ={} self.data['name'] = name self.data['romantic'] = romantic self.data['thriller'] = thriller self.data['hasSuperVillain'] = hasSuperVillain self.data['fiction'] = fiction self.data['scifi'] = scifi def bestMatch(self, *books): #Returns the best match for a book based on its properties match = None #The best match bestScore = 0 #The score, or number of traits that are similar for the two books for book in books: #Initialise the score score = 0 #Iterate over all the properties/tags and compare them for key in self.data: if self.data[key] == book.data[key]: score += 1 #Increment the score by one if score > bestScore: #Update bestScore and match bestScore = score match = book return "Best Score: %s \nBook: %s\n"%(bestScore,match) #Declare four books book1 = Book('Book One',True,False,False,True,True) book2 = Book('Book Two',True,False,True,False,True) book3 = Book('Book Three',False,True,True,True,False) book4 = Book('Book Four',False,True,True,False,True) print book3.bestMatch(book1,book2,book4) #Find the best match for book3
The above program will output:
Best Score: 3 Book: Book Four
The above code is self explanatory. We define four Book objects having some properties i.e. some books are of romantic genre, others are not; some are fiction, others are not and so on.
Then, for book3, we try to find the book that best matches it's content (or attributes). We see that book4 and book3 have three properties in common (Both are not romantic,both are thriller and both have a super villain). This means that book3 and book4 are more similar than book3 and book1 (or book3 and book2). So, if a user is interested in book3, he/she may be interested in book4 as well.
This is not the best (or even a very good) method of creating a recommendation system, but it should give you a fair idea of where to begin.
Here is the wiki page about recommender systems:
https://en.wikipedia.org/wiki/Recommender_system
Keep learning, keep hacking!
No comments:
Post a Comment