concept centroid in category recommender system

appears as: centroids, centroid, centroids
Practical Recommender Systems

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

The k-means clustering algorithm works by finding k points, called centroids, which satisfy the theorem that the sum of the distances between all items and their assigned centroids are as small as possible. As I write this I’m watching a sprinkler watering the flowers. Why? It takes much consideration about where to place the sprinkler because water in Denmark is precious, so I need to find k spots where it’ll water everything but use as little water as possible. The k-means clustering can be used for that too because it produces centroids that would show the best place to put the sprinkler.

For each data point in the data set, find the centroid with the s- shortest distance

The next thing to do is to calculate the distance between the items in the data and the centroids and find which is closer. The distance algorithm you’ll use here is a well-known fellow: the Euclidian norm as described earlier. The formula is the following:

Listing 7.4. Moving centroids to the center of the cluster
from functools import reduce

def move_centroids(k, kim):

  centroids = []
  for cen in range(k):                                                    #1
    members = [i[0] for i in kim if i[1] == cen]                          #2
    if members:                                                           #3
        centroid = [i/len(members) for i in reduce(add_vector, members)]
        centroids.append(centroid)                                        #4

  return centroids