[Python] Csr matrix: How to replace missing value with np.nan instead of 0?

Discussão em 'Python' iniciado por Stack, Setembro 5, 2025.

  1. Stack

    Stack Membro Participativo

    It seems that csr_matrix fill missing value with 0 in default. So how to fill the missing value with np.nan?

    from scipy.sparse import csr_matrix
    row = np.array([0, 0, 1, 2, 2, 2])
    col = np.array([0, 2, 2, 0, 1, 2])
    data = np.array([0, 2, 3, 4, 5, 6])
    csr_matrix((data, (row, col)), shape=(3, 3)).toarray()


    Output:

    array([[0, 0, 2],
    [0, 0, 3],
    [4, 5, 6]])


    Expected:

    array([[0, np.nan, 2],
    [np.nan, np.nan, 3],
    [4, 5, 6]])

    Continue reading...

Compartilhe esta Página