Skip to content

Instantly share code, notes, and snippets.

@ianlcassidy
Created December 17, 2018 16:47
Show Gist options
  • Save ianlcassidy/f985c0b83dee89424f5167892739df1a to your computer and use it in GitHub Desktop.
Save ianlcassidy/f985c0b83dee89424f5167892739df1a to your computer and use it in GitHub Desktop.
from typing import Dict
import pandas as pd
from sklearn.metrics import euclidean_distances
def get_personalized_hotel_recommendations(df: pd.DataFrame, user_features: Dict) -> pd.DataFrame:
# features used to compute the similarity
features = ['distance', 'avg_rate', 'star_rating', 'user_rating']
# create the features
df_features = df[features].copy()
df_features = normalize_features(df_features)
# artifically set the distance of the user anchor to be the min of the
# normalized distance
user_features['distance'] = df_features['distance'].min()
df_user = pd.DataFrame([user_features])
df_features = pd.concat([df_user, df_features], sort=False)
# compute the distances
X = df_features.values
Y = df_features.values[0].reshape(1, -1)
distances = euclidean_distances(X, Y)
df_sorted = df.copy()
df_sorted['similarity_distance'] = distances[1:]
return df_sorted.sort_values('similarity_distance').reset_index(drop=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment