I want to slice a numpy array with its index and reshape that with one-liner lambda function. Let me explain with minimal producible example:
This is my numpy array.
In [1]: composite
Out[1]:
array([ 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 3,
-1, 3, 1, 3, 1, 1, 3, 1, -1, 1, -1, -1, 3, 1, 1, 1, -1,
1, 1, -3, -3, -3, -1, -1, -3, -1, -1, 1, -3, -1, -1, -1, -1, -3,
-1, -3, 1, -1, -3, -1, -3, -1, -1, -1, -3, -3, -3, -1, -3, -3, 1,
1, 1, -1, -1, 1, -1, -1, -3, 1, -1, -1, -1, -1, 1, -1, 1, -3,
-1, 1, -1, 1, -1, -1, -1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 3,
3, 1, 3, 3, 1, 1, 3, -1, 3, -1, 1, -1, 1, 1, -1, 1, 3,
1, 3, 3, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1,
1, -1, -1, 1, -3, 1, -3, -1, -3, -1, -1, -3, -1, 1, -1, 1, 1,
-3, -1, -3, -3, -1, -3, -3, 1, 1, 1, -1, -1, 1, -1, -1, -3, 1,
-1, -1, -1, -1, 1, -1, 1, -3, -1, 1, -1, 1, -1, -1, -1, 1, -1,
-1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -3, 1,
-3, -1, -3, -1, -1, -3, -1, 1, -1, 1, 1, -3, -1, 1, 1, -1, 1,
1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 3, -1, 3, 1, 3,
1, 1, 3, 1, -1, 1, -1, -1, 3, 1])
I want to slice every 32 element in the array and make them as one element in array, something like this:
# I know i used composite[:32] and get below
array([ 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 3,
-1, 3, 1, 3, 1, 1, 3, 1, -1, 1, -1, -1, 3, 1, 1])
This is just an example but the actual length of the array is unknown in my case so i don't know how big my array is going to be. But i want something like this:
# I know i used composite[:32] and get below
array([ 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, 3,
-1, 3, 1, 3, 1, 1, 3, 1, -1, 1, -1, -1, 3, 1, 1],
[-1, 1, 1, -3, -3, -3, -1, -1, -3, -1, -1, 1, -3, -1, -1,
-1, -1,-3, -1, -3, 1, -1, -3, -1, -3, -1, -1, -1, -3, -3, -3], ... and so
on...till the end of the array))
Is there an intelligent way of doing this ? Perhaps a lambda function ?
presently i am manually doing it using below function.
def de_repeater(seq, size):
chunks = []
index = 0
while index < len(seq):
#print(list(seq[index:index size]))
#print(len(list(seq[index:index size])))
chunks.append(list(seq[index:index size]))
index =size
bits = []
for List in chunks:
#print(max(set(List), key = List.count))
bits.append(max(set(List), key = List.count))
return bits
a1 = de_repeater(composite, 8)
len(a1) #31
CodePudding user response:
You can use np.reshape()
if you know that the length of the array is a multiple of 32 (as described in a comment):
np.reshape(composite, (-1, 32))
If you pad the original composite
array with eight ones, you'll get:
[[ 1 1 -1 1 1 1 1 1 -1 -1 1 -1 -1 1 1 -1 3 -1 3 1 3 1 1 3
1 -1 1 -1 -1 3 1 1]
[ 1 -1 1 1 -3 -3 -3 -1 -1 -3 -1 -1 1 -3 -1 -1 -1 -1 -3 -1 -3 1 -1 -3
-1 -3 -1 -1 -1 -3 -3 -3]
[-1 -3 -3 1 1 1 -1 -1 1 -1 -1 -3 1 -1 -1 -1 -1 1 -1 1 -3 -1 1 -1
1 -1 -1 -1 1 1 1 3]
[ 1 1 1 1 1 3 3 1 3 3 1 1 3 -1 3 -1 1 -1 1 1 -1 1 3 1
3 3 -1 1 -1 -1 1 -1]
[-1 -1 -1 -1 1 1 -1 1 1 -1 -1 1 -3 1 -3 -1 -3 -1 -1 -3 -1 1 -1 1
1 -3 -1 -3 -3 -1 -3 -3]
[ 1 1 1 -1 -1 1 -1 -1 -3 1 -1 -1 -1 -1 1 -1 1 -3 -1 1 -1 1 -1 -1
-1 1 -1 -1 1 -1 -1 -1]
[-1 -1 1 1 -1 1 1 -1 -1 1 -3 1 -3 -1 -3 -1 -1 -3 -1 1 -1 1 1 -3
-1 1 1 -1 1 1 1 1]
[ 1 -1 -1 1 -1 -1 1 1 -1 3 -1 3 1 3 1 1 3 1 -1 1 -1 -1 3 1
1 1 1 1 1 1 1 1]]
CodePudding user response:
Try using .reshape()
:
size = round(len(composite)/32) # 8
composite.reshape((size,-1))
Output:
array([[ 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1,
3, -1, 3, 1, 3, 1, 1, 3, 1, -1, 1, -1, -1, 3, 1],
[ 1, 1, -1, 1, 1, -3, -3, -3, -1, -1, -3, -1, -1, 1, -3, -1,
-1, -1, -1, -3, -1, -3, 1, -1, -3, -1, -3, -1, -1, -1, -3],
[-3, -3, -1, -3, -3, 1, 1, 1, -1, -1, 1, -1, -1, -3, 1, -1,
-1, -1, -1, 1, -1, 1, -3, -1, 1, -1, 1, -1, -1, -1, 1],
[ 1, 1, 3, 1, 1, 1, 1, 1, 3, 3, 1, 3, 3, 1, 1, 3,
-1, 3, -1, 1, -1, 1, 1, -1, 1, 3, 1, 3, 3, -1, 1],
[-1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1,
-3, 1, -3, -1, -3, -1, -1, -3, -1, 1, -1, 1, 1, -3, -1],
[-3, -3, -1, -3, -3, 1, 1, 1, -1, -1, 1, -1, -1, -3, 1, -1,
-1, -1, -1, 1, -1, 1, -3, -1, 1, -1, 1, -1, -1, -1, 1],
[-1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1,
-3, 1, -3, -1, -3, -1, -1, -3, -1, 1, -1, 1, 1, -3, -1],
[ 1, 1, -1, 1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1,
3, -1, 3, 1, 3, 1, 1, 3, 1, -1, 1, -1, -1, 3, 1]])