Skip to content

Instantly share code, notes, and snippets.

@LouisdeBruijn
Last active August 28, 2019 12:03
Show Gist options
  • Save LouisdeBruijn/52b673ff3a7a690c795c61a0825ae8e8 to your computer and use it in GitHub Desktop.
Save LouisdeBruijn/52b673ff3a7a690c795c61a0825ae8e8 to your computer and use it in GitHub Desktop.
Build SciPy sparse matrices
def sparse_matrices(df):
'''creates the sparse user-item and item-user matrices'''
# using a scalar value (40) to convert ratings from a scale (1-5) to a like/click/view (1)
alpha = 40
sparse_user_item = csr_matrix( ([alpha]*len(df['movie_id']), (df['user_id'], df['movie_id']) ))
# transposing the item-user matrix to create a user-item matrix
sparse_item_user = sparse_user_item.T.tocsr()
# save the matrices for recalculating user on the fly
save_npz("sparse_user_item.npz", sparse_user_item)
save_npz("sparse_item_user.npz", sparse_item_user)
return sparse_user_item, sparse_item_user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment