14

Suppose I have a NumPy 2D array 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 = 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.

3 Answers 3

14

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:

>>> splits = np.hsplit(A, [1, 3]) 
>>> B = np.hstack((splits[0], splits[2]))
>>> C = splits[1]
Sign up to request clarification or add additional context in comments.

Comments

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])

Comments

3

For C you can use simple slicing:

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

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

>>> 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]])
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.