-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecommenderlab-submission-NOTOPPOPULAR.R
53 lines (49 loc) · 2.31 KB
/
recommenderlab-submission-NOTOPPOPULAR.R
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
library(recommenderlab)
library(plyr)
source("modified-packages.R")
source("custom-functions.R")
# SUbmission with association rules
#Preparing training and test data
trainCsv = ReadCsvData("train")
# Coerce to the class used by recommenderlab
urm <- as(trainCsv, "realRatingMatrix")
# Binarize just according to ratings
urm <- binarize(urm, minRating=4)
# Train recommender
recommender <- Recommender(urm, method = "AR", param=list(measure="support", confidence = 0.80, support=0.015, maxlen=3))
popularRecommender <- Recommender(urm, method = "POPULAR")
# Generate recommendations
testCsv <- ReadCsvData("test")
testUserIds <- testCsv$UserId
recommendations <- predict(recommender, testUserIds, data=urm, n=5)
as(recommendations,"list")
# Aggregate per user
recommendedPerUser <- data.frame(UserId = testUserIds)
recommendedPerUser$ItemIds <- as(recommendations,"list")
# Prepare as string
submission <- ddply(recommendedPerUser, "UserId",
function(userRow) {
# Retrieve existing recommendations
itemIds <- GetItemIdsVector(userRow)
numberOfExistingRecommendations <- length(itemIds)
desiredNumberRecommendations <- 5
numberOfMissingRecommendations <- desiredNumberRecommendations - numberOfExistingRecommendations
if(numberOfMissingRecommendations == 0)
{
# Recommendations are complete
recommended <- itemIds
} else {
# Fill missing recommendations with popular
topPopular <- predict(popularRecommender, userRow$UserId,
data=urm, n=5)
# Convert from topNList to vector
topPopular <- as(topPopular, "list")[[1]]
prova <- list(4000,4000,4000,4000,4000)
newRecommendations <- head(prova, numberOfMissingRecommendations)
recommended <- append(itemIds, newRecommendations)
}
RecommendedMovieIds <- paste(recommended, collapse = " ")
data.frame(RecommendedMovieIds)
})
# Write to file
WriteSubmission(submission)