5

I have a numpy array of a shape (400, 3, 3, 3) and I want to split it into two parts, so I would get arrays like (100, 3, 3, 3) and (300, 3, 3, 3).

I was playing with numpy split methods, e.g.:

subsets = np.array_split(arr, 2)

which gives me what I want, but it divides the original array into two halves the same size and I don't know how to specify these sizes. It'd be probably easy with some indexing (I guess) but I'm not sure how to do it.

3
  • 2
    x, y = arr[:100, ...], arr[100:, ...] should do... Commented Nov 5, 2017 at 8:42
  • 1
    I'd use the slice notation like @cᴏʟᴅsᴘᴇᴇᴅ suggests, it potentially takes much less memory (since the arrays would share the underlying buffer). Not sure if that is what will happen with split, but if you must, you could do subsets = np.array_spit(arr, [100]) Commented Nov 5, 2017 at 8:45
  • The slice notation is exactly what I was looking for, thanks. Commented Nov 5, 2017 at 8:48

1 Answer 1

6

As mentioned in my comment, you can use the Ellipsis notation to specify all axes:

x, y = arr[:100, ...], arr[100:, ...]
Sign up to request clarification or add additional context in comments.

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.