python - NumPy Stack 多维数组
问题描述
我有3个nD数组如下
x = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
y = [[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]
z = [[ 19, 20, 21],
[ 22, 23, 24],
[ 25, 26, 27]]
在不使用 for 循环的情况下,我试图将每个 2x2 矩阵元素附加在一起,这样
a1 = [[1,2]
[4,5]]
a2 = [[10,11],
[13,14]]
a3 = [[19,20],
[22,23]]
should append to
a = [[1,10,19],[2,11,20],[4,13,22],[5,14,23]]
Please note, the NxN matrix will always be N = j - 1 where j is x.shape(i,j)
Similarly for other 2x2 matrices, the arrays are as follows
b = [[2,11,20],[3,12,21],[5,14,23],[6,15,24]]
c = [[4,13,22],[5,14,23],[7,16,25],[8,17,26]]
d = [[5,14,23],[6,15,24],[8,17,26],[9,18,27]]
对于大型数据集,for 循环会影响运行时,所以我试图看看是否有使用 NumPy 堆叠技术的方法
解决方案
你的 3 个数组:
In [46]: x=np.arange(1,10).reshape(3,3)
In [48]: y=np.arange(10,19).reshape(3,3)
In [49]: z=np.arange(19,28).reshape(3,3)
合二为一:
In [50]: xyz=np.stack((x,y,z))
In [51]: xyz
Out[51]:
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]],
[[19, 20, 21],
[22, 23, 24],
[25, 26, 27]]])
您的a1
(2,2) 数组:
In [55]: xyz[0,:2,:2]
Out[55]:
array([[1, 2],
[4, 5]])
和所有3个:
In [56]: xyz[:,:2,:2]
Out[56]:
array([[[ 1, 2],
[ 4, 5]],
[[10, 11],
[13, 14]],
[[19, 20],
[22, 23]]])
并将它们重新排列成所需的(4,3):
In [57]: xyz[:,:2,:2].transpose(1,2,0)
Out[57]:
array([[[ 1, 10, 19],
[ 2, 11, 20]],
[[ 4, 13, 22],
[ 5, 14, 23]]])
In [58]: xyz[:,:2,:2].transpose(1,2,0).reshape(4,3)
Out[58]:
array([[ 1, 10, 19],
[ 2, 11, 20],
[ 4, 13, 22],
[ 5, 14, 23]])
对于其他窗口类似:
In [59]: xyz[:,1:3,:2].transpose(1,2,0).reshape(4,3)
Out[59]:
array([[ 4, 13, 22],
[ 5, 14, 23],
[ 7, 16, 25],
[ 8, 17, 26]])
In [60]: xyz[:,0:2,1:3].transpose(1,2,0).reshape(4,3)
Out[60]:
array([[ 2, 11, 20],
[ 3, 12, 21],
[ 5, 14, 23],
[ 6, 15, 24]])
In [61]: xyz[:,1:3,1:3].transpose(1,2,0).reshape(4,3)
Out[61]:
array([[ 5, 14, 23],
[ 6, 15, 24],
[ 8, 17, 26],
[ 9, 18, 27]])
我们也可以view_as_windows
像@Divakar 建议的那样(或as_strided
),但从概念上讲这更棘手。
====
如果我stack
不同,我可以跳过转置:
In [65]: xyz=np.stack((x,y,z), axis=2)
In [66]: xyz
Out[66]:
array([[[ 1, 10, 19],
[ 2, 11, 20],
[ 3, 12, 21]],
[[ 4, 13, 22],
[ 5, 14, 23],
[ 6, 15, 24]],
[[ 7, 16, 25],
[ 8, 17, 26],
[ 9, 18, 27]]])
In [68]: xyz[:2,:2].reshape(4,3)
Out[68]:
array([[ 1, 10, 19],
[ 2, 11, 20],
[ 4, 13, 22],
[ 5, 14, 23]])
===
In [84]: import skimage
In [85]: skimage.util.view_as_windows(xyz,(2,2,3),1).shape
Out[85]: (2, 2, 1, 2, 2, 3)
In [86]: skimage.util.view_as_windows(xyz,(2,2,3),1).reshape(4,4,3)
Out[86]:
array([[[ 1, 10, 19],
[ 2, 11, 20],
[ 4, 13, 22],
[ 5, 14, 23]],
[[ 2, 11, 20],
[ 3, 12, 21],
[ 5, 14, 23],
[ 6, 15, 24]],
[[ 4, 13, 22],
[ 5, 14, 23],
[ 7, 16, 25],
[ 8, 17, 26]],
[[ 5, 14, 23],
[ 6, 15, 24],
[ 8, 17, 26],
[ 9, 18, 27]]])
推荐阅读
- java - Java 8 int 数组映射
- c++ - 外壳附加重定向替换现有文本?
- php - 使用 PHP 中的命令删除文件夹及其内容?
- c - 递归反向链表
- thymeleaf - Thymeleaf 单选按钮 th:field 检查验证
- javascript - 比较 IndexedDB 中两个不同存储的值
- android - 如何使用 Room 检测 UPDATE sql 查询是否成功?
- osmnx - 将清理后的交点保存到 shapefile
- mysql - 更多列或一个数组,什么是更好的性能?
- firebase - 为什么 firebase serve 命令显示“Cannot GET /”?firebase+nuxt+express