for i in xrange(0, len(u2items)): wi = math.pow(len(u2items[i]) + 5, -0.35) for j in xrange(i + 1, len(u2items)): intersection = u2items[i] & u2items[j] wj = wi * math.pow(len(u2items[j]) + 5, -0.35) for product_id in intersection: i2i[product_id] = i2i.get(product_id, 0.0) + wj / (1 + len(intersection)) # u2items = array of users and their items # u2items[i] = items user i clicked on # u2items[j] = items user j clicked on # intersection = items both user i and user j clicked on # wj = product-pair score # i2i is incrementally updated as we loop through users (we won't use a loop in production)