In order to do K-fold validation I would like to use slice a numpy array such that a view of the original array is made but with every nth element removed.
For example: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
If n = 4 then the result would be [1, 2, 4, 5, 6, 8, 9]
Note: the numpy requirement is due to this being used for a machine learning assignment where the dependencies are fixed.
Approach #1 with
modulus
Sample run -
Approach #2 with
masking
: Requirement as aview
Considering the views requirement, if the idea is to save on memory, we could store the equivalent boolean array that would occupy
8
times less memory on Linux system. Thus, such a mask based approach would be like so -Here's the memory requirement stat -
Then, we could use boolean-indexing as a view -
Approach #3 with
NumPy array strides
: Requirement as aview
You can use
np.lib.stride_tricks.as_strided
to create such a view given the length of the input array is a multiple ofn
. If it's not a multiple, it would still work, but won't be a safe practice, as we would be going beyond the memory allocated for input array. Please note that the view thus created would be2D
.Thus, an implementaion to get such a view would be -
Sample run -
numpy.delete :