python - Check that all rows in numpy array are unique -
i have 4 column array:
a=array([[100,1,500,1], [100,1,501,1], [101,1,501,1], [102,2,502,2], [500,1,100,1], [100,1,500,1], [502,2,102,2], [502,1,102,1]]) i want extract rows unique (or first occurrence) , rows such row i, there no other rows j in array a[i,:]==a[j,[2,1,0,3]] (or first occurrence).
so array a, array looks like:
b=array([[100,1,500,1], [100,1,501,1], [101,1,501,1], [102,2,502,2], [502,1,102,1]]) thank help!
a[np.unique(np.sort(a,1).view("int, int, int, int"), return_index=true)[1]] in steps:
in [385]: out[385]: array([[100, 1, 500, 1], [100, 1, 501, 1], [101, 1, 501, 1], [102, 2, 502, 2], [500, 1, 100, 1], [100, 1, 500, 1], [502, 2, 102, 2], [502, 1, 102, 1]]) we can eliminate need swapping columns 0 , 2 (the thing a[i] = a[j, [2,1,0,3]) sorting each row. don't have worry swapping columns 1 , 3, since rows in a, have column 1 equals column 3: a[:, 1] == a[:, 3].
in [386]: = np.sort(a,1) in [387]: out[387]: array([[ 1, 1, 100, 500], [ 1, 1, 100, 501], [ 1, 1, 101, 501], [ 2, 2, 102, 502], [ 1, 1, 100, 500], [ 1, 1, 100, 500], [ 2, 2, 102, 502], [ 1, 1, 102, 502]]) find unique rows in as (the sorted array). view structured array each row single element (since np.unique otherwise flatten array first)
in [388]: as.view('int, int, int, int') out[388]: array([[(1, 1, 100, 500)], [(1, 1, 100, 501)], [(1, 1, 101, 501)], [(2, 2, 102, 502)], [(1, 1, 100, 500)], [(1, 1, 100, 500)], [(2, 2, 102, 502)], [(1, 1, 102, 502)]], dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<i8'), ('f3', '<i8')]) in [389]: u, = np.unique(as.view('int, int, int, int'), return_index=true) in [390]: out[390]: array([0, 1, 2, 7, 3]) and use them rows unique in as original array a:
in [391]: a[i] out[391]: array([[100, 1, 500, 1], [100, 1, 501, 1], [101, 1, 501, 1], [502, 1, 102, 1], [102, 2, 502, 2]])
Comments
Post a Comment