numpy - Python - Find K max values in each row of one matrix and compare to binary matrix -
i need determine if position (index) of k largest values in matrix in same position binary indicator matrix, b.
import numpy np = np.matrix([[.8,.2,.6,.4],[.9,.3,.8,.6],[.2,.6,.8,.4],[.3,.3,.1,.8]]) b = np.matrix([[1,0,0,1],[1,0,1,1],[1,1,1,0],[1,0,0,1]]) print "a:\n", print "b:\n", b d = argsort(a) d[:,2:] # return whether these indices in 'b'
returns:
a: [[ 0.8 0.2 0.6 0.4] [ 0.9 0.3 0.8 0.6] [ 0.2 0.6 0.8 0.4] [ 0.3 0.3 0.1 0.8]] b: [[1 0 0 1] [1 0 1 1] [1 1 1 0] [1 0 0 1]] matrix([[2, 0], [2, 0], [1, 2], [1, 3]])
i compare indices returned last result and, if b
has ones in positions, return count. example, final desired result be:
1 2 2 1
in other words, in first row of a
, top-2 values correspond 1 of ones in b
, etc.
any ideas how efficiently? maybe argsort wrong approach here. thanks.
when take argsort
minimum 0
maximum 3
, can reverse doing [::-1]
maximum 0
, minimum 3
:
s = np.argsort(a, axis=1)[:,::-1] #array([[0, 2, 3, 1], # [0, 2, 3, 1], # [2, 1, 3, 0], # [3, 1, 0, 2]])
now can use np.take
0
s maximums , 1
s second-maximums are:
s2 = s + (np.arange(s.shape[0])*s.shape[1])[:,none] s = np.take(s.flatten(),s2) #array([[0, 3, 1, 2], # [0, 3, 1, 2], # [3, 1, 0, 2], # [2, 1, 3, 0]])
in b
, 0
values should replaced np.nan
0==np.nan
gives false
:
b = np.float_(b) b[b==0] = np.nan #array([[ 1., nan, nan, 1.], # [ 1., nan, 1., 1.], # [ 1., 1., 1., nan], # [ 1., nan, nan, 1.]])
and following comparison give desired result:
print np.logical_or(s==b-1, s==b).sum(axis=1) #[[1] # [2] # [2] # [1]]
the general case, compare n
biggest values of a
against binary b
:
def check_a_b(a,b,n=2): b = np.float_(b) b[b==0] = np.nan s = np.argsort(a, axis=1)[:,::-1] s2 = s + (np.arange(s.shape[0])*s.shape[1])[:,none] s = np.take(s.flatten(),s2) ans = s==(b-1) in range(n-1): ans = np.logical_or( ans, s==b+i ) return ans.sum(axis=1)
this pair-wise comparisons in logical_or
.
Comments
Post a Comment