-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperfect-map-train.R
27 lines (24 loc) · 939 Bytes
/
perfect-map-train.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
# Load required library
library(recommenderlab)
source("custom-functions.R")
#Preparing training and test data
trainCsv = ReadCsvData("train")
# Filtering out test ratings
testUserIds = read.csv("data/test.csv")
nonTestRatings <- trainCsv[! trainCsv$UserId %in% testUserIds$UserId,]
# Coerce to the class used by recommenderlab
urm <- as(nonTestRatings, "realRatingMatrix")
# Binarize over 4 to leave relevant items only
urmBin <- binarize(urm, 4)
# Count how many relevant items per user
relevantCountPerUser <- rowCounts(urmBin)
# number of recommendation / N for each user
relevantInverse <- 5 / relevantCountPerUser
# Allocate index and vector replacement
noRelevantItems <- relevantCountPerUser == 0
zeroVector <- rep(0,length(relevantCountPerUser))
# Replace infinite entries for 0
relevantInverse[noRelevantItems] <- zeroVector[noRelevantItems]
# Compute mean average
perfectMapOnTrain <- mean(relevantInverse)
perfectMapOnTrain