Skip to content

utils

EPSILON = 1e-13 module-attribute

logger = logging.getLogger(__name__) module-attribute

invert(x)

Invert an array.

:param x: [description] :type x: [type] :return: [description] :rtype: [type]

Source code in src/streamsight/algorithms/time_aware_item_knn/utils.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def invert(x: np.ndarray | csr_matrix) -> np.ndarray | csr_matrix:
    """Invert an array.

    :param x: [description]
    :type x: [type]
    :return: [description]
    :rtype: [type]
    """
    if isinstance(x, np.ndarray):
        ret = np.zeros(x.shape)
    elif isinstance(x, csr_matrix):
        ret = csr_matrix(x.shape)
    else:
        raise TypeError("Unsupported type for argument x.")
    ret[x.nonzero()] = 1 / x[x.nonzero()]
    return ret

to_binary(X)

Converts a matrix to binary by setting all non-zero values to 1.

:param X: Matrix to convert to binary. :type X: csr_matrix :return: Binary matrix. :rtype: csr_matrix

Source code in src/streamsight/algorithms/time_aware_item_knn/utils.py
37
38
39
40
41
42
43
44
45
46
47
def to_binary(X: csr_matrix) -> csr_matrix:
    """Converts a matrix to binary by setting all non-zero values to 1.

    :param X: Matrix to convert to binary.
    :type X: csr_matrix
    :return: Binary matrix.
    :rtype: csr_matrix
    """
    X_binary = X.astype(bool).astype(X.dtype)

    return X_binary