Compressed Sparse Matrices¶
The CSR class is the entry point for pure Python code to work with the
CSR package.
-
class
csr.CSR(nrows=None, ncols=None, nnz=None, ptrs=None, inds=None, vals=None, R=None)¶ Simple compressed sparse row matrix. This is like
scipy.sparse.csr_matrix, with a couple of useful differences: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.
It is backed by separate storage type (py:class:csr._CSR) that can be passed around through Numba-compiled functions, and nopython compiled equivalents of many of its methods are available as functions in the
csr.native_opsmodule that take the underlying tuple (accessible byR) as a parameter.If you need to pass an instance off to a Numba-compiled function, use
R:_some_numba_fun(csr.R)
-
rowptrs¶ the row pointers.
- Type
-
colinds¶ the column indices.
- Type
-
values¶ the values
- Type
Constructing Matrices¶
In addition to the CSR constructor, there are several utility methods for constructing sparse matrices.
-
classmethod
CSR.from_coo(rows, cols, vals, shape=None, rpdtype=<class 'numpy.int32'>)¶ 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
Noneto infer from row & column indices.
Accessing Rows¶
The CSR data itself is exposed through attributes. There are also several methods to extract row data in a more convenient form.
-
CSR.row_extent(row)¶ Get the extent of a row in the underlying column index and value arrays.
-
CSR.row_cs(row)¶ Get the column indcies for the stored values of a row.
-
CSR.row_vs(row)¶ Get the stored values of a row. If only the matrix structure is stored, this returns a vector of 1s.
Transforming and Manipulating Matrices¶
-
CSR.copy(include_values=True, *, copy_structure=True)¶ Create a copy of this CSR.
-
CSR.subset_rows(begin, end)¶ Subset the rows in this matrix.
-
CSR.filter_nnzs(filt)¶ Filter the values along the full NNZ axis.
-
CSR.transpose(values=True)¶ Transpose a CSR matrix.
-
CSR.normalize_rows(normalization)¶ Normalize the rows of the matrix.
Note
The normalization ignores missing values instead of treating them as 0.
- Parameters
normalization (str) –
The normalization to perform. Can be one of:
'center'- center rows about the mean'unit'- convert rows to a unit vector
- Returns
The normalization values for each row.
- Return type
-
CSR.drop_values()¶ Remove the value array from this CSR. This is an in-place operation.
-
CSR.fill_values(value)¶ Fill the values of this CSR with the specified value. If the CSR is structure-only, a value array is added. This is an in-place operation.
Arithmetic¶
CSRs do not yet support the full suite of SciPy/NumPy matrix operations, but they do support multiplications:
-
CSR.mult_vec(v)¶ Multiply this matrix by a vector.
- Parameters
other (numpy.ndarray) – A vector, of length ncols.
- Returns
\(A\vec{x}\), as a vector.
- Return type
SciPy Integration¶
CSR matrices can be converted to and from SciPy sparse matrices (in any layout):
-
classmethod
CSR.from_scipy(mat, copy=True)¶ Convert a scipy sparse matrix to a 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
-
CSR.to_scipy()¶ Convert a CSR matrix to a SciPy
scipy.sparse.csr_matrix. Avoids copying if possible.- Parameters
self (CSR) – A CSR matrix.
- Returns
A SciPy sparse matrix with the same data.
- Return type