def pad_to_square(a, pad_value=0):
    m = a.reshape((a.shape[0], -1))
padded = pad_value * np.ones(2 * [max(m.shape)], dtype=m.dtype)
padded[0:m.shape[0], 0:m.shape[1]] = m
return padded
Making a matrix square and padding it with desired value in numpy