darekarsam darekarsam - 1 month ago 7
Python Question

get max value of multiplication of column combinations and their respective index in python

I have a

numpy
array of
M*N
dimensions in which each element of the array is a
float
with a value between 0-1.

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
for first iteration and
1,2,3
for second) and get the maximum value of multiplication of all possible combinations of the 3 columns and also get the index of their respective values.

In this case I should get max value of
0.5*0.6*0.8=0.24
and the index of the rows of values that gave the max value:
(2,2,1)
in this case.

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
?

Answer

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))]