R language: Recommenderlab Package Summary and application Case 1. Recommendation system: The whole idea of Recommenderlab package
The Recommenderlab package provides a framework for developing and testing recommended algorithms with scoring data and 0-1 data.
It provides several basic algorithms and allows users to use their own algorithms using the registration mechanism
The data type of the Recommender package is constructed using the S4 class.
(1) Scoring matrix Data interface: Use abstract Raringmatrix to provide an interface for scoring data. Raringmatrix uses a lot of operations like matrix objects, such as dim (), Dimnames (), rowcounts (), Colmeans (), Rowmeans (), Colsums (), Rowmeans () It also adds some special methods of operation, such as sample (), which is used to sample from a user (i.e., a row), and image () can generate a pixel graph. The two specific applications of Raringmatrix are Realratingmatrix and Binaryratingmatrix, respectively, corresponding to the different conditions of the scoring matrix. The Realratingmatrix uses a scoring matrix of real values, stored in the sparse matrix (spare matrix) format defined by the matrix package; Binaryratingmatrix uses a 0-1 scoring matrix, stored in the Itemmatrix defined by the Arule package.
(2) store the recommended model and recommend it based on the model. Class Recommender uses data structures to store recommended models. The method of creation is: Rencommender (data=ratingmatrix,method,parameter=null), which returns a Rencommender object, which can be used to make top-n recommended predictions:
Predict (Object,newdata,n,type=c (' topnlist,ratings '),...)
(3) The user can customize their own recommendation algorithm by using the registration mechanism provided by the registry package. The registration mechanism calls Recommenderregistry and stores the name and brief description of the recommended algorithm.
(4) Evaluate the performance of the recommended algorithm: The Recommender package provides an object of the Evaluationscheme class for creating and saving the evaluation plan. Create the function as follows: Evaluatiomscheme (Data,method,train,k,given) Here the method can be divided into simple, self-help sampling, K-fold cross-validation and so on. Next you can use function Evalute () to evaluate the performance of multiple evaluation algorithms using the evaluation plan.
2. Example Analysis
Library (Recommenderlab)
Library (GGPLOT2)
# #数据处理与数据探索性分析
Data (Movielense)
Image (Movielense)
# get ratings
Ratings.movie <-data.frame (ratings = getratings (movielense))
Summary (ratings.movie$ratings)
# # Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1.00 3.00 4.00 3.53 4.00 5.00
Ggplot (Ratings.movie, AES (x = ratings)) + Geom_histogram (fill = "Beige", color = "BLACK",
Binwidth = 1, alpha = 0.7) + Xlab ("rating") + Ylab ("Count")
# Standardization
Ratings.movie1 <-data.frame (ratings = getratings (normalize (movielense, method = "Z-score")))
Summary (ratings.movie1$ratings)
# # Min. 1st Qu. Median Mean 3rd Qu. Max.
# # -4.850-0.647 0.108 0.000 0.751 4.130
Ggplot (Ratings.movie1, AES (x = ratings)) + Geom_histogram (fill = "Beige", color = "BLACK",
Alpha = 0.7) + Xlab ("rating") + Ylab ("Count")
# Number of users ' movie reviews
Movie.count <-data.frame (count = rowcounts (movielense))
Ggplot (Movie.count, AES (x = count)) + geom_histogram (fill = "Beige", color = "BLACK",
Alpha = 0.7) + Xlab ("Counts of users") + Ylab ("Counts of movies rated")
Rating.mean <-data.frame (rating = Colmeans (movielense))
Ggplot (Rating.mean, AES (x = rating)) + geom_histogram (fill = "Beige", color = "BLACK",
Alpha = 0.7) + Xlab ("rating") + Ylab ("Counts of movies")
# #推荐算法的情况
# First Look at the methods you can use
Recommenderregistry$get_entries (DataType = "Realratingmatrix")
#对于realRatingMatrix有六种方法: IBCF (item-based recommendation), UBCF (user-based recommendation), SVD (Matrix factorization), PCA (principal component analysis), random (randomly recommended), POPULAR (popularity-based recommendations)
#利用前940位用户建立推荐模型
M.recomm <-recommender (movielense[1:940], method = "IBCF")
M.recomm
#对后三位用户进行推荐预测, using the Predict () function, the default is TOPN recommendation, where n=3 is taken. After predicting a Topnlist object, you can turn it into a list and see the predicted results.
(Ml.predict <-Predict (M.recomm, movielense[941:943], n = 3))
STR (ML.PREDICT)
As (ml.predict, "list") #预测结果
#代码示例
Library (Recommenderlab)
Data (Movielense)
Scheme <-Evaluationscheme (movielense, method = "Split", train = 0.9, k = 1,
given = ten, goodrating = 4)
Algorithms <-list (popular = list (name = "popular", param = List (normalize = "Z-score"),
UBCF = List (name = "UBCF", param = List (normalize = "Z-score", method = "cosine",
nn = +, minrating = 3), IBCF = List (name = "IBCF", param = List (normalize = "Z-score"))
Results <-Evaluate (scheme, algorithms, n = C (1, 3, 5, 10, 15, 20))
Plot (results, annotate = 1:3, legend = "TopLeft") #ROC
Plot (results, "prec/rec", annotate = 3) #precision-recall
# Build a recommendation model according to the evaluation plan
Model.popular <-recommender (getData (scheme, "Train"), method = "popular")
MODEL.IBCF <-recommender (getData (scheme, "Train"), method = "IBCF")
MODEL.UBCF <-recommender (getData (scheme, "Train"), method = "UBCF")
# Make predictions for the recommendation model
Predict.popular <-Predict (model.popular, getData (scheme, "known"), type = "ratings")
PREDICT.IBCF <-Predict (MODEL.IBCF, getData (scheme, "known"), type = "ratings")
PREDICT.UBCF <-Predict (MODEL.UBCF, getData (scheme, "known"), type = "ratings")
# Make a calculation of the error
Predict.err <-Rbind (Calcpredictionerror (Predict.popular, getData (scheme, "unknown")),
Calcpredictionerror (PREDICT.UBCF, getData (scheme, "unknown")), Calcpredictionerror (PREDICT.IBCF,
GetData (scheme, "Unknown")))
Rownames (Predict.err) <-C ("POPULAR," UBCF "," IBCF ")
Predict.err
The parameters "know" and "unknow" of the #calcPredictionError () indicate further partitioning of the test set: "Know" denotes items that the user has scored, to be used for forecasting, "unknow" means that the user has scored, Items to be predicted for easy model evaluation.
R Language: Summary and application case of Recommenderlab package