Matrix Utilities¶
We have some matrix-related utilities, since matrices are used so heavily in recommendation algorithms.
Building Ratings Matrices¶
-
lenskit.matrix.
sparse_ratings
(ratings, scipy=False)¶ Convert a rating table to a sparse matrix of ratings.
- Parameters
ratings (pandas.DataFrame) – a data table of (user, item, rating) triples.
scipy – if
True
, return a SciPy matrix instead ofCSR
.
- Returns
a named tuple containing the sparse matrix, user index, and item index.
- Return type
Compressed Sparse Row Matrices¶
We use CSR-format sparse matrices in quite a few places. Since SciPy’s sparse matrices are not directly usable from Numba, we have implemented a Numba-compiled CSR representation that can be used from accelerated algorithm implementations.
-
class
lenskit.matrix.
CSR
(nrows=None, ncols=None, nnz=None, ptrs=None, inds=None, vals=None, N=None)¶ Simple compressed sparse row matrix. This is like
scipy.sparse.csr_matrix
, with a couple of useful differences:It is backed by a Numba jitclass, so it can be directly used from Numba-optimized functions.
The value array is optional, for cases in which only the matrix structure is required.
The value array, if present, is always double-precision.
You generally don’t want to create this class yourself with the constructor. Instead, use one of its class methods.
If you need to pass an instance off to a Numba-compiled function, use
N
:_some_numba_fun(csr.N)
We use the indirection between this and the Numba jitclass so that the main CSR implementation can be pickled, and so that we can have class and instance methods that are not compatible with jitclass but which are useful from interpreted code.
-
rowptrs
¶ the row pointers.
- Type
-
colinds
¶ the column indices.
- Type
-
values
¶ the values
- Type
-
classmethod
from_coo
(rows, cols, vals, shape=None)¶ Create a CSR matrix from data in COO format.
- Parameters
rows (array-like) – the row indices.
cols (array-like) – the column indices.
vals (array-like) – the data values; can be
None
.shape (tuple) – the array shape, or
None
to infer from row & column indices.
-
classmethod
from_scipy
(mat, copy=True)¶ Convert a scipy sparse matrix to an internal CSR.
- Parameters
mat (scipy.sparse.spmatrix) – a SciPy sparse matrix.
copy (bool) – if
False
, reuse the SciPy storage if possible.
- Returns
a CSR matrix.
- Return type
-
row
(row)¶ Return a row of this matrix as a dense ndarray.
- Parameters
row (int) – the row index.
- Returns
the row, with 0s in the place of missing values.
- Return type
-
row_cs
(row)¶ Get the column indcies for the stored values of a row.
-
row_extent
(row)¶ Get the extent of a row in the underlying column index and value arrays.
-
row_nnzs
()¶ Get a vector of the number of nonzero entries in each row.
Note
This method is not available from Numba.
- Returns
the number of nonzero entries in each row.
- Return type
-
row_vs
(row)¶ Get the stored values of a row.
-
rowinds
() → numpy.ndarray¶ Get the row indices from this array. Combined with
colinds
andvalues
, this can form a COO-format sparse matrix.Note
This method is not available from Numba.
-
sort_values
()¶ Sort CSR rows in nonincreasing order by value.
Note
This method is not available from Numba.
-
to_scipy
()¶ Convert a CSR matrix to a SciPy
scipy.sparse.csr_matrix
.- Parameters
self (CSR) – A CSR matrix.
- Returns
A SciPy sparse matrix with the same data.
- Return type