Spaces:
Runtime error
Runtime error
File size: 1,171 Bytes
3ca8293 3d26679 3ca8293 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
def create_model(df):
# Train model with the key words
count = CountVectorizer(stop_words='english')
count_matrix = count.fit_transform(df['key_words'])
# Get the cosine similarity matrix from the key words
from sklearn.metrics.pairwise import cosine_similarity
cosine_sim = cosine_similarity(count_matrix, count_matrix)
# Get the cosine similarity matrix from the key words
indices = pd.Series(df.index)
return cosine_sim, indices
def recommendations(id, cosine_sim, indices, df):
# Get the cosine similarity matrix from the key words
id = indices[indices == id].index[0]
# Get the cosine similarity matrix from the key words
similarity_scores = pd.Series(cosine_sim[id]).sort_values(ascending=False)
# Get the cosine similarity matrix from the key words
top_10 = list(similarity_scores.iloc[1:11].index)
# Remove restaurants with same name
top_10 = [i for i in top_10 if df['RestaurantName'][i] != df['RestaurantName'][id]]
# Get the cosine similarity matrix from the key words
return df.iloc[top_10]
|