热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

R开发:协调过滤推荐

set.seed(1234)#加载数据包library(“recommenderlab”)#构造数据运用recommenderlab包实现协同过滤推荐,其数据类型采用S4类构造,需

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 ( &#8220;u&#8221; ,1 : 5 ,sep = &#8221; ) ,item = paste ( &#8220;i&#8221; ,1 : 10 ,sep = &#8221; ) ) )

val2 <- as ( val1, &#8220;realRatingMatrix&#8221; ) 

《R开发:协调过滤推荐》

数据转换

val3<- normalize ( val2 )

#二元分类转换,normalize()函数进行标准化处理,标准化的目的是为了去除用户评分的偏差

val4 <- binarize ( val3 , minRating = 4 )

val5 <- as ( val4 , &#8220;matrix&#8221; )

《R开发:协调过滤推荐》

数据可视化

接下来,我们采用MovieLense数据集,

data ( MovieLense )

key1 <- sample ( MovieLense , 943 , replace = F )

image ( MovieLense )

《R开发:协调过滤推荐》

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

《R开发:协调过滤推荐》

hist ( rowCounts ( key1 ) , breaks = 50 )

《R开发:协调过滤推荐》

建立模型

对于realRatingMatrix有六种方法:IBCF(基于物品的推荐)、UBCF(基于用户的推荐)、PCA(主成分分析)、RANDOM(随机推荐)、SVD(矩阵因子化)、POPULAR(基于流行度的推荐)

建立协同过滤推荐算法模型,主要运用recommender(data=ratingMatrix,method,parameter=NULL)函数,getModel()可查看模型参数

key1_recom <- Recommender (key1 , method = &#8220;IBCF&#8221; )

key1_popul <- Recommender ( key1, method = &#8220;POPULAR&#8221; )

#查看模型方法

names ( getModel ( key1_recom ) )


《R开发:协调过滤推荐》

模型预测

TOP-N预测

对模型预测可运用predict()函数,在此分别以TOP-N预测及评分预测为例,预测第940-943位观影者的评分情况。n表示最终为TOP-N的列表推荐,参数type = &#8220;ratings&#8221;表示运用评分预测观影者对电影评分,模型结果均需转为list或矩阵表示

pred <- predict ( key1_popul ,key1 [ 940 : 943,] , n = 5 )

as ( pred , &#8220;list&#8221; )

《R开发:协调过滤推荐》

#top-N为有序列表,抽取最优推荐子集

pred3 <- bestN ( pred , n = 3 )

as ( pred3 , &#8220;list&#8221; )

《R开发:协调过滤推荐》

#评分预测

rate <- predict ( key1_popul , key1 [ 940 : 943 ] , type = &#8220;ratings&#8221; )

as ( rate , &#8220;matrix&#8221; ) [ , 1 : 5 ]


《R开发:协调过滤推荐》

预测模型评价

评分预测模型评价

eva <- evaluationScheme (key1 [ 1 : 800 ] , method = &#8220;split&#8221; , train = 0.9,given = 15)

method=&#8221;split&#8221;&train=0.9为按90%划分训练测试集合,given为评价的类目数

r_eva1<- Recommender ( getData ( eva , &#8220;train&#8221; ) , &#8220;UBCF&#8221; )

p_eva1<- predict ( r_eva1 , getData ( eva, &#8220;known&#8221; ) , type = &#8220;ratings&#8221; )

r_eva2 <- Recommender ( getData ( eva, &#8220;train&#8221; ) , &#8220;IBCF&#8221; )

p_eva2 <- predict ( r_eva2 , getData ( eva, &#8220;known&#8221; ) , type = &#8220;ratings&#8221; )

c_eva1 <- calcPredictionAccuracy ( p_eva1 , getData ( eva , &#8220;unknown&#8221; ) )

c_eva2 <- calcPredictionAccuracy ( p_eva2 , getData ( eva , &#8220;unknown&#8221; ) )

error <- rbind ( c_eva1 , c_eva2 ) 

rownames ( error ) <- c ( &#8220;UBCF&#8221; , &#8220;IBCF&#8221; )

计算预测模型的准确度

《R开发:协调过滤推荐》

TOP-N预测模型评价

通过4-fold交叉验证方法分割数据集,运用evaluate()进行TOP-N预测模型评价,评价结果可通过ROC曲线及准确率-召回率曲线展示:

#4-fold交叉验证

tops <- evaluationScheme ( key1 [ 1 : 800 ] , method = &#8220;cross&#8221; , k = 4 , given = 3 ,goodRating = 5 )

results <- evaluate ( tops , method = &#8220;POPULAR&#8221; , type = &#8220;topNList&#8221; ,n = c ( 1 , 3 , 5 , 10 ) )

#获得混淆矩阵

getConfusionMatrix ( results ) [ [ 1 ] ]

avg ( results )

《R开发:协调过滤推荐》

推荐算法的比较

除了对预测模型进行评价,还可以对不同推荐算法进行比较。可首先构建一个推荐算法列表,通过ROC曲线、、准确率-召回率曲线或RMSE直方图进行比较

TOP-N算法比较

set.seed ( 2016 )

scheme <- evaluationScheme ( key1 , method = &#8220;split&#8221; , 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 = &#8220;bottomright&#8221; )

#准确率-召回率曲线

plot ( results , &#8220;prec/rec&#8221; , annotate = c ( 2 , 3 , 4 ) , legend = &#8220;topleft&#8221; )

预测评分算法比较

results2 <- evaluate ( scheme , algorithms , type = &#8220;ratings&#8221; )

plot ( results2 , ylim = c ( 0 , 20 ) )


推荐阅读
author-avatar
mobiledu2502927067
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有