def stratify(cnt_dict, X, y, scale=4):
    """ return a subsample of X and y where each class only appears maximum `scale x the minimum count` """
    allowed_size = min(cnt_dict.values()) * scale
    indices = []
    for val, cnt in cnt_dict.items():
        if cnt < allowed_size:
            indices += y[y==val].index.to_list()
        else:
            indices += y[y==val].sample(allowed_size).index.to_list()
    return X.loc[indices], y.loc[indices]