为了账号安全,请及时绑定邮箱和手机立即绑定

通过最近的邻居切片对numpy数组进行升采样的快速方法

通过最近的邻居切片对numpy数组进行升采样的快速方法

狐的传说 2019-10-21 14:44:41
我有一个2D整数数组MxN,我想将该数组扩展为(BM)x(BN),其中B是正方形瓷砖边的长度,因此输入数组的每个元素都作为BxB块重复在最后的数组中。下面是带有嵌套的for循环的示例。有更快/内置的方法吗?import numpy as npa = np.arange(9).reshape([3,3])            # input array - 3x3B=2.                                       # block size - 2  A = np.zeros([a.shape[0]*B,a.shape[1]*B])  # output array - 6x6# Loop, filling A with tiled values of a at each indexfor i,l in enumerate(a):                   # lines in a    for j,aij in enumerate(l):             # a[i,j]        A[B*i:B*(i+1),B*j:B*(j+1)] = aij结果...a=      [[0 1 2]         [3 4 5]         [6 7 8]]A =     [[ 0.  0.  1.  1.  2.  2.]         [ 0.  0.  1.  1.  2.  2.]         [ 3.  3.  4.  4.  5.  5.]         [ 3.  3.  4.  4.  5.  5.]         [ 6.  6.  7.  7.  8.  8.]         [ 6.  6.  7.  7.  8.  8.]]
查看完整描述

3 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

一种选择是


>>> a.repeat(2, axis=0).repeat(2, axis=1)

array([[0, 0, 1, 1, 2, 2],

       [0, 0, 1, 1, 2, 2],

       [3, 3, 4, 4, 5, 5],

       [3, 3, 4, 4, 5, 5],

       [6, 6, 7, 7, 8, 8],

       [6, 6, 7, 7, 8, 8]])

由于中间数组,这有点浪费,但至少是简洁的。


查看完整回答
反对 回复 2019-10-21
  • 3 回答
  • 0 关注
  • 1236 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信