util.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:prysm 作者: brandondube 项目源码 文件源码
def fold_array(array, axis=1):
    ''' Folds an array in half over the given axis and averages.

    Args:
        array (`numpy.ndarray`): 2d array to fold.

        axis (`int`): axis to fold over.

    Returns
        numpy.ndarray:  new array.

    '''

    xs, ys = array.shape
    if axis is 1:
        xh = xs // 2
        left_chunk = array[:, :xh]
        right_chunk = array[:, xh:]
        folded_array = np.concatenate((right_chunk[:, :, np.newaxis],
                                       np.flip(np.flip(left_chunk, axis=1),
                                               axis=0)[:, :, np.newaxis]),
                                      axis=2)
    else:
        yh = ys // 2
        top_chunk = array[:yh, :]
        bottom_chunk = array[yh:, :]
        folded_array = np.concatonate((bottom_chunk[:, :, np.newaxis],
                                       np.flip(np.flip(top_chunk, axis=1),
                                               axis=0)[:, :, np.newaxis]),
                                      axis=2)
    return folded_array.mean(axis=2)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号