[Python] Find value groups in a numpy array and calculate the distance between them

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

  1. Stack

    Stack Membro Participativo

    I have a huge numpy 2D array. I'd like to find groups of points with the same values and find the distance of the closest similar group. An example:

    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 12 0 0 0
    0 0 0 0 0 0 0 12 12 0 0 0
    0 0 0 0 0 0 12 12 12 12 0 0
    0 0 0 0 0 12 12 12 12 0 0 0
    0 0 0 0 12 12 12 12 0 0 0 0
    0 0 12 12 12 12 12 0 0 0 0 0
    0 12 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 20 20 20 0
    0 0 0 0 0 0 0 0 0 20 20 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 14 14 0 0 0 0 0
    0 0 0 0 0 14 14 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0


    In this case, I've three groups: 12, 20, and 14. I´d like to find the groups, then calculate the minimum distance between them. In the case of 12th group, the closest is the 20th group, and the distance will be sqrt(2^2+4^2)=4.47.

    How can I do that?

    Continue reading...

Compartilhe esta Página