concept distance in category recommender system

appears as: distce, distce, distces, distces, The distce, distance
Practical Recommender Systems

This is an excerpt from Manning's book Practical Recommender Systems.

Listing 7.2. Calculating the distance between two vectors
import math

def distance(x,y):

    dist = 0
    for i in range(len(x)):                    #1
        dist += math.pow((x[i] - y[i]), 2)     #2
    return math.sqrt(dist)                     #3

You can use the distance method to decide which cluster each element should be a part of as shown next (listing 7.3). The method returns the centroid with the smallest distance to the item (the most similar one).

Listing 7.3. Assigning a data item to a cluster
def add_to_cluster(item, centroids):
    return item, min(range(len(centroids)),                        #1
                     key= lambda i: distance(item, centroids[i]))

When all points are assigned, the distance method calculates the sum of all the distances between the item and its centroid. The sum is used to compare the iterations of the algorithm. The second iteration compares the sum of the distances in the second setting with the previous one. If the earlier one is better, then the algorithm stops; otherwise, it does another iteration.

With this formula, the distance is 9 - 6 = 3. Because the maximal distance between the two ratings is 9, you can make it a similarity (1 when the distance is minimum and close to 0 when the distance is maximum) with this calculation:

Figure 8.7. Two ways of finding the neighborhood. The left side shows how the threshold neighborhood works. Around the active point, you draw a circle (at least in 2D) and everything that’s inside is a neighbor. On the right is the Top-N approach. It doesn’t look at distance; it grows until it has N neighbors in the neighborhood.