본문 바로가기

데이터 다루기/Base of R

[R] 협업 필터링 (Collaborative filtering)

728x90
반응형

이번 포스팅에서는 협업 필터링 (Collaborative filtering)을 R 환경에서 실습해보겠습니다.

데이터는 영화 평점 데이터입니다.

 

MovieLense.csv
4.44MB

install.packages("recommenderlab")
library(recommenderlab)

저희가 사용할 패키지는 recommenderlab 입니다.

movie <- read.csv('MovieLense.csv', stringsAsFactors = FALSE)

movie_matrix <- as(as(movie, "matrix"), 'realRatingMatrix')

영화 평점 데이터를 불러와주시고, 해당 데이터셋을 협업 필터링을 사용하기 위한 데이터 형태로 바꿔주어야합니다.

realRatingMatrix 형태로 바꾸어줍시다.

## User-Based Collaborative Filtering (UBCF)
rec_UBCF <- Recommender(movie_matrix, method = 'UBCF', param = list(method = 'pearson')) # 유사도 = pearson
who <- 1 # 첫번째 사람
head(as(predict(rec_UBCF, movie_matrix[who, ], type = 'ratings'), 'list')[[1]])

                 Heat..1995.               Sabrina..1995. Sense.and.Sensibility..1995.     Leaving.Las.Vegas..1995. 
                    3.605166                     3.588246                     3.648910                     3.631042 
          Restoration..1995.          Bed.of.Roses..1996. 
                    3.605166                     3.605166 


as(predict(rec_UBCF, movie_matrix[who, ], type = 'topNList', n = 10), 'list')
[[1]]
 [1] "Titanic..1997."                                 "L.A..Confidential..1997."                      
 [3] "Air.Force.One..1997."                           "Seven.Years.in.Tibet..1997."                   
 [5] "English.Patient..The..1996."                    "Postman..The..1997."                           
 [7] "Anna.Karenina..1997."                           "Midnight.in.the.Garden.of.Good.and.Evil..1997."
 [9] "Eve.s.Bayou..1997."                             "Cop.Land..1997."   

첫번째로, 사용자 기반 (User-based) 협업 필터링입니다.

Recommender 함수와 method 인자에 'UBCF'를 입력함으로써, 모델링 가능합니다.

첫 번째 결과는, 유저 1이 아직 평점을 매기지 않은 영화들에 대해서 사용자 기반 협업 필터링 모델에 따른 예측 평점입니다.

그리고 두 번째 결과는, 그 예측 평점중에서 가장 높은 10개의 영화를 추천해주는 것입니다.

이 때, param = list(method = 'pearson') 에서 보시다시피, 사용자간의 유사도를 계산할 때, pearson 유사도가 사용되었습니다.

이를 cosine 유사도로 변경해보겠습니다.

rec_UBCF <- Recommender(movie_matrix, method = 'UBCF', param = list(method = 'cosine')) # 유사도 = cosine
as(predict(rec_UBCF, movie_matrix[who, ], type = 'topNList', n = 10), 'list')

[[1]]
 [1] "Titanic..1997."                              "Air.Force.One..1997."                       
 [3] "English.Patient..The..1996."                 "Game..The..1997."                           
 [5] "Rainmaker..The..1997."                       "Wedding.Singer..The..1998."                 
 [7] "Anna.Karenina..1997."                        "Winter.Guest..The..1997."                   
 [9] "Washington.Square..1997."                    "Mrs..Brown..Her.Majesty..Mrs..Brown...1997."

살짝 다른 결과가 나오는 것을 확인할 수 있습니다.

## Item Based Collaborative Filtering (IBCF)
rec_IBCF <- Recommender(movie_matrix, method = "IBCF", param=list(method = 'pearson')) # 유사도 = pearson
who <- 1
head(as(predict(rec_IBCF, movie_matrix[who,], type = 'ratings'), 'list')[[1]])


                Bent..1997.               Vermin..1998. Lay.of.the.Land..The..1997.  Celestial.Clockwork..1994. 
                   3.552268                    4.384615                    4.142857                    4.100000 
        Commandments..1997.     Truman.Show..The..1998. 
                   3.666667                    3.504645 

as(predict(rec_IBCF, movie_matrix[who,], type = 'topNList', n=10), 'list')


[[1]]
 [1] "Salut.cousin...1996."                                                        
 [2] "To.Have..or.Not..1995."                                                      
 [3] "Little.City..1998."                                                          
 [4] "Rough.Magic..1995."                                                          
 [5] "Stranger..The..1994."                                                        
 [6] "Ballad.of.Narayama..The..Narayama.Bushiko...1958."                           
 [7] "Convent..The..Convento..O...1995."                                           
 [8] "My.Life.and.Times.With.Antonin.Artaud..En.compagnie.d.Antonin.Artaud...1993."
 [9] "Santa.with.Muscles..1996."                                                   
[10] "The.Deadly.Cure..1996."   

아이템 기반 (Item-based) 협업 필터링은, method 인자에 'IBCF'를 입력해주시면 됩니다.

결과 또한 유저 기반 협업 필터링과는 사뭇 다르게 관찰됩니다.

반응형

'데이터 다루기 > Base of R' 카테고리의 다른 글

[R] 신경망 (Neural network)  (0) 2020.05.07
[R] 연관성 분석 (Association rule)  (0) 2020.04.19
[R] Decision Tree (의사결정나무)  (0) 2020.04.09
[R] Logistic Regression  (0) 2020.04.07
[R] K-nearest neighbor (KNN) method  (0) 2020.04.07