I have a NumPy array of dimension 698x64x64 (64rows, 64columns)
I would like to slice it into a new array of 698x32x32 by taking every second row and column of the original array. How do I do that?
CodePudding user response:
IIUC, You can use slicing
like below. (You can read this : understanding-slicing)
import numpy as np
np.random.seed(123)
# this array is a sample array for showing result
a = np.random.rand(2,4,4)
print(a)
b = a[:, 1::2, 1::2] # <- you can use this for (698, 64, 64)
print(b)
Output:
# a =>
array([[[0.69646919, 0.28613933, 0.22685145, 0.55131477],
[0.71946897, 0.42310646, 0.9807642 , 0.68482974],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this
[0.4809319 , 0.39211752, 0.34317802, 0.72904971],
[0.43857224, 0.0596779 , 0.39804426, 0.73799541]],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this
[[0.18249173, 0.17545176, 0.53155137, 0.53182759],
[0.63440096, 0.84943179, 0.72445532, 0.61102351],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this
[0.72244338, 0.32295891, 0.36178866, 0.22826323],
[0.29371405, 0.63097612, 0.09210494, 0.43370117]]])
# -------------------^^^^^^^^^^,-----------,, ^^^^^^^^^^ <- you want this
# b =>
array([[[0.42310646, 0.68482974],
[0.0596779 , 0.73799541]],
[[0.84943179, 0.61102351],
[0.63097612, 0.43370117]]])