I’m starting our with list of tuples (each tuple is an (X,Y)). My end result is I want to find the maximum Y-value within EACH window/bin of length 4 using numpy.
JavaScript
x
24
24
1
# List of tuples
2
[(0.05807200929152149, 9.9720125), (0.34843205574912894, 1.1142874), (0.6387921022067363, 2.0234027), (0.9291521486643438, 1.4435122), (1.207897793263647, 2.3677678), (1.4982578397212543, 1.9457655), (1.7886178861788617, 2.8343441), (2.078977932636469, 5.7816567), ]
3
4
# Convert to numpy array
5
dt = np.dtype('float,float')
6
arr = np.asarray(listTuples, dt)
7
# [(0.05807201, 9.97201252) (0.34843206, 1.11428738)
8
# (0.6387921 , 2.02340269) (0.92915215, 1.4435122 )
9
# (1.20789779, 2.36776781) (1.49825784, 1.9457655 )
10
# (1.78861789, 2.83434415) (2.07897793, 5.78165674)
11
# (2.36933798, 3.14842606) ...]
12
13
#Create windows/blocks of 4 elements
14
arr = arr.reshape(-1,4)
15
# [[(0.05807201, 9.97201252) (0.34843206, 1.11428738)
16
# (0.6387921 , 2.02340269) (0.92915215, 1.4435122 )]
17
# [(1.20789779, 2.36776781) (1.49825784, 1.9457655 )
18
# (1.78861789, 2.83434415) (2.07897793, 5.78165674)]
19
# [(2.36933798, 3.14842606) (2.95005807, 2.10357308)
20
# (3.24041812, 1.15985966) (3.51916376, 2.03056955)]...]
21
22
print(arr.max(axis=1)) <-- ERROR HERE
23
print(max(arr,key=lambda x:x[1])) <-- ERROR, tried this too but doesn't work
24
Expected output I want from each window/blocks using max y-value is below. Alternatively format could be regular list of tuples, doesn’t strictly need to be numpy array:
JavaScript
1
6
1
[[(0.05807201, 9.97201252)]
2
[(2.07897793, 5.78165674)]
3
[(2.36933798, 3.14842606)] ]
4
OR other format:
5
[(0.05807201, 9.97201252),(2.07897793, 5.78165674),(2.36933798, 3.14842606)] ]
6
Advertisement
Answer
Here is a vectorized (and probably fastest) approach:
JavaScript
1
5
1
arr = np.asarray(listTuples, np.dtype('float,float'))
2
idx = arr.view(('float',2))[:,1].reshape(-1,4).argmax(1)
3
arr = arr.reshape(-1,4)[np.arange(len(idx)),idx]
4
#[(0.05807201, 9.9720125) (2.07897793, 5.7816567) ...]
5
You basically use a array (non-structured) version of your structured array by view
and find argmax of Y along axis=1
. Then use those indices to filter the tuples from your original array arr
.