set.seed ( 1234 )
#加载数据包
library ( "recommenderlab" )
#构造数据运用recommenderlab包实现协同过滤推荐,其数据类型采用S4类构造,需通过as()函数转为raringMatrix类型。
val1<- matrix ( sample ( c ( as.numeric ( 0 : 5 ) ,NA ) ,50 ,replace = TRUE ,prob = c ( rep ( .4 / 6 , 6 ) , .6 ) ) ,ncol = 10 , dimnames = list ( user = paste ( "u" ,1 : 5 ,sep = '' ) ,item = paste ( "i" ,1 : 10 ,sep = '' ) ) )
val2 <- as ( val1, "realRatingMatrix" )

数据转换
val3<- normalize ( val2 )
#二元分类转换,normalize()函数进行标准化处理,标准化的目的是为了去除用户评分的偏差
val4 <- binarize ( val3 , minRating = 4 )
val5 <- as ( val4 , "matrix" )

数据可视化
接下来,我们采用MovieLense数据集,
data ( MovieLense )
key1 <- sample ( MovieLense , 943 , replace = F )
image ( MovieLense )

hist ( getRatings ( normalize ( MovieLense ) ) , breaks = 100 )

hist ( rowCounts ( key1 ) , breaks = 50 )

建立模型
对于realRatingMatrix有六种方法:IBCF(基于物品的推荐)、UBCF(基于用户的推荐)、PCA(主成分分析)、RANDOM(随机推荐)、SVD(矩阵因子化)、POPULAR(基于流行度的推荐)
建立协同过滤推荐算法模型,主要运用recommender(data=ratingMatrix,method,parameter=NULL)函数,getModel()可查看模型参数
key1_recom <- Recommender (key1 , method = "IBCF" )
key1_popul <- Recommender ( key1, method = "POPULAR" )
#查看模型方法
names ( getModel ( key1_recom ) )

模型预测
TOP-N预测
对模型预测可运用predict()函数,在此分别以TOP-N预测及评分预测为例,预测第940-943位观影者的评分情况。n表示最终为TOP-N的列表推荐,参数type = "ratings"表示运用评分预测观影者对电影评分,模型结果均需转为list或矩阵表示
pred <- predict ( key1_popul ,key1 [ 940 : 943,] , n = 5 )
as ( pred , "list" )

#top-N为有序列表,抽取最优推荐子集
pred3 <- bestN ( pred , n = 3 )
as ( pred3 , "list" )

#评分预测
rate <- predict ( key1_popul , key1 [ 940 : 943 ] , type = "ratings" )
as ( rate , "matrix" ) [ , 1 : 5 ]

预测模型评价
评分预测模型评价
eva <- evaluationScheme (key1 [ 1 : 800 ] , method = "split" , train = 0.9,given = 15)
method="split"&train=0.9为按90%划分训练测试集合,given为评价的类目数
r_eva1<- Recommender ( getData ( eva , "train" ) , "UBCF" )
p_eva1<- predict ( r_eva1 , getData ( eva, "known" ) , type = "ratings" )
r_eva2 <- Recommender ( getData ( eva, "train" ) , "IBCF" )
p_eva2 <- predict ( r_eva2 , getData ( eva, "known" ) , type = "ratings" )
c_eva1 <- calcPredictionAccuracy ( p_eva1 , getData ( eva , "unknown" ) )
c_eva2 <- calcPredictionAccuracy ( p_eva2 , getData ( eva , "unknown" ) )
error <- rbind ( c_eva1 , c_eva2 )
rownames ( error ) <- c ( "UBCF" , "IBCF" )
计算预测模型的准确度

TOP-N预测模型评价
通过4-fold交叉验证方法分割数据集,运用evaluate()进行TOP-N预测模型评价,评价结果可通过ROC曲线及准确率-召回率曲线展示:
#4-fold交叉验证
tops <- evaluationScheme ( key1 [ 1 : 800 ] , method = "cross" , k = 4 , given = 3 ,goodRating = 5 )
results <- evaluate ( tops , method = "POPULAR" , type = "topNList" ,n = c ( 1 , 3 , 5 , 10 ) )
#获得混淆矩阵
getConfusionMatrix ( results ) [ [ 1 ] ]
avg ( results )

推荐算法的比较
除了对预测模型进行评价,还可以对不同推荐算法进行比较。可首先构建一个推荐算法列表,通过ROC曲线、、准确率-召回率曲线或RMSE直方图进行比较
TOP-N算法比较
set.seed ( 2016 )
scheme <- evaluationScheme ( key1 , method = "split" , train = 0.9 , k = 1 , given = 10 , goodRating = 5 )
#构建不同算法模型
results <- evaluate ( scheme ,test_data ,n = c ( 1 ,3 ,5 ,10 ,15 ,20 ) )
#模型比较#ROC曲线
plot ( results , annotate = c ( 1 , 3 ) , legend = "bottomright" )
#准确率-召回率曲线
plot ( results , "prec/rec" , annotate = c ( 2 , 3 , 4 ) , legend = "topleft" )
预测评分算法比较
results2 <- evaluate ( scheme , algorithms , type = "ratings" )
plot ( results2 , ylim = c ( 0 , 20 ) )
网友评论