Database Reference
In-Depth Information
for ((p, i) <- predK.zipWithIndex) {
if (actual.contains(p)) {
numHits += 1.0
score += numHits / (i.toDouble + 1.0)
}
}
if (actual.isEmpty) {
1.0
} else {
score / scala.math.min(actual.size, k).toDouble
}
}
As you can see, this takes as input a list of
actual
item IDs that are associated with the
user and another list of
predicted
ids so that our estimate will be relevant for the user.
We can compute the APK metric for our example user
789
as follows. First, we will ex-
tract the actual movie IDs for the user:
val actualMovies = moviesForUser.map(_.product)
The output is as follows:
actualMovies: Seq[Int] = ArrayBuffer(1012, 127, 475, 93,
1161, 286, 293, 9, 50, 294, 181, 1, 1008, 508, 284, 1017,
137, 111, 742, 248, 249, 1007, 591, 150, 276, 151, 129,
100, 741, 288, 762, 628, 124)
We will then use the movie recommendations we made previously to compute the APK
score using
K = 10
:
val predictedMovies = topKRecs.map(_.product)
Here is the output:
predictedMovies: Array[Int] = Array(27, 497, 633, 827, 602,
849, 401, 584, 1035, 1014)
The following code will produce the average precision: