Skip to content

util

to_csr_matrix(X, binary=False)

Convert a matrix-like object to a scipy csr_matrix.

:param X: Matrix-like object or tuple of objects to convert. :type X: csr_matrix :param binary: If true, ensure matrix is binary by setting non-zero values to 1. :type binary: bool, optional :return: Matrices as csr_matrix. :rtype: Union[csr_matrix, Tuple[csr_matrix, ...]]

Source code in src/recnexteval/matrix/util.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def to_csr_matrix(X: InteractionMatrix | csr_matrix, binary: bool = False) -> csr_matrix:
    """Convert a matrix-like object to a scipy csr_matrix.

    :param X: Matrix-like object or tuple of objects to convert.
    :type X: csr_matrix
    :param binary: If true, ensure matrix is binary by setting non-zero values to 1.
    :type binary: bool, optional
    :return: Matrices as csr_matrix.
    :rtype: Union[csr_matrix, Tuple[csr_matrix, ...]]
    """

    if isinstance(X, csr_matrix):
        res = X
    elif isinstance(X, InteractionMatrix):
        res = X.values
    else:
        raise AttributeError("Not supported Matrix conversion")
    return to_binary(res) if binary else res