Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export similarity matrix as scipy sparse matrix #7

Open
zheng-da opened this issue Nov 1, 2019 · 2 comments
Open

export similarity matrix as scipy sparse matrix #7

zheng-da opened this issue Nov 1, 2019 · 2 comments

Comments

@zheng-da
Copy link

zheng-da commented Nov 1, 2019

To integrate SLIM with other models (for example, use SLIM to construct the item similarity graph and run GCN on it to compute item embeddings for item-based recommendation), it'll be nice that SLIM exports the similarity matirx as a scipy sparse matrix directly.

Currently, SLIM exports the similarity matrix to a file first before being loaded as a scipy matrix. Here is an example code we have to do. This is slow and inconvenient.

from SLIM import SLIM, SLIMatrix
model = SLIM()
params = {'algo': 'cd', 'nthreads': 2, 'l1r': 1.0, 'l2r': 1.0}
trainmat = SLIMatrix(user_movie_spm.tocsr())
model.train(params, trainmat)

model.save_model(modelfname='slim_model.csr', mapfname='slim_map.csr')
def read_csr(filename):
    f = open(filename, 'r')
    all_rows = []
    all_cols = []
    all_vals = []
    for i, line in enumerate(f.readlines()):
        strs = line.split(' ')
        cols = [int(s) for s in strs[1::2]]
        vals = [float(s) for s in strs[2::2]]
        all_cols.extend(cols)
        all_vals.extend(vals)
        all_rows.extend([i for _ in cols])
    all_rows = np.array(all_rows, dtype=np.int64)
    all_cols = np.array(all_cols, dtype=np.int64)
    all_vals = np.array(all_vals, dtype=np.float32)
    mat = spsp.coo_matrix((all_vals, (all_rows, all_cols)))
    return mat

movie_spm = read_csr('slim_model.csr')
@shuix007
Copy link
Contributor

shuix007 commented Nov 2, 2019

Hi,

Thanks for your feedback. This function will come in the next update of the package.

Thank you,
Zeren Shui

@shuix007
Copy link
Contributor

shuix007 commented Nov 4, 2019

Hi,

The newer version of the package is available. As we also need to update the documents, it will take some time for us to merge it into the master branch. You can now access the newer version of the package from branch version2.0.1.

You can export a SLIM model to a scipy csr matrix by running,
movie_spm = model.to_csr()
or
movie_spm, movie_map = model.to_csr(returnmap=True)
if you need the item map.

Thank you,
Zeren Shui

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants