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0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]]
[[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]]
[[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]
[0. 0. 0. 0. 0. ]]
[[0.8 0.2 0. 0. 0. ]
[0. 0.8 0.2 0. 0. ]
[0. 0. 0.8 0.2 0. ]
[0. 0. 0. 0.8 0.2 ]
[0. 0. 0. 0. 1. ]]]]
我如何有效地将其转换为(25,25)矩阵,其第一行是前五个(5,5)块的第一行的串联,而第二行是该矩阵的第二行的串联前(5,5)个块,依此类推?例如,给定我的输入矩阵,输出矩阵的第一行应为:
[0.64 0.16 0. 0. 0. 0.16 0.04 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. ]
第六行应是第6到第10个5×5块的第一行的组合,即:
[0. 0. 0. 0. 0. 0.64 0.16 0. 0. 0. 0.16
0.04 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. ]
我尝试了numpy.reshape(input,(25,25)),但没有得到我想要的结果。任何帮助表示赞赏!
答案 0 :(得分:2)
在重塑之前,请使用swapaxes
(或transpose
)重新排列轴的顺序:
In [48]: y = x.swapaxes(1,2).reshape(25,25)
In [49]: y[0]
Out[49]:
array([0.64, 0.16, 0. , 0. , 0. , 0.16, 0.04, 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. ])
In [50]: y[5]
Out[50]:
array([0. , 0. , 0. , 0. , 0. , 0.64, 0.16, 0. , 0. , 0. , 0.16,
0.04, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. ])