KarypisLab / SLIM

High-performance implementation of SLIM-based approaches for Top-N recommendation
112 stars 17 forks source link

export similarity matrix as scipy sparse matrix #7

Open zheng-da opened 4 years ago

zheng-da commented 4 years ago

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 commented 4 years ago

Hi,

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

Thank you, Zeren Shui

shuix007 commented 4 years ago

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