Database Reference
In-Depth Information
puted by first calculating the dot product between the vectors and then dividing the result
by a denominator, which is the norm (or length) of each vector multiplied together (spe-
cifically, the L2-norm is used in cosine similarity). In this way, cosine similarity is a nor-
malized dot product.
The cosine similarity measure takes on values between -1 and 1. A value of 1 implies
completely similar, while a value of 0 implies independence (that is, no similarity). This
measure is useful because it also captures negative similarity, that is, a value of -1 implies
that not only are the vectors not similar, but they are also completely dissimilar.
Let's create our cosineSimilarity function here:
def cosineSimilarity(vec1: DoubleMatrix, vec2:
DoubleMatrix): Double = {
vec1.dot(vec2) / (vec1.norm2() * vec2.norm2())
}
Tip
Note that we defined a return type for this function of Double . We are not required to do
this, since Scala features type inference. However, it can often be useful to document re-
turn types for Scala functions.
Let's try it out on one of our item factors for item 567 . We will need to collect an item
factor from our model; we will do this using the lookup method in a similar way that we
did earlier to collect the ratings for a specific user. In the following lines of code, we also
use the head function, since lookup returns an array of values, and we only need the
first value (in fact, there will only be one value, which is the factor vector for this item).
Since this will be an Array[Double] , we will then need to create a DoubleMatrix
object from it and compute the cosine similarity with itself:
val itemId = 567
val itemFactor = model.productFeatures.lookup(itemId).head
val itemVector = new DoubleMatrix(itemFactor)
cosineSimilarity(itemVector, itemVector)
A similarity metric should measure how close, in some sense, two vectors are to each oth-
er. Here, we can see that our cosine similarity metric tells us that this item vector is
identical to itself, which is what we would expect:
Search WWH ::




Custom Search