Skip to content

Instantly share code, notes, and snippets.

recommend('Hampton Inn Suites National HarborAlexandria Area')
recommend('Silver Sands Oceanfront Motel')
recommend('AC Hotel Miami Beach')
def recommend(hotel_name):
# get index of hotel in ratings matrix
index = ratings_matrix.columns.get_loc(hotel_name)
# person coefficients of the hotel
hotel_coeff = pearson_matrix[index]
# create dataframe and sort by coeff and get top 10
return pd.DataFrame({'pearson':hotel_coeff, 'hotel': ratings_matrix.columns}).sort_values('pearson', ascending=False).head(1
pearson_matrix = np.corrcoef(ratings_svd)
pearson_matrix.shape
from sklearn.decomposition import TruncatedSVD
SVD = TruncatedSVD(n_components=10, random_state=34)
ratings_svd = SVD.fit_transform(ratings_transpose)
ratings_svd.shape
import pandas as pd
import numpy as np
import sklearn
ratings_transpose = ratings_matrix.T
ratings_transpose.head(5)
ratings_matrix = ratings.pivot_table(values='rating', index='user', columns='hotel', fill_value=0)
ratings_matrix.head(5)
df = pd.read_csv('hotel_reviews.csv')
df.head()
ratings = df[['reviews.username','name','reviews.rating']]
ratings = ratings.drop_duplicates()
ratings.columns = ["user","hotel","rating"]
#8866 unique ratings
ratings.head(5)