Compare neighbors in array
You can improve iterating over the array by using np.ndenumerate to get the current coordinates and current item. From the coordinates you can derive the neighbouring elements.
You can also use inverse checking to only set the respective field to False if a neighbor does not match:

from contextlib import suppress

def compare_neighbors(arr):

    comp_arr = np.full(arr.shape, True, dtype=bool)

    for (x, y), item in np.ndenumerate(arr):
        # Check left.
        if x &gt;= 0:
            if arr[x-1, y] != item:
                comp_arr[x, y] = False
                continue

        # Check right.
        with suppress(IndexError):
            if arr[x+1, y] != item:
                comp_arr[x, y] = False
                continue

        # Check top.
        with suppress(IndexError):
            if arr[x, y+1] != item:
                comp_arr[x, y] = False
                continue

        # Check bottom.
        if y &gt;= 0:
            if arr[x, y-1] != item:
                comp_arr[x, y] = False
                continue

    return comp_arr