python中repeat、tile、broadcast区别与联系

1、numpy.repeat(a, repeats, axis=None)

在指定的维度上重复数组中的元素
参数:
a:输入序列
repeats:每个元素重复的次数
axis:要沿其重复值的轴。默认情况下,使用展开的输入数组,并返回展开的输出数组。

例1:输入序列3,repeat 4倍,axis使用默认值

np.repeat(3, 4)
array([3, 3, 3, 3])

例2:输入序列[[1,2],[3,4]],维度为2x2,因为axis使用默认值,因此将输入序列铺平,变为[1,2,3,4],维度为(4,),然后repeats为2,

x = np.array([[1,2],[3,4]])
np.repeat(x, 2)
array([1, 1, 2, 2, 3, 3, 4, 4])

例3:输入尺寸为2x2,在第二个维度上repeat

x = np.array([[1,2],[3,4]])
np.repeat(x, 3, axis=1)
array([[1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4]])

例3:输入尺寸为2x2x2,在第二个维度上repeat

x = np.array([[[1,4],[2,5]],[[3,6],[4,7]]])
np.repeat(x, 3, axis=1)
array([[[1, 4],
        [1, 4],
        [1, 4],
        [2, 5],
        [2, 5],
        [2, 5]],
        
       [[3, 6],
        [3, 6],
        [3, 6],
        [4, 7],
        [4, 7],
        [4, 7]]])

例4:输入尺寸为2x2,在第一个维度上repeat

x = np.array([[1,2],[3,4]])
np.repeat(x, [1, 2], axis=0)
array([[1, 2],
       [3, 4],
       [3, 4]])

2、numpy.tile(A, reps)

  • Construct an array by repeating A the number of times given by reps
    将输入序列A重复一定的倍数

  • 参数
    若A 的维度小于reps的,A扩展新的维度,例如(3,) reshape 为(1, 3) ,或者 (1, 3) reshape为 (1, 1, 3);

If A.ndim > d, reps is promoted to A.ndim by pre-pending 1’s to it. Thus for an A of shape (2, 3, 4, 5), a reps of (2, 2) is treated as (1, 1, 2, 2).

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值