GroupLens Research has collected and made available rating data sets from the MovieLens web site (http://movielens.org). The data sets were collected over various periods of time. The selected dataset has ~100K movie ratings (1-5) from ~600 users on ~9000 movies.
Users were selected at random for inclusion. All selected users had rated at least 20 movies. No demographic information is included. Each user is represented by an id, and no other information is provided.
The data are contained in the files links.csv
, movies.csv
, ratings.csv
and tags.csv
.
# Loading datasets, Package Installation
movies <- read.csv('https://raw.githubusercontent.com/humbertohpgit/MSDS3rdSem_DATA612/master/movies.csv')
ratings <- read.csv('https://raw.githubusercontent.com/humbertohpgit/MSDS3rdSem_DATA612/master/ratings.csv')
#install.packages("tidyverse")
library(dplyr)
library(tidyr)
# Movie Feature Matrix
# Build a matrix with genres in the columns and 0's or 1's indicating the presence of a genre in each movie
# Genres split
genres <- as.data.frame(movies$genres, stringsAsFactors=FALSE)
library(data.table)
genres_tmp <- as.data.frame(tstrsplit(genres[,1], '[|]', type.convert=TRUE), stringsAsFactors=FALSE)
colnames(genres_tmp) <- c(1:10)
genre_list <- c("Action", "Adventure", "Animation", "Children", "Comedy", "Crime","Documentary", "Drama", "Fantasy","Film-Noir", "Horror", "Musical", "Mystery","Romance","Sci-Fi", "Thriller", "War", "Western") # genres list from the dataset readme
genre_mat <- matrix(0,nrow(genres_tmp),length(genre_list)) #empty matrix
genre_mat[1,] <- genre_list #set first row to genre list
colnames(genre_mat) <- genre_list #set column names to genre list
#iterate through matrix to assign 0's or 1's accordingly
for (i in 1:nrow(genres_tmp)) {
for (c in 1:ncol(genres_tmp)) {
genmat_col = which(genre_mat[1,] == genres_tmp[i,c])
if (i != nrow(genres_tmp)){
genre_mat[i+1,genmat_col] <- 1
}
}
}
genre_mat <- as.data.frame(genre_mat[-1,], stringsAsFactors=FALSE) #remove first row, which was the genre list
for (c in 1:ncol(genre_mat)) {
genre_mat[,c] <- as.numeric(genre_mat[,c])
}
head(genre_mat,5)
## Action Adventure Animation Children Comedy Crime Documentary Drama
## 1 0 1 1 1 1 0 0 0
## 2 0 1 0 1 0 0 0 0
## 3 0 0 0 0 1 0 0 0
## 4 0 0 0 0 1 0 0 1
## 5 0 0 0 0 1 0 0 0
## Fantasy Film-Noir Horror Musical Mystery Romance Sci-Fi Thriller War
## 1 1 0 0 0 0 0 0 0 0
## 2 1 0 0 0 0 0 0 0 0
## 3 0 0 0 0 0 1 0 0 0
## 4 0 0 0 0 0 1 0 0 0
## 5 0 0 0 0 0 0 0 0 0
## Western
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
# User Profile Matrix
# Build a matrix with userd in the columns, movies as rows and the ratings set to 0 when there are NAs
#Simplify ratings values to a binary scale
bin_ratings <- ratings
for (i in 1:nrow(bin_ratings)){
if (bin_ratings[i,3] > 3){
bin_ratings[i,3] <- 1
}
else{
bin_ratings[i,3] <- -1
}
}
user_mat <- spread(bin_ratings[,1:3], userId, rating)
for (i in 1:ncol(user_mat)){
user_mat[which(is.na(user_mat[,i]) == TRUE),i] <- 0
}
user_mat = user_mat[,-1]
head(user_mat,1)
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
## 1 1 0 0 0 1 0 1 0 0 0 0 0 0 0 -1 0 1 1 1 0 1 0 0 0 0 0 -1
## 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
## 1 0 0 0 1 -1 -1 0 0 0 0 0 0 1 0 0 1 -1 1 1 0 0 0 -1 0
## 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
## 1 0 0 -1 0 0 1 0 0 0 0 0 1 1 0 1 0 -1 0 0 1 0 1 0 0
## 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
## 1 -1 0 1 0 0 0 -1 0 0 0 1 0 0 -1 -1 1 0 -1 0 0 1 0 1 0
## 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
## 1 0 0 0 1 0 0 0 1 0 0 0 0 -1 0 0 0 0 0
## 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
## 1 0 1 0 1 0 0 1 0 0 0 0 0 -1 0 -1 0 -1 1
## 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
## 1 0 1 0 0 -1 1 0 0 1 1 0 0 0 0 0 1 0 -1
## 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
## 1 0 -1 1 0 0 1 1 1 0 0 0 0 1 1 0 1 0 1
## 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
## 1 0 0 0 0 0 1 1 1 0 0 1 0 0 1 1 0 0 0
## 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
## 1 0 1 0 -1 0 0 0 0 0 0 1 1 1 0 0 0 1 0
## 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
## 1 0 0 0 0 0 1 -1 0 -1 1 0 1 1 0 0 1 0 0
## 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
## 1 1 0 0 1 0 0 1 -1 1 0 0 0 0 1 1 0 0 0
## 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
## 1 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0
## 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
## 1 0 1 1 0 -1 0 0 1 1 0 0 1 1 1 1 1 0 -1
## 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
## 1 1 0 1 -1 0 0 0 0 1 0 1 1 1 -1 0 0 0 0
## 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
## 1 -1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 -1 0
## 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
## 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0
## 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
## 1 1 0 1 1 0 1 0 1 0 0 0 0 0 1 0 0 1 0
## 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
## 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0
## 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
## 1 0 0 -1 -1 0 0 0 0 1 0 1 1 1 0 0 1 0 0
## 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
## 1 0 1 0 -1 0 0 0 0 1 0 0 1 0 1 0 0 0 0
## 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
## 1 0 0 0 0 0 1 -1 0 1 0 0 0 0 0 1 0 1 0
## 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
## 1 0 0 0 0 0 0 0 0 -1 0 1 0 1 0 1 0 0 0
## 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
## 1 0 1 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0
## 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
## 1 1 0 -1 0 0 0 0 0 1 1 1 1 0 0 1 0 1 1
## 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
## 1 0 0 -1 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0
## 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
## 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0
## 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
## 1 1 0 0 1 0 0 0 0 -1 0 1 1 0 0 -1 -1 0 0
## 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
## 1 0 1 1 0 0 0 0 0 0 -1 0 0 -1 0 0 0 0 0
## 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
## 1 1 0 0 0 0 1 0 0 0 1 -1 1 1 0 0 0 0 1
## 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
## 1 0 0 1 0 1 1 0 0 0 0 0 1 -1 0 0 0 1 0
## 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
## 1 0 1 0 0 1 0 0 0 0 0 1 1 0 -1 -1 1 0 1
## 604 605 606 607 608 609 610
## 1 -1 1 -1 1 -1 -1 1
# Matrices Dot Product - User preference towards movie genres
dim(genre_mat)
## [1] 9741 18
dim(user_mat)
## [1] 9724 610
#Remove movies that have no rating in the dataset
movieIds <- unique(movies$movieId) #9741
ratingmovieIds <- unique(ratings$movieId) #9724
genre_mat_final <- genre_mat[-which((movieIds %in% ratingmovieIds) == FALSE),]
rownames(genre_mat_final) <- NULL
genre_mat_final <- as.data.frame(genre_mat_final, stringsAsFactors=FALSE)
for (c in 1:ncol(genre_mat_final)) {
genre_mat_final[,c] <- as.integer(genre_mat_final[,c])
}
user_mat_final <- user_mat[-9724,]
dim(genre_mat_final)
## [1] 9723 18
dim(user_mat_final)
## [1] 9723 610
dot_prod <- matrix(0,18,610)
for (c in 1:ncol(user_mat_final)){
for (i in 1:ncol(genre_mat_final)){
dot_prod[i,c] <- sum((genre_mat_final[,i]) * (user_mat_final[,c]))
## genre_mat_final[,i] %*% user_mat_final[,c]
}
}
dot_prod[,1]
## [1] 62 63 25 32 57 33 0 60 35 1 1 18 8 22 24 31 18 5
#Simplify to a Binary scale
for (c in 1:ncol(dot_prod)){
for(i in 1:nrow(dot_prod)){
if (dot_prod [i,c] > 0){
dot_prod [i,c] <- 1
}
else {
dot_prod [i,c] <- 0
}
}
}
# Similarity Calculation between user profiles and movie genres
# Calculation for 3 users (optimizing processing time)
recomm_movies_5users <- list()
for (i in 1:5){
result_user <- dot_prod[,i] # Specific user's profile
sim_mat <- rbind.data.frame(result_user, genre_mat_final)
sim_mat <- data.frame(lapply(sim_mat,function(x){as.integer(x)}))
#Calculate Jaccard distance between user profile and all movies
library(proxy)
sim_results <- dist(sim_mat, method = "Jaccard")
sim_results <- as.data.frame(as.matrix(sim_results[1:9723]))
rows <- which(sim_results == min(sim_results))
#Top Recommended movies for specific (5) users
recomm_movies_5users[[i]] <- list(movies[rows,2])
}
# Top Recommended for Users (1-5)
recomm_movies_5users
## [[1]]
## [[1]][[1]]
## [1] It's Kind of a Funny Story (2010)
## 9737 Levels: '71 (2014) ... Zulu (2013)
##
##
## [[2]]
## [[2]][[1]]
## [1] It's Kind of a Funny Story (2010)
## 9737 Levels: '71 (2014) ... Zulu (2013)
##
##
## [[3]]
## [[3]][[1]]
## [1] Hands Off the Loot (Touchez pas au grisbi) (1954)
## 9737 Levels: '71 (2014) ... Zulu (2013)
##
##
## [[4]]
## [[4]][[1]]
## [1] It's Kind of a Funny Story (2010)
## 9737 Levels: '71 (2014) ... Zulu (2013)
##
##
## [[5]]
## [[5]][[1]]
## [1] Snow White and the Seven Dwarfs (1937)
## [2] Sleeping Beauty (1959)
## [3] 10th Kingdom, The (2000)
## [4] Blades of Glory (2007)
## 9737 Levels: '71 (2014) ... Zulu (2013)
# Build a user matrix with movies as columns
rating_mat <- spread(ratings[,1:3], movieId, rating)
rating_mat <- as.matrix(rating_mat[,-1]) #remove userIds
library(recommenderlab)
#Convert into a recommenderlab sparse matrix
rating_mat <- as(rating_mat, "realRatingMatrix")
#Normalize data
rating_mat_norm <- normalize(rating_mat)
#Create Recommender Model. "UBCF" (User-Based Collaborative Filtering)
recommender_model <- Recommender(rating_mat_norm, method = "UBCF", param=list(method="Cosine",nn=30))
#Obtain top 10 recommendations for users (1-5)
recom <- predict(recommender_model, rating_mat[1:5], n=10)
recom_list <- as(recom, "list") #convert recommenderlab object to readable list
recom_result <- list()
for (i in c(1:5)){
recom_result[[i]] <- movies[as.integer(recom_list[[i]]),2]
}
# Top 10 recommendations for users (1-5)
recom_result
## [[1]]
## [1] <NA>
## [2] Da geht noch was! (2013)
## [3] I Like It Like That (1994)
## [4] <NA>
## [5] Money Talks (1997)
## [6] Welcome to Collinwood (2002)
## [7] Children of a Lesser God (1986)
## [8] <NA>
## [9] All the Right Moves (1983)
## [10] Town is Quiet, The (Ville est tranquille, La) (2000)
## 9737 Levels: '71 (2014) ... Zulu (2013)
##
## [[2]]
## [1] Bread and Chocolate (Pane e cioccolata) (1973)
## [2] Great White Hype, The (1996)
## [3] Rock, The (1996)
## [4] Lord of Illusions (1995)
## [5] Big Green, The (1995)
## [6] NeverEnding Story III, The (1994)
## [7] Kid in King Arthur's Court, A (1995)
## [8] What's Eating Gilbert Grape (1993)
## [9] In the Company of Men (1997)
## [10] Priest (1994)
## 9737 Levels: '71 (2014) ... Zulu (2013)
##
## [[3]]
## [1] I Like It Like That (1994)
## [2] Rock, The (1996)
## [3] Last Dance (1996)
## [4] Picture Perfect (1997)
## [5] Wonderland (1999)
## [6] In the Company of Men (1997)
## [7] Angus, Thongs and Perfect Snogging (2008)
## [8] Mimic (1997)
## [9] Big Green, The (1995)
## [10] Rainmaker, The (1997)
## 9737 Levels: '71 (2014) ... Zulu (2013)
##
## [[4]]
## [1] <NA>
## [2] I Like It Like That (1994)
## [3] <NA>
## [4] Aristocrats, The (2005)
## [5] <NA>
## [6] Vanya on 42nd Street (1994)
## [7] <NA>
## [8] <NA>
## [9] Lilies of the Field (1963)
## [10] Everything You Always Wanted to Know About Sex * But Were Afraid to Ask (1972)
## 9737 Levels: '71 (2014) ... Zulu (2013)
##
## [[5]]
## [1] Teenage Mutant Ninja Turtles (1990)
## [2] In the Company of Men (1997)
## [3] Hart's War (2002)
## [4] Fog, The (1980)
## [5] Indian Summer (a.k.a. Alive & Kicking) (1996)
## [6] Tournament, The (2009)
## [7] Welcome to Collinwood (2002)
## [8] <NA>
## [9] Mouse Hunt (1997)
## [10] Good, the Bad and the Ugly, The (Buono, il brutto, il cattivo, Il) (1966)
## 9737 Levels: '71 (2014) ... Zulu (2013)
Collaborative Filtering: correlating personal preferences
Content Based Filtering: understanding user / item profiles