基於內容的推薦例子(電影推薦)

推薦例子介紹

根據典型關鍵數據python

導演git

演員github

關鍵字app

題材ide

'keywords', 'cast', 'genres', 'director'ui

構造天然語言的組合特徵,利用CountVectorizer計算每一個詞出現的次數,做爲特徵向量,this

使用餘弦類似性構造全部電影之間的類似性。spa

 

代碼

https://github.com/fanqingsong/Content-based-Recommandation-Enginecode

import pandas as pd from sklearn.feature_extraction.text import CountVectorizer from sklearn.metrics.pairwise import cosine_similarity def get_title_from_index(index): return df[df.index == index]["title"].values[0] def get_index_from_title(title): return df[df.title == title]["index"].values[0] # Reading CSV File
df = pd.read_csv("movie_dataset_content.csv", encoding='utf-8') # Selecting Features
features = ['keywords', 'cast', 'genres', 'director'] # Creating a column in DF which combines all selected features
for feature in features: df[feature] = df[feature].fillna('') def combine_features(row): return row['keywords'] + " " + row['cast'] + " " + row["genres"] + " " + row["director"] df["combined_features"] = df.apply(combine_features, axis=1) # making an object of CountVectorizer class to create count matrix
cv = CountVectorizer() # Creating count matrix from this new combined column
count_matrix = cv.fit_transform(df["combined_features"]) # Computing the Cosine Similarity based on the count_matrix
cosine_sim = cosine_similarity(count_matrix) movie_liked_by_user = "Thor"

# Getting index of this movie from its title
liked_movie_index = get_index_from_title(movie_liked_by_user) similar_movies = list(enumerate(cosine_sim[liked_movie_index])) # Get a list of similar movies in descending order of similarity score
predictions = sorted(similar_movies, key=lambda x: x[1], reverse=True) # Print titles of 10 predicted movies
i = 0 for movie in predictions: print(get_title_from_index(movie[0])) i = i+1
    if i > 10: break

 

運行

root@DESKTOP-OGSLB14:~/mine/Content-based-Recommandation-Engine# python3 content_based_recommender.py
Thor
Thor: The Dark World
The Avengers
Captain America: The Winter Soldier
Avengers: Age of Ultron
Captain America: Civil War
Pirates of the Caribbean: Dead Man's Chest
Cinderella
Jack Ryan: Shadow Recruit
The Amazing Spider-Man 2
Captain America: The First Avenger
root@DESKTOP-OGSLB14:~/mine/Content-based-Recommandation-Engine#

orm

相關文章
相關標籤/搜索