将一个NumPy数组分割为两个数组

时间:2021-12-12 12:32:52

Suppose I have a NumPy 2D array A:

假设我有一个NumPy 2D数组a:

>>> import numpy as np
>>> A=np.arange(30).reshape(3,10)
>>> A
array([[ 0,  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]])

I need to get two arrays B and C with the following properties:

我需要得到两个数组B和C,具有以下属性:

B = array([[ 0,  3,  4,  5,  6,  7,  8,  9],
           [10, 13, 14, 15, 16, 17, 18, 19],
           [20, 23, 24, 25, 26, 27, 28, 29]])

C = array([[ 1,  2],
           [11, 12],
           [21, 22]])

What is the easiest way to accomplish this?

最简单的方法是什么?

Note that I have to get all sets of C (2 adjacent columns) and B (which is A without C). I tried different NumPy constructs like np.delete, np.hstack but nothing seem to work at the corner conditions like in the above example.

注意,我必须得到所有C(2个相邻列)和B (A没有C)的集合。但是在类似于上面示例的角落条件下,似乎没有任何东西可以工作。

3 个解决方案

#1


10  

One of the simplest ways is to use indexing to select the appropriate columns:

最简单的方法之一是使用索引来选择适当的列:

>>> A[:, [1, 2]] # choose all rows from columns 1-2 (gives C)
array([[ 1,  2],
       [11, 12],
       [21, 22]])

>>> A[:, np.r_[0, 3:10]] # choose all rows from columns 0, 3-9 (gives B)
array([[ 0,  3,  4,  5,  6,  7,  8,  9],
       [10, 13, 14, 15, 16, 17, 18, 19],
       [20, 23, 24, 25, 26, 27, 28, 29]])

Alternatively, you could try hsplit break up A and then concatenate bits back together. This feels less efficient than the indexing method above though:

或者,您可以尝试hsplit broken A,然后将位连接到一起。这感觉不如上面的索引方法有效:

>>> splits = np.hsplit(A, [1, 3]) 
>>> B = np.hstack((splits[0], splits[2]))
>>> C = splits[1]

#2


4  

You can use array fancy indexing:

你可以使用数组奇特索引:

B = A[:, [0] + list(range(3, A.shape[1]))]
C = A[:, [1, 2]]

where:

地点:

  • the comma separates the indices you want to take from each dimension.
  • 逗号分隔你想从每个维度取的指标。
  • operator : tells to take all elements of that dimension
  • 运算符:告诉取该维度的所有元素
  • using a sequence of integers will specify which elements of the corresponding dimension should be taken (ex. [1, 2])
  • 使用一个整数序列将指定应该采用相应维度的哪个元素(例如[1,2])

#3


3  

For C you can use simple slicing:

对于C,你可以使用简单的切片:

>>> A[:,1:3]
array([[ 1,  2],
       [11, 12],
       [21, 22]])

For B use numpy.hstack on two slices of A:

B numpy使用。两个A片上的hstack:

>>> np.hstack((A[:,:1], A[:,3:]))
array([[ 0,  3,  4,  5,  6,  7,  8,  9],
       [10, 13, 14, 15, 16, 17, 18, 19],
       [20, 23, 24, 25, 26, 27, 28, 29]])
>>> 

#1


10  

One of the simplest ways is to use indexing to select the appropriate columns:

最简单的方法之一是使用索引来选择适当的列:

>>> A[:, [1, 2]] # choose all rows from columns 1-2 (gives C)
array([[ 1,  2],
       [11, 12],
       [21, 22]])

>>> A[:, np.r_[0, 3:10]] # choose all rows from columns 0, 3-9 (gives B)
array([[ 0,  3,  4,  5,  6,  7,  8,  9],
       [10, 13, 14, 15, 16, 17, 18, 19],
       [20, 23, 24, 25, 26, 27, 28, 29]])

Alternatively, you could try hsplit break up A and then concatenate bits back together. This feels less efficient than the indexing method above though:

或者,您可以尝试hsplit broken A,然后将位连接到一起。这感觉不如上面的索引方法有效:

>>> splits = np.hsplit(A, [1, 3]) 
>>> B = np.hstack((splits[0], splits[2]))
>>> C = splits[1]

#2


4  

You can use array fancy indexing:

你可以使用数组奇特索引:

B = A[:, [0] + list(range(3, A.shape[1]))]
C = A[:, [1, 2]]

where:

地点:

  • the comma separates the indices you want to take from each dimension.
  • 逗号分隔你想从每个维度取的指标。
  • operator : tells to take all elements of that dimension
  • 运算符:告诉取该维度的所有元素
  • using a sequence of integers will specify which elements of the corresponding dimension should be taken (ex. [1, 2])
  • 使用一个整数序列将指定应该采用相应维度的哪个元素(例如[1,2])

#3


3  

For C you can use simple slicing:

对于C,你可以使用简单的切片:

>>> A[:,1:3]
array([[ 1,  2],
       [11, 12],
       [21, 22]])

For B use numpy.hstack on two slices of A:

B numpy使用。两个A片上的hstack:

>>> np.hstack((A[:,:1], A[:,3:]))
array([[ 0,  3,  4,  5,  6,  7,  8,  9],
       [10, 13, 14, 15, 16, 17, 18, 19],
       [20, 23, 24, 25, 26, 27, 28, 29]])
>>>