Linda
Linda

Reputation: 3

Divide a 3d numpy array into 2 groups python

i have a 3d array in this form (12457,8,6) i wand to divide it into 2 equal numpy arrays like (12457,3,8) In fact that the first one containt the first 3 bands and the second one contains the remaind bands: In other words I want my array1 contains the bands 1,2,3 and my array2 contains the bands 4,5,6

I tried with that but it doesnt work

array1=data[:,:,3]
array1.shape
(12457,8)

Upvotes: 0

Views: 2175

Answers (2)

UncleCid
UncleCid

Reputation: 1

split or array_split should be able to help you out.

import numpy as np

arr = np.array([[1,2,3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
newarr = np.split(arr, 2)
print(newarr)

Prints:

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

Upvotes: 0

Akshay Sehgal
Akshay Sehgal

Reputation: 19322

You can use np.split -

X = np.random.random((1200,6,8))
print(X.shape)

X1, X2 = np.split(X, 2, axis=1) #Array, num of splits, axis for splitting
print(X1.shape, X2.shape)
(1200, 6, 8)
(1200, 3, 8) (1200, 3, 8)

Upvotes: 2

Related Questions