A recommendation system is one of the popular applications of Data Science. A restaurant recommendation system is an application that recommends similar restaurants to a customer according to the customer’s taste. If you want to learn how to build a restaurant recommendation system, this article is for you. This article will take you through how to build a restaurant recommendation system using Python.
Restaurant Recommendation System using Python
A restaurant recommendation system is an application that recommends similar restaurants to a customer according to the customer’s taste. To build a restaurant recommender system using Python, I have collected data from Kaggle. You can download the dataset for this task from here.
Now let’s import the necessary Python libraries and the dataset we need for this task:
import numpy as np import pandas as pd from sklearn.feature_extraction import text from sklearn.metrics.pairwise import cosine_similarity data = pd.read_csv("TripAdvisor_RestauarantRecommendation.csv") print(data.head())
Name Street Address \ 0 Betty Lou's Seafood and Grill 318 Columbus Ave 1 Coach House Diner 55 State Rt 4 2 Table Talk Diner 2521 South Rd Ste C 3 Sixty Vines 3701 Dallas Pkwy 4 The Clam Bar 3914 Brewerton Rd Location Type \ 0 San Francisco, CA 94133-3908 Seafood, Vegetarian Friendly, Vegan Options 1 Hackensack, NJ 07601-6337 Diner, American, Vegetarian Friendly 2 Poughkeepsie, NY 12601-5476 American, Diner, Vegetarian Friendly 3 Plano, TX 75093-7777 American, Wine Bar, Vegetarian Friendly 4 Syracuse, NY 13212 American, Bar, Seafood Reviews No of Reviews \ 0 4.5 of 5 bubbles 243 reviews 1 4 of 5 bubbles 84 reviews 2 4 of 5 bubbles 256 reviews 3 4.5 of 5 bubbles 235 reviews 4 4 of 5 bubbles 285 reviews Comments Contact Number \ 0 NaN +1 415-757-0569 1 Both times we were there very late, after 11 P... +1 201-488-4999 2 Waitress was very friendly but a little pricey... +1 845-849-2839 3 Not sure why I went there for the second time.... +1 469-620-8463 4 Doesn't look like much from the outside but wa... +1 315-458-1662 Trip_advisor Url \ 0 https://www.tripadvisor.com//Restaurant_Review... 1 https://www.tripadvisor.com//Restaurant_Review... 2 https://www.tripadvisor.com//Restaurant_Review... 3 https://www.tripadvisor.com//Restaurant_Review... 4 https://www.tripadvisor.com//Restaurant_Review... Menu Price_Range 0 Check The Website for a Menu $$ - $$$ 1 Check The Website for a Menu $$ - $$$ 2 http://tabletalkdiner.com/menu/breakfast/ $$ - $$$ 3 https://sixtyvines.com/menu/plano-tx/ $$ - $$$ 4 Check The Website for a Menu $$ - $$$
I will select two columns from the dataset for the rest of the task (Name, Type):
data = data[["Name", "Type"]] print(data.head())
Name Type 0 Betty Lou's Seafood and Grill Seafood, Vegetarian Friendly, Vegan Options 1 Coach House Diner Diner, American, Vegetarian Friendly 2 Table Talk Diner American, Diner, Vegetarian Friendly 3 Sixty Vines American, Wine Bar, Vegetarian Friendly 4 The Clam Bar American, Bar, Seafood
Before moving forward, let’s have a look at whether the data contains any null values or not:
print(data.isnull().sum())
Name 0 Type 13 dtype: int64
So the data has some null values in the Type column. I will delete the rows containing null values before moving forward:
data = data.dropna()
The type of restaurant is a valuable feature in the data to build a recommendation system. The type column here represents the category of restaurants. For example, if a customer likes vegetarian-friendly restaurants, he will only look at the recommendations if they are vegetarian friendly too. So I will use the Type column as the feature to recommend similar restaurants to the customer:
feature = data["Type"].tolist() tfidf = text.TfidfVectorizer(input=feature, stop_words="english") tfidf_matrix = tfidf.fit_transform(feature) similarity = cosine_similarity(tfidf_matrix)
Now I will set the name of the restaurant as an index so that we can find similar restaurants by giving the name of the restaurant as an input:
indices = pd.Series(data.index, index=data['Name']).drop_duplicates()
Now here’s how to write a function to recommend similar restaurants:
def restaurant_recommendation(name, similarity = similarity): index = indices[name] similarity_scores = list(enumerate(similarity[index])) similarity_scores = sorted(similarity_scores, key=lambda x: x[1], reverse=True) similarity_scores = similarity_scores[0:10] restaurantindices = [i[0] for i in similarity_scores] return data['Name'].iloc[restaurantindices] print(restaurant_recommendation("Market Grill"))
23 The Lion's Share 154 Houlihan's 518 Midgley's Public House 568 Aspen Creek Grill 770 Pete's Sunset Grille 1190 Paul Martin's American Grill 1581 Aviation Grill 1872 Aviation Grill 2193 Crest Bar & Grill 2612 Tahoe Joe's Famous Steakhouse Name: Name, dtype: object
Summary
So this is how to build a restaurant recommender system using the Python programming language. A restaurant recommender system is an application that recommends similar restaurants to a customer according to the customer’s taste. I hope you liked this article on building a restaurant recommender system using Python. Feel free to ask valuable questions in the comments section below.