Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否可以使用numpy中可用的函数将2d数组修补到子数组中?_Python_Numpy - Fatal编程技术网

Python 是否可以使用numpy中可用的函数将2d数组修补到子数组中?

Python 是否可以使用numpy中可用的函数将2d数组修补到子数组中?,python,numpy,Python,Numpy,是否可以使用np.RESPORE和np.split函数将2d阵列修补为子阵列阵列 import numpy as np data = np.arange(24).reshape(4,6) print data [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] answer = np.split(data,(-1,2,2),axis=1) 预期答案是: answer = [[

是否可以使用np.RESPORE和np.split函数将2d阵列修补为子阵列阵列

import numpy as np
data = np.arange(24).reshape(4,6)
print data
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

answer = np.split(data,(-1,2,2),axis=1)
预期答案是:

answer = [[[ 0  1]
   [ 6  7]]

  [[ 2  3]
   [ 8  9]]

  [[ 4  5]
   [10 11]]    

 [[12 13]
   [18 19]]

  [[14 15]
   [20 21]]

  [[16 17]
   [22 23]]]

split
不能同时用于多轴。但这里有一个解决方案,使用此操作两次:

In [1]: import numpy as np

In [2]: data = np.arange(24).reshape(4,6)

In [3]: chunk = 2, 2

In [4]: tmp = np.array(np.split(data, data.shape[1]/chunk[1], axis=1))

In [5]: answer = np.vstack(np.split(tmp, tmp.shape[1]/chunk[0], axis=1))

In [6]: answer
Out[6]: 
array([[[ 0,  1],
        [ 6,  7]],

       [[ 2,  3],
        [ 8,  9]],

       [[ 4,  5],
        [10, 11]],

       [[12, 13],
        [18, 19]],

       [[14, 15],
        [20, 21]],

       [[16, 17],
        [22, 23]]])

但是,我更喜欢Cyber所注意到的解决方案。

您也可以执行以下操作:

>>> data = np.arange(24).reshape(4,6)
>>> data_split = data.reshape(2, 2, 3, 2)
>>> data_split = np.transpose(data_split, (0, 2, 1, 3))
>>> data_split = data_split.reshape(-1, 2, 2) # this makes a copy
>>> data_split
array([[[ 0,  1],
        [ 6,  7]],

       [[ 2,  3],
        [ 8,  9]],

       [[ 4,  5],
        [10, 11]],

       [[12, 13],
        [18, 19]],

       [[14, 15],
        [20, 21]],

       [[16, 17],
        [22, 23]]])

如果你真的想在这个数组上调用split,这应该很简单,但是这个重新排序的数组在大多数设置中都会像split返回的元组一样工作。

请看我已经看到的这个线程,但是我只想使用问题中提到的numpy中的可用函数