系统运行python教程
Nowadays, almost every company applies Recommender Systems (RecSys) which is a subclass of information filtering system that seeks to predict the “ rating” or “ preference “ a user would give to an item. They are primarily used in commercial applications. Just to give an example of some famous recommender systems:
如今,几乎每个公司都使用推荐系统(Recmender Systems)(RecSys),它是信息过滤系统的子类,旨在预测用户对某项产品的“评级”或“偏好”。 它们主要用于商业应用。 仅举一些著名的推荐系统为例:
Amazon: Was the first company that applied Recommender Systems extensively around 1998. Based on the user’s preferences was suggesting similar products. It first applied with books and now with all of its products.
亚马逊(Amazon) :是第一家在1998年左右广泛使用Recommender Systems的公司。根据用户的喜好,他建议使用类似的产品。 它首先适用于书籍,现在适用于所有产品。
youtube: Based on the videos that you have watched, it suggested other videos that are likely to like them.
youtube:根据您观看的视频,它建议了其他可能喜欢的视频。
Spotify: Their successful Recommender System made them famous and many people let Spotify play music for them.
Spotify:成功的推荐系统使他们声名大噪,许多人让Spotify为他们播放音乐。
Facebook: It shows on the top of the feed the posts are more likely to be of your interest.
Facebook:它在Feed顶部显示您更可能感兴趣的帖子。
Instagram: It suggests profiles to follow based on your preference.
Instagram :它会根据您的喜好推荐个人资料。
Netflix: It recommends movies for you based on your past ratings. It is worth mentioning the Netflix Prize, an open competition for the best collaborative filtering algorithm to predict user ratings for films, based on previous ratings without any other information about the users or films, i.e. without the users or the films being identified except by numbers assigned for the contest. On September 21, 2009 they awarded the $1M Grand Prize to team “BellKor’s Pragmatic Chaos”. So, you can build your own improved Recommender System and you can become rich one day 🙂
Netflix :它会根据您过去的评分为您推荐电影。 值得一提的是Netflix奖,这是公开竞争的最佳协作过滤算法,用于根据先前的评分来预测电影的用户收视率,而无需提供有关用户或电影的任何其他信息,即除了编号之外,无需识别用户或电影分配给比赛。 2009年9月21日,他们向“ BellKor的务实混乱”团队授予了100万美元的大奖。 因此,您可以构建自己的改进的推荐系统,有一天可以变得富有🙂
Still, there is much interest in Recommender Systems and a great field of research. Our goal here is to show how you can easily apply your Recommender System without explaining the maths below. We will work with the surprise package which is an easy-to-use Python scikit for recommender systems. The available prediction algorithms are:
尽管如此,对推荐系统和广泛的研究领域还是有很多兴趣。 我们的目标是显示如何轻松地应用推荐系统,而无需在下面解释数学。 我们将与合作惊喜包,它是一个易于使用的Python的scikit的推荐系统。 可用的预测算法为:
Surprise Documentation 惊喜文档的屏幕截图We will provide an example of how you can build your own recommender. We will work with the MovieLens dataset, collected by the GroupLens Research Project at the University of Minnesota.
我们将提供一个示例,说明如何构建自己的推荐器。 我们将使用明尼苏达大学的GroupLens研究项目收集的MovieLens数据集。
Let’s get our hands dirty!
让我们弄脏双手吧!
import pandas as pdimport numpy as npcolumns = ['user_id', 'item_id', 'rating', 'timestamp']df = pd.read_csv('ml-100k/u.data', sep='\t', names=columns)columns = ['item_id', 'movie title', 'release date', 'video release date', 'IMDb URL', 'unknown', 'Action', 'Adventure', 'Animation', 'Childrens', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy', 'Film-Noir', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller', 'War', 'Western']movies = pd.read_csv('ml-100k/u.item', sep='|', names=columns, encoding='latin-1')movie_names = movies[['item_id', 'movie title']]combined_movies_data = pd.merge(df, movie_names, on='item_id')combined_movies_data = combined_movies_data[['user_id','movie title', 'rating']]combined_movies_data.head()I will also provide my ratings for some movies from this data set since my ultimate goal is to get recommendations for myself ;). Below you can see my preferences. I will give myself the user_id 1001.
由于我的最终目标是为自己获得推荐,因此,我还将根据该数据集提供一些电影的收视率。 您可以在下面看到我的偏好。 我将给自己user_id 1001 。
# my user_id is the 1001my_ratings = pd.read_csv('my_movies_rating.csv')my_ratingsThe next step is to append my ratings to the rest ratings. Also, we will keep the movies which have at least 25 reviews
下一步是将我的评分附加到其余评分。 另外,我们将保留至少25条评论的电影
combined_movies_data = pd.concat([combined_movies_data, my_ratings], axis=0)# rename the columns to userID, itemID and ratingcombined_movies_data.columns = ['userID', 'itemID', 'rating']# use the transform method group by userID and count # to keep the movies with more than 25 reviewscombined_movies_data['reviews'] = combined_movies_data.groupby(['itemID'])['rating'].transform('count')combined_movies_data= combined_movies_data[combined_movies_data.reviews>25][['userID', 'itemID', 'rating']]Now we have ready our dataset and we can apply different recommender systems using the surprise package.
现在我们已经准备好数据集,并且可以使用Surprise包应用不同的推荐系统。
from surprise import NMF, SVD, SVDpp, KNNBasic, KNNWithMeans, KNNWithZScore, CoClusteringfrom surprise.model_selection import cross_validatefrom surprise import Reader, Dataset# A reader is still needed but only the rating_scale param is requiered.reader = Reader(rating_scale=(1, 5))data = Dataset.load_from_df(combined_movies_data, reader)Clearly, we want to remove the movies that I have rated from the suggested ones. Let’s remove the rated movies:
显然,我们要从建议的电影中删除我已评分的电影。 让我们删除分级的电影:
# get the list of the movie idsunique_ids = combined_movies_data['itemID'].unique()# get the list of the ids that the userid 1001 has ratediids1001 = combined_movies_data.loc[combined_movies_data['userID']==1001, 'itemID']# remove the rated movies for the recommendationsmovies_to_predict = np.setdiff1d(unique_ids,iids1001)My recommendations according to NMF:
根据NMF的建议:
Recommender Systems using SVD
使用SVD的推荐系统
Recommender Systems using SVD++
使用SVD ++的推荐系统
Recommender Systems using KNN with Z-Score
使用KNN和Z-Score的推荐系统
algo = KNNWithZScore()algo.fit(data.build_full_trainset())my_recs = []for iid in movies_to_predict: my_recs.append((iid, algo.predict(uid=1001,iid=iid).est)) pd.DataFrame(my_recs, columns=['iid', 'predictions']).sort_values('predictions', ascending=False).head(10)Recommender Systems using KNN with Z-Score
使用KNN和Z-Score的推荐系统
Recommender Systems using Co-Clustering
使用共聚的推荐系统
We saw earlier that each recommender algorithm suggested different movies. The question is which one performed best and how we can choose between different algorithms.
前面我们看到,每个推荐器算法建议的电影都不相同。 问题是哪一个表现最好,以及我们如何在不同算法之间进行选择。
Like in all Machine Learning problems, we can split our dataset into train and test and evaluate the performance on the test dataset. We will apply Cross Validation (k-fold of k=3) and we will get the average RMSE of the 3-folds.
像所有机器学习问题一样,我们可以将数据集分为训练和测试,以及评估测试数据集的性能。 我们将应用交叉验证(k = 3的k倍),我们将获得3倍的平均RMSE 。
cv = []# Iterate over all recommender system algorithmsfor recsys in [NMF(), SVD(), SVDpp(), KNNWithZScore(), CoClustering()]: # Perform cross validation tmp = cross_validate(recsys, data, measures=['RMSE'], cv=3, verbose=False) cv.append((str(recsys).split(' ')[0].split('.')[-1], tmp['test_rmse'].mean()))pd.DataFrame(cv, columns=['RecSys', 'RMSE'])Average RMSE on the Test Dataset
测试数据集的平均RMSE
As we can see the SVD++ had the best performance (lowest RMSE)
如我们所见,SVD ++的性能最佳(RMSE最低)
We built several Recommender Systems where the RMSE was less than 1. For our models, we took into consideration only the UserID and the ItemID. This post explains briefly the logic of the item-based and user-based collaborative filtering. You can also find an example of item-based collaborative filtering. We can apply different algorithms by taking into account other attributes like the genre of the movie, the released date, the director, the actor, the budget, the duration and so on. In this case, we are referring to Content-based recommenders that treat recommendation as a user-specific classification problem and learn a classifier for the user’s likes and dislikes based on an item’s features. In this system, keywords are used to describe the items and a user profile is built to indicate the type of item this user likes. Finally, we can even take into consideration the user’s attributes, like gender, age, location, language, etc.
我们构建了RMSE小于1的几个Recommender系统。对于我们的模型,我们仅考虑了UserID和ItemID 。 这篇文章简要解释了基于项目和基于用户的协作过滤的逻辑。 您还可以找到基于项目的协作过滤的示例。 我们可以通过考虑其他属性来应用不同的算法,例如电影的流派,发行日期,导演,演员,预算,时长等。 在这种情况下,我们指的是基于内容的推荐者,这些推荐者将推荐视为特定于用户的分类问题,并根据商品的功能了解用户喜欢与不喜欢的分类器。 在此系统中,关键字用于描述项目,并且建立了用户简要表以指示该用户喜欢的项目类型。 最后,我们甚至可以考虑用户的属性,例如性别,年龄,位置,语言等。
Originally published at https://predictivehacks.com.
最初发布在https://predictivehacks.com 。
翻译自: https://towardsdatascience.com/how-to-run-recommender-systems-in-python-1fcea853738f
系统运行python教程
相关资源:四史答题软件安装包exe