[英]Clustering - how to recommend movie based on selected movie?
正如我的問題所述,我正在使用聚類算法。 我一直在對 IMDB 中的電影進行聚類,我有 15 個聚類,每個聚類包含一個流派組合。 現在我正在為我推薦一部電影的部分而苦苦掙扎,我該怎么做? 我曾嘗試制作假電影數據,方法是制作電影的特征,然后將該數據放入 k-means 算法中,但當然這不起作用,因為它沒有可比較的數據。
我想要的是我可以選擇一部電影或創建一些數據,然后從所選電影所在的集群中獲取前 20 個電影列表。
目前我只是通過預先選擇一個集群來以一種非常便宜的方式來完成它。
cluster_test = prediction_result[prediction_result['cluster'] == 5].sort_values(by =['averageRating', 'numVotes'], ascending=False)
cluster_test.head(15)
像這樣在這種情況下顯示來自集群 5 的頂級電影
這是一種方法。
# Check out all the movies and their respective IDs
movie_titles = pd.read_csv('C:\\movies.csv')
movie_titles = movie_titles.head(10000)
print(movie_titles.shape)
#Import TfIdfVectorizer from scikit-learn
from sklearn.feature_extraction.text import TfidfVectorizer
#Define a TF-IDF Vectorizer Object. Remove all english stop words such as 'the', 'a'
tfidf = TfidfVectorizer(stop_words='english')
#Replace NaN with an empty string
movie_titles['genres'] = movie_titles['genres'].fillna('')
#Construct the required TF-IDF matrix by fitting and transforming the data
tfidf_matrix = tfidf.fit_transform(movie_titles['genres'])
#Output the shape of tfidf_matrix
tfidf_matrix.shape
# Import linear_kernel
from sklearn.metrics.pairwise import linear_kernel
# Compute the cosine similarity matrix
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
#Construct a reverse map of indices and movie titles
indices = pd.Series(movie_titles.index, index=movie_titles['title']).drop_duplicates()
# Function that takes in movie title as input and outputs most similar movies
def get_recommendations(title, cosine_sim=cosine_sim):
# Get the index of the movie that matches the title
idx = indices[title]
# Get the pairwsie similarity scores of all movies with that movie
sim_scores = list(enumerate(cosine_sim[idx]))
# Sort the movies based on the similarity scores
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
# Get the scores of the 10 most similar movies
sim_scores = sim_scores[1:11]
# Get the movie indices
movie_indices = [i[0] for i in sim_scores]
# Return the top 10 most similar movies
return movie_titles['title'].iloc[movie_indices]
get_recommendations('Toy Story (1995)')
結果:
# data:
# https://grouplens.org/datasets/movielens/
在這個例子中,我們看到了 Cosine Similarity 的實現。 您可以使用 KNN 做一些非常相似的事情。 我相信有很多方法可以解決這類問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.