def get_checkerboard_matrix ( kernel_width ) : return np . vstack ( ( np . hstack ( ( - 1 * np . ones ( ( kernel_width , kernel_width ) ) , np . ones ( ( kernel_width , kernel_width ) ) ) ) , np . hstack ( ( np . ones ( ( kernel_width , kernel_width ) ) , - 1 * np . ones ( ( kernel_width , kernel_width ) ) ) ) ) )
square matrix with a checkerboard pattern of 0's and 1's of any given size python
example matrix for width = 2
def get_checkerboard_matrix(kernel_width):

    """
    example matrix for width = 2

    -1  -1    1   1
    -1  -1    1   1
     1   1   -1  -1
     1   1   -1  -1

    :param kernel_width:
    :return:
    """

    return np.vstack((
        np.hstack((
            -1 * np.ones((kernel_width, kernel_width)), np.ones((kernel_width, kernel_width))
        )),
        np.hstack((
            np.ones((kernel_width, kernel_width)), -1 * np.ones((kernel_width, kernel_width))
        ))
    ))