SLIM icon indicating copy to clipboard operation
SLIM copied to clipboard

export similarity matrix as scipy sparse matrix

Open zheng-da opened this issue 6 years ago • 2 comments

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')

zheng-da avatar Nov 01 '19 23:11 zheng-da

Hi,

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

Thank you, Zeren Shui

shuix007 avatar Nov 02 '19 20:11 shuix007

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

shuix007 avatar Nov 04 '19 19:11 shuix007