amzn / pecos

PECOS - Prediction for Enormous and Correlated Spaces
https://libpecos.org/
Apache License 2.0
509 stars 105 forks source link

Some confusion in pecos.utils.smat_utils.binarized #265

Open denghj3 opened 10 months ago

denghj3 commented 10 months ago

Description

the func show below, it describe all nonzero elements become 1, while it change the the whole mat to 1 in reality, with using X.data[:] = 1 or X[:] = 1

def binarized(X, inplace=False):
    """Binarize a dense/sparse matrix. All nonzero elements become 1.

    Args:
        X (np.ndarray, spmatrix): input matrix to binarize
        inplace (bool, optional): if True do the binarization in-place, else return a copy. Default False

    Returns:
        binarized X
    """

    if not isinstance(X, (np.ndarray, smat.spmatrix)):
        raise NotImplementedError(
            "this function only support X being np.ndarray or scipy.sparse.spmatrix."
        )

    if not inplace:
        X = X.copy()

    if isinstance(X, smat.spmatrix):
        X.data[:] = 1
    else:
        X[:] = 1

    return X

References

denghj3 commented 9 months ago

i have some ideas: when the X is spmatrix, it is right. But when the X is np.ndarray, it will change the whole ndarray to 1?