Building a Content Based Recommender System for Hotels in Seattle | by Susan Li | Towards Data Science

The cold start problem is a well known and well researched problem for recommender systems, where system is not able to recommend items to users. due to three different situation i.e. for new users, for new products and for new websites.
Content-based filtering is the method that solve this problem. Our system first uses the metadata of new products when creating recommendations, while visitor action is secondary for a certain period of time. And our systems recommend a product to a user based upon the category and description of the product.
Content-based recommendation systems may be used in a variety of domains ranging from recommending web pages, news articles, restaurants, television programs, and hotels. The advantage of content-based filtering is that it doesn’t have a cold-start problem. If you just start out a new website, or any new products can be recommended right away.
Let’s assume we are starting a new online travel agency (OTA), and we have signed up thousands of hotels that are willing to sell on our platform, and we start seeing traffic coming from our website users, but we don’t have any users history, therefore, we are going to build a content-based recommendation systems to analyze hotel descriptions to identify hotels that are of particular interest to the user.
We would like to recommend hotels based on the hotels that a user has already booked or viewed using the cosine similarity. We would recommend hotels with the largest similarity to the ones previously booked or viewed or showed interest by the user. Our recommender system is highly dependent on defining an appropriate similarity measure. Eventually, we select a subset of hotels to display to the user or to determine an order in which to display the hotels.
It’s very hard to find public available hotel description data, therefore, I collected them by myself from each hotel’s homepage for over 150 hotels in Seattle area, that includes downtown business hotels, boutique hotels and bed and breakfast, airport business hotels, inns near the universities, motels in the middle of nowhere, and so on. The data can be found here.
import pandas as pdimport numpy as npfrom nltk.corpus import stopwordsfrom sklearn.metrics.pairwise import linear_kernelfrom sklearn.feature_extraction.text import CountVectorizerfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.decomposition import LatentDirichletAllocationimport reimport randomimport plotly.graph_objs as goimport plotly.plotly as pyimport cufflinkspd.options.display.max_columns = 30from IPython.core.interactiveshell import InteractiveShellimport plotly.figure_factory as ffInteractiveShell.ast_node_interactivity = 'all'from plotly.offline import iplotcufflinks.go_offline()cufflinks.set_config_file(world_readable=True, theme='solar')df = pd.read_csv('Seattle_Hotels.csv', encoding="latin-1")df.head()print('We have ', len(df), 'hotels in the data')
Have a look few hotel name and description pairs.
print_description(10)
print_description(100)
Everyone knows Seattle’s Pike Place Market, it is way more than a public farmers market. It is a historical vibrant tourism attraction comprised of hundreds of farmers, craftspeople, small businesses. The hotel industry thrives on location, tourists look for a hotel that is possibly nearest to downtown and / or must-visit attractions of the city. Therefore, every hotel would brag about it if it is not too far from the hotel.
df['word_count'] = df['desc'].apply(lambda x: len(str(x).split()))desc_lengths = list(df['word_count'])print("Number of descriptions:",len(desc_lengths),      "\nAverage word count", np.average(desc_lengths),      "\nMinimum word count", min(desc_lengths),      "\nMaximum word count", max(desc_lengths))
Many hotels use description to their full potential, know how to utilize captivating descriptions to appeal to travelers’ emotions to drive direct bookings. Their descriptions may be longer than others.
The test is pretty clean, we don’t have a lot to do, but just in case.
Create a TF-IDF matrix of unigrams, bigrams, and trigrams for each hotel.
Compute similarity between all hotels using sklearn’s linear_kernel (equivalent to cosine similarity in our case).
Define a function that takes in hotel name as input and returns the top 10 recommended hotels.
Let’s make some recommendations!
recommendations('Hilton Seattle Airport & Conference Center')
A good test on whether our similarity works is that the content based recommender returns all airport hotels when an airport hotel is a seed.
We can also ask Google. The following are recommended by Google for “Hilton Seattle Airport & Conference Center”:
Three out of four recommended by Google were also recommended by us.
The following are recommended by tripadvisor for “Hilton Seattle Airport & Conference Center”:
Not bad either.
Try a bed & breakfast.
recommendations("The Bacon Mansion Bed and Breakfast")
The following are recommended by Google for “The Bacon Mansion Bed and Breakfast”:
Cool!
The following are recommended by tripadvisor for “The Bacon Mansion Bed and Breakfast”, which I was not impressed.
Jupyter notebook can be found on Github, if you prefer, this is a nbviewer version.
Have a productive week!