python3機器學習經典實例-第五章構建推薦引擎28

生成電影推薦

至此,我們已經創建了一個推薦引擎的各個不同組成部分,接下來生成一個實際的電影推薦系統。本節將用到前面各節中構建的函數來構建電影推薦引擎,接下來看看具體如何實現。

  • 創建movie_recommendations.py文件,載入必要的資料庫

import jsonimport numpy as npfrom euclidean_score import euclidean_scorefrom pearson_score import pearson_scorefrom find_similar_users import find_similar_users

  • 定義一個為給定用戶生成電影推薦的函數。首先檢查該用戶是否存在於資料庫中:

# Generate recommendations for a given userdef generate_recommendations(dataset, user): if user not in dataset: raise TypeError(User + user + not present in the dataset)

  • 計算該用戶與資料庫中其他用戶的皮爾遜相關係數:

total_scores = {} similarity_sums = {} for u in [x for x in dataset if x != user]: similarity_score = pearson_score(dataset, user, u) if similarity_score <= 0: continue

  • 找到還未被該用戶評分的電影:

for item in [x for x in dataset[u] if x not in dataset[user] or dataset[user][x] == 0]: total_scores.update({item: dataset[u][item] * similarity_score}) similarity_sums.update({item: similarity_score})

  • 如果該用戶看過資料庫中所有的電影,那就不能為用戶推薦電影。對該條件做如下處理:有了皮爾遜相關係數列表,下面生成一個電影評分標準化列表:對皮爾遜相關係數進行降序排列:最後提取出推薦的電影:

if len(total_scores) == 0: return [No recommendations possible] # Create the normalized list movie_ranks = np.array([[total/similarity_sums[item], item] for item, total in total_scores.items()]) # Sort in decreasing order based on the first column movie_ranks = movie_ranks[np.argsort(movie_ranks[:, 0])[::-1]] # Extract the recommended movies recommendations = [movie for _, movie in movie_ranks] return recommendations

  • 定義main函數,載入數據集:為Michael Henry生成推薦:用戶John Carson看過所有電影,因此在為他推薦電影時,應該顯示0推薦。來看看程序運行結果是否正確:

if __name__==__main__: data_file = movie_ratings.json with open(data_file, r) as f: data = json.loads(f.read()) user = Michael Henry print ("
Recommendations for " + user + ":") movies = generate_recommendations(data, user) for i, movie in enumerate(movies): print (str(i+1) + . + movie) user = John Carson print ("
Recommendations for " + user + ":") movies = generate_recommendations(data, user) for i, movie in enumerate(movies): print (str(i+1) + . + movie)

結果輸出out

Recommendations for Michael Henry:1. Jerry Maguire2. Inception3. Anger ManagementRecommendations for John Carson:1. No recommendations possible

推薦閱讀:

PyQt5如何實現窗口關閉淡出效果?
學習編程的時候不會那些數學題還可以繼續學嗎?
用Python獲取整個學校女生的電話和QQ

TAG:機器學習 | Python |