如何切片和扩展二维numpy阵列?

时间:2021-10-02 15:39:11

I have a numpy array of size nxm. I want the number of columns to be limited to k and rest of the columns to be extended in new rows. Following is the scenario -

我有一个numpy数组大小的nxm。我希望将列的数量限制为k,其余的列将被扩展到新的行中。下面是场景。

Initial array: nxm

最初的数组:nxm

Final array: pxk

最后的数组:pxk

where p = (m/k)*n

在p =(m / k)* n

Eg. n = 2, m = 6, k = 2

如。n = 2 m = 6 k = 2。

Initial array:

最初的数组:

[[1, 2, 3, 4, 5, 6,],
[7, 8, 9, 10, 11, 12]]

Final array:

最后的数组:

[[1, 2],
[7, 8],
[3, 4],
[9, 10],
[5, 6],
[11, 12]]

I tried using reshape but not getting the desired result.

我试着用整形术但没有得到想要的结果。

2 个解决方案

#1


4  

Here's one way to do it

这是一种方法。

q=array([[1, 2, 3, 4, 5, 6,],
         [7, 8, 9, 10, 11, 12]])
r=q.T.reshape(-1,2,2)
s=r.swapaxes(1,2)
t=s.reshape(-1,2)

as a one liner,

作为一个衬套,

q.T.reshape(-1,2,2).swapaxes(1,2).reshape(-1,2)

array([[ 1,  2],
       [ 7,  8],
       [ 3,  4],
       [ 9, 10],
       [ 5,  6],
       [11, 12]])

EDIT: for the general case, use

编辑:一般情况下,使用。

q=arange(1,1+n*m).reshape(n,m) #example input
r=q.T.reshape(-1,k,n)
s=r.swapaxes(1,2)
t=s.reshape(-1,k)

one liner is:

一个班轮是:

q.T.reshape(-1,k,n).swapaxes(1,2).reshape(-1,k)

example for n=3,m=12,k=4

例子n = 3 m = 12 k = 4

q=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, 28, 29, 30, 31, 32, 33, 34, 35, 36]])

result is

结果是

array([[ 1,  2,  3,  4],
       [13, 14, 15, 16],
       [25, 26, 27, 28],
       [ 5,  6,  7,  8],
       [17, 18, 19, 20],
       [29, 30, 31, 32],
       [ 9, 10, 11, 12],
       [21, 22, 23, 24],
       [33, 34, 35, 36]])

#2


2  

Using numpy.vstack and numpy.hsplit:

使用numpy。vstack numpy.hsplit:

a = np.array([[1, 2, 3, 4, 5, 6,],
              [7, 8, 9, 10, 11, 12]])
n, m, k = 2, 6, 2
np.vstack(np.hsplit(a, m/k))

result array:

结果数组:

array([[ 1,  2],
       [ 7,  8],
       [ 3,  4],
       [ 9, 10],
       [ 5,  6],
       [11, 12]])

UPDATE As flebool commented, above code is very slow, because hsplit returns a python list, and then vstack reconstructs the final array from a list of arrays.

正如flebool评论的那样,上面的代码非常慢,因为hsplit返回一个python列表,然后vstack从数组的列表中重新构造最终的数组。

Here's alternative solution that is much faster.

这里有一个更快的替代解决方案。

a.reshape(-1, m/k, k).transpose(1, 0, 2).reshape(-1, k)

or

a.reshape(-1, m/k, k).swapaxes(0, 1).reshape(-1, k)

#1


4  

Here's one way to do it

这是一种方法。

q=array([[1, 2, 3, 4, 5, 6,],
         [7, 8, 9, 10, 11, 12]])
r=q.T.reshape(-1,2,2)
s=r.swapaxes(1,2)
t=s.reshape(-1,2)

as a one liner,

作为一个衬套,

q.T.reshape(-1,2,2).swapaxes(1,2).reshape(-1,2)

array([[ 1,  2],
       [ 7,  8],
       [ 3,  4],
       [ 9, 10],
       [ 5,  6],
       [11, 12]])

EDIT: for the general case, use

编辑:一般情况下,使用。

q=arange(1,1+n*m).reshape(n,m) #example input
r=q.T.reshape(-1,k,n)
s=r.swapaxes(1,2)
t=s.reshape(-1,k)

one liner is:

一个班轮是:

q.T.reshape(-1,k,n).swapaxes(1,2).reshape(-1,k)

example for n=3,m=12,k=4

例子n = 3 m = 12 k = 4

q=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, 28, 29, 30, 31, 32, 33, 34, 35, 36]])

result is

结果是

array([[ 1,  2,  3,  4],
       [13, 14, 15, 16],
       [25, 26, 27, 28],
       [ 5,  6,  7,  8],
       [17, 18, 19, 20],
       [29, 30, 31, 32],
       [ 9, 10, 11, 12],
       [21, 22, 23, 24],
       [33, 34, 35, 36]])

#2


2  

Using numpy.vstack and numpy.hsplit:

使用numpy。vstack numpy.hsplit:

a = np.array([[1, 2, 3, 4, 5, 6,],
              [7, 8, 9, 10, 11, 12]])
n, m, k = 2, 6, 2
np.vstack(np.hsplit(a, m/k))

result array:

结果数组:

array([[ 1,  2],
       [ 7,  8],
       [ 3,  4],
       [ 9, 10],
       [ 5,  6],
       [11, 12]])

UPDATE As flebool commented, above code is very slow, because hsplit returns a python list, and then vstack reconstructs the final array from a list of arrays.

正如flebool评论的那样,上面的代码非常慢,因为hsplit返回一个python列表,然后vstack从数组的列表中重新构造最终的数组。

Here's alternative solution that is much faster.

这里有一个更快的替代解决方案。

a.reshape(-1, m/k, k).transpose(1, 0, 2).reshape(-1, k)

or

a.reshape(-1, m/k, k).swapaxes(0, 1).reshape(-1, k)