2016-11-18 62 views
2

考慮陣列a累計argmax

np.random.seed([3,1415]) 
a = np.random.randint(0, 10, (10, 2)) 
a 

array([[0, 2], 
     [7, 3], 
     [8, 7], 
     [0, 6], 
     [8, 6], 
     [0, 2], 
     [0, 4], 
     [9, 7], 
     [3, 2], 
     [4, 3]]) 

什麼是量化的方式來獲得累計argmax?

array([[0, 0], <-- both start off as max position 
     [1, 1], <-- 7 > 0 so 1st col = 1, 3 > 2 2nd col = 1 
     [2, 2], <-- 8 > 7 1st col = 2, 7 > 3 2nd col = 2 
     [2, 2], <-- 0 < 8 1st col stays the same, 6 < 7 2nd col stays the same 
     [2, 2], 
     [2, 2], 
     [2, 2], 
     [7, 2], <-- 9 is new max of 2nd col, argmax is now 7 
     [7, 2], 
     [7, 2]]) 

這裏是一個非量化的方式來做到這一點。

請注意,隨着窗口的擴展,argmax適用於不斷增長的窗口。

pd.DataFrame(a).expanding().apply(np.argmax).astype(int).values 

array([[0, 0], 
     [1, 1], 
     [2, 2], 
     [2, 2], 
     [2, 2], 
     [2, 2], 
     [2, 2], 
     [7, 2], 
     [7, 2], 
     [7, 2]]) 

回答

3

提到下面是執行相當沒好氣向量化純NumPy的溶液:

def cumargmax(a): 
    m = np.maximum.accumulate(a) 
    x = np.repeat(np.arange(a.shape[0])[:, None], a.shape[1], axis=1) 
    x[1:] *= m[:-1] < m[1:] 
    np.maximum.accumulate(x, axis=0, out=x) 
    return x 

然後我們有:

>>> cumargmax(a) 
array([[0, 0], 
     [1, 1], 
     [2, 2], 
     [2, 2], 
     [2, 2], 
     [2, 2], 
     [2, 2], 
     [7, 2], 
     [7, 2], 
     [7, 2]]) 

對具有數千到數百萬個值的數組進行一些快速測試表明,這比在Python級別循環(隱式或顯式)快10-50倍。

+0

這是我的要求http://stackoverflow.com/a/40680265/2336654 – piRSquared

1

我不能想到一種方法來向兩個列輕鬆地向量化這種方法;但是,如果列數相對於行數,這不應該是一個問題,一個for循環應該能滿足該軸小:

import numpy as np 
import numpy_indexed as npi 
a = np.random.randint(0, 10, (10)) 
max = np.maximum.accumulate(a) 
idx = npi.indices(a, max) 
print(idx) 
1

我想提出一個計算累計argmax功能對於1d數組,然後將其應用於所有列。這是代碼:

import numpy as np 

np.random.seed([3,1415]) 
a = np.random.randint(0, 10, (10, 2)) 

def cumargmax(v): 
    uargmax = np.frompyfunc(lambda i, j: j if v[j] > v[i] else i, 2, 1) 
    return uargmax.accumulate(np.arange(0, len(v)), 0, dtype=np.object).astype(v.dtype) 

np.apply_along_axis(cumargmax, 0, a) 

其原因轉換爲np.object然後轉換回爲numpy的1.9一種解決方法,如在generalized cumulative functions in NumPy/SciPy?

+1

請注意,frompyfunc僅向量化語法;不是表現。這將具有與天真的python循環相當的性能。 –