Skip to content

Instantly share code, notes, and snippets.

@eugeneyan
Created April 26, 2021 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugeneyan/6a58c7ce58ef1a69eb8edfc083644da6 to your computer and use it in GitHub Desktop.
Save eugeneyan/6a58c7ce58ef1a69eb8edfc083644da6 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment