Skip to content

Instantly share code, notes, and snippets.

@eugeneyan
Created April 26, 2021 02:15
Show Gist options
  • Save eugeneyan/6a58c7ce58ef1a69eb8edfc083644da6 to your computer and use it in GitHub Desktop.
Save eugeneyan/6a58c7ce58ef1a69eb8edfc083644da6 to your computer and use it in GitHub Desktop.

Revisions

  1. eugeneyan created this gist Apr 26, 2021.
    14 changes: 14 additions & 0 deletions swing.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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)