I have an array x of 30 samples, and I wish to separate it out into chunks of 8 samples each in 2 different ways.
First, I want to separate it avoiding any overlap so that I end up with 3 arrays of length 8 and the final array will be only 6 (due to some samples being missing).
Secondly, I want to separate it so that the final array will be the last 2 samples of the previous array plus the final 6.
Both methods preferably without for loops as I'm trying to optimise this for when I expand it to arrays with lengths in the ten thousands.
I have tried using np.array_split as follows
x = np.array([1 ,1, 2 ,1 ,1 ,2 ,1, 0 ,3, 1, 2 ,2, 1, 2, 1, 1,50,1 ,1, 1, 1, 4, 1, 11, 15, 0, 0, 1, 1,0])
y = np.array_split(x,np.ceil(len(x)/8))
However, that results in:
y = [array([1, 1, 2, 1, 1, 2, 1, 0]),
array([3, 1, 2, 2, 1, 2, 1, 1]),
array([50, 1, 1, 1, 1, 4, 1]),
array([11, 15, 0, 0, 1, 1, 0])]
so y is clearly made up of 2x8 length arrays and 2x7 length arrays, not what I want. How do I go about achieving it the way I want. The first method is the more important, the second is a bonus.
Thanks
CodePudding user response:
"""for the first you can use range"""
x = np.array([1 ,1, 2 ,1 ,1 ,2 ,1, 0 ,3, 1, 2 ,2, 1, 2, 1, 1,50,1 ,1, 1, 1, 4, 1, 11, 15, 0, 0, 1, 1,0])
res = [x[i:i 8] for i in range(0, len(x), 8)]
"""for the second you could just pop the first item"""
res.pop(0)
print(res)
CodePudding user response:
import numpy as np
x = np.array([1 ,1, 2 ,1 ,1 ,2 ,1, 0 ,3, 1, 2 ,2, 1, 2, 1, 1,50 ,1 ,1, 1, 1, 4, 1, 11, 15, 0, 0, 1, 1,0])
def split_reminder(x, chunk_size, axis=0):
indices = np.arange(chunk_size, x.shape[axis], chunk_size)
return np.array_split(x, indices, axis)
split_reminder(x, 8)
Checkout the below link for reference: Similar answer
CodePudding user response:
Use utilpsace
from utilspie import iterutils
x = np.array([1 ,1, 2 ,1 ,1 ,2 ,1, 0 ,3, 1, 2 ,2, 1, 2, 1, 1,50,1 ,1, 1, 1, 4, 1, 11, 15, 0, 0, 1, 1,0])
print(list(iterutils.get_chunks(x, 8)))
Gives
[array([1, 1, 2, 1, 1, 2, 1, 0]),
array([3, 1, 2, 2, 1, 2, 1, 1]),
array([50, 1, 1, 1, 1, 4, 1, 11]),
array([15, 0, 0, 1, 1, 0])]
Solution 2 - WOrking on it..
CodePudding user response:
You could split just the part of the array will produces your chunk size then add back on an array of the final 8 values
num = int(len(x)/8)
y = np.array_split(x[:num*8], num)
y = [x[-9:-1]]