pynewbie
pynewbie

Reputation: 143

Splitting one NumPy array into two arrays

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.

Upvotes: 14

Views: 15355

Answers (3)

Alex Riley
Alex Riley

Reputation: 177088

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]

Upvotes: 14

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251186

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

Upvotes: 3

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 59015

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

Upvotes: 4

Related Questions