[Python] Replace all Zeros by Ones in a numpy array

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a numpy array:

    a = np.array([[-1,1,-1],[-1,1,1]])


    My array only contains two different values: -1 and 1. However, I want to replace all 1's by 0 and all -1's by 1. Of course I can loop over my array, check the value of every field and replace it. This will work for sure, but I was looking for a more convenient way to do it.

    I am looking for some sort of

    replace(old, new)


    function.

    Continue reading...

Compartilhe esta Página