Вот один способ с помощью masking
получить оба значения: row
и col
-
def triu_elements(a):
n = len(a)
r1 = np.broadcast_to(a,(n,n))
r2 = np.broadcast_to(a[:,None],(n,n))
mask = ~np.tri(n,k=-1,dtype=bool)
return r2[mask],r1[mask]
Пример выполнения -
In [56]: myIncidents = np.array([9,7,6])
In [57]: triu_elements(myIncidents)
Out[57]: (array([9, 9, 9, 7, 7, 6]), array([9, 7, 6, 7, 6, 6]))
Время: для различных наборов данных
Сравнение решений @Paul Panzer с np.triu_indices
здесь.
Set # 1 (Small):
In [105]: Incidents = np.random.randint(0,100,(100))
# @Paul Panzer's solution-1
In [106]: %%timeit
...: rowID,colID = np.triu_indices(len(Incidents))
...: row,col = Incidents[rowID],Incidents[colID]
10000 loops, best of 3: 66.8 µs per loop
# @Paul Panzer's solution-2
In [116]: %timeit np.fromiter(it.chain.from_iterable(it.combinations_with_replacement(Incidents,2)),int).reshape(2,-1,order="F")
1000 loops, best of 3: 259 µs per loop
In [107]: %timeit triu_elements(Incidents)
10000 loops, best of 3: 38.3 µs per loop
Set # 2 (Large):
In [99]: Incidents = np.random.randint(0,100,(1000))
In [100]: %%timeit
...: rowID,colID = np.triu_indices(len(Incidents))
...: row,col = Incidents[rowID],Incidents[colID]
100 loops, best of 3: 6.24 ms per loop
In [101]: %timeit triu_elements(Incidents)
1000 loops, best of 3: 1.7 ms per loop
Комплект № 3 (очень большой):
In [121]: Incidents = np.random.randint(0,100,(10000))
In [122]: %%timeit
...: rowID,colID = np.triu_indices(len(Incidents))
...: row,col = Incidents[rowID],Incidents[colID]
1 loop, best of 3: 1.08 s per loop
In [123]: %timeit triu_elements(Incidents)
1 loop, best of 3: 421 ms per loop