numpy - Order of repetition per row and column in Python -
i have been trying figure order of repetition per-row , couldn't it. ok. lets consider ndarray of size (2, 11, 10)
a = np.array([ [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1, 1, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0, 1, 0], [1, 1, 0, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 0], [1, 1, 0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0, 1, 1, 0, 1], [1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 1, 0, 0, 1, 1], [0, 1, 1, 1, 0, 0, 1, 1, 0, 1] ], [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 1, 1], [0, 1, 0, 1, 0, 0, 0, 1, 0, 0], [1, 1, 0, 1, 0, 1, 1, 1, 0, 0], [1, 1, 0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 1, 1, 0, 0], [1, 0, 0, 0, 1, 1, 0, 0, 1, 1], [1, 1, 1, 0, 0, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 0, 0, 1, 0, 0, 0], [1, 1, 1, 0, 0, 1, 1, 1, 0, 1] ] ]) what wanted order of every 1's per row based on column. whenever first 1 found in row order starts start @ 0; goes second row if 1 found here order 1, if 1 present @ column index in previous row, ignored. example
lets consider these lists:
0 1 2 3 4 5 6 7 8 9 -> column index 0 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], -> no 1's no order here 1 [1, 1, 0, 0, 0, 1, 1, 1, 0, 0], -> order starts @ 0 2 [0, 1, 0, 0, 0, 1, 0, 0, 1, 0], -> order starts @ 1 at row index 0 there no 1 nothing happens, @ row index 1 there ones in column index [0,1,5,6,7] equal 0; output should be
column order 0 0 1 0 2 - 3 - 4 - 5 0 6 0 7 0 8 - 9 - at row index 2 there 1 @ column index [1,5,8] whos order 1; in there 1 , 5 ignored because has order 0 it, unknown order should 1; final output should
column order 0 0 1 0 2 - 3 - 4 - 5 0 6 0 7 0 8 1 9 - i have tried using numpy's np.where method index values; this
index = np.asarray(np.where(a == 1)).t i have no idea next. can please me?
apparently, desired result--based on comments on question , earlier version of answer--is find "dense ranking" of row index of first 1 in each column. (see docstring of scipy.stats.rankdata meaning of "dense ranking".) result can found using combination of .argmax() method , scipy.stats.rankdata.
here's function computes order two-dimensional array. question doesn't define should happen when column zeros; order assigns column value -1.
from scipy.stats import rankdata def order(x): result = x.argmax(axis=0) result[(x == 0).all(axis=0)] = -1 rank = rankdata(result, method='dense') - 1 - np.any(result < 0) return rank for example, here array y:
in [71]: y out[71]: array([[0, 1, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0], [1, 1, 1, 0, 1, 1, 0, 0], [1, 0, 1, 1, 1, 1, 0, 0], [1, 0, 0, 0, 1, 1, 1, 0]]) in [72]: order(y) out[72]: array([ 1, 0, 1, 2, 0, 0, 3, -1]) here's array a question:
in [73]: out[73]: array([[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 1, 1, 1, 0, 0], [0, 1, 0, 0, 0, 1, 0, 0, 1, 0], [1, 1, 0, 0, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [1, 0, 0, 1, 0, 1, 1, 1, 0, 0], [1, 1, 0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 1, 0, 0, 1, 1, 0, 1], [1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 1, 0, 0, 1, 1], [0, 1, 1, 1, 0, 0, 1, 1, 0, 1]], [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 1, 1], [0, 1, 0, 1, 0, 0, 0, 1, 0, 0], [1, 1, 0, 1, 0, 1, 1, 1, 0, 0], [1, 1, 0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 1, 1, 0, 0], [1, 0, 0, 0, 1, 1, 0, 0, 1, 1], [1, 1, 1, 0, 0, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 0, 0, 1, 0, 0, 0], [1, 1, 1, 0, 0, 1, 1, 1, 0, 1]]]) the function order() expects two-dimensional array, must use loop order each subarray in a:
in [74]: np.array([order(m) m in a]) out[74]: array([[0, 0, 3, 3, 2, 0, 0, 0, 1, 4], [2, 0, 3, 1, 0, 2, 2, 1, 0, 0]])
Comments
Post a Comment