darekarsam - 1 year ago 53

Python Question

I have a

`numpy`

`M*N`

`float`

Input: for simplicity purpose lets consider a 3*4 array:

`a=np.array([`

[0.1, 0.2, 0.3, 0.6],

[0.3, 0.4, 0.8, 0.7],

[0.5, 0.6, 0.2, 0.1]

])

I want to consider 3 columns at a time (say col

`0,1,2`

`1,2,3`

In this case I should get max value of

`0.5*0.6*0.8=0.24`

`(2,2,1)`

Output:

`[[0.24,(2,2,1)],[0.336,(2,1,1)]]`

I can do this using loops but I want to avoid them as it would affect running time, is there anyway I can do that in

`numpy`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Here's an approach using `NumPy strides`

that is supposedly very efficient for such sliding windowed operations as it `creates a view into the array`

without actually making copies -

```
N = 3 # Window size
m,n = a.strides
p,q = a.shape
a3D = np.lib.stride_tricks.as_strided(a,shape=(p, q-N +1, N),strides=(m,n,n))
out1 = a3D.argmax(0)
out2 = a3D.max(0).prod(1)
```

Sample run -

```
In [69]: a
Out[69]:
array([[ 0.1, 0.2, 0.3, 0.6],
[ 0.3, 0.4, 0.8, 0.7],
[ 0.5, 0.6, 0.2, 0.1]])
In [70]: out1
Out[70]:
array([[2, 2, 1],
[2, 1, 1]])
In [71]: out2
Out[71]: array([ 0.24 , 0.336])
```

We can zip those two outputs together if needed in that format -

```
In [75]: zip(out2,map(tuple,out1))
Out[75]: [(0.23999999999999999, (2, 2, 1)), (0.33599999999999997, (2, 1, 1))]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**