I have a symmetric similarity matrix and I want to keep only the k largest value in each row.
Here's some code that does exactly what I want, but I'm wondering if there's a better way. Particularly the flatten/reshape is a bit clumsy. Thanks in advance.
Note that nrows (below) will have to scale into the tens of thousands.
from scipy.spatial.distance import pdist, squareform
random.seed(1)
nrows = 4
a = (random.rand(nrows,nrows))
# Generate a symmetric similarity matrix
s = 1-squareform( pdist( a, 'cosine' ) )
print "Start with:\n", s
# Generate the sorted indices
ss = argsort(s.view(np.ndarray), axis=1)[:,::-1]
s2 = ss + (arange(ss.shape[0])*ss.shape[1])[:,None]
# Zero-out after k-largest-value entries in each row
k = 3 # Number of top-values to keep, per row
s = s.flatten()
s[s2[:,k:].flatten()] = 0
print "Desired output:\n", s.reshape(nrows,nrows)
Gives:
Start with:
[[ 1. 0.61103296 0.82177072 0.92487807]
[ 0.61103296 1. 0.94246304 0.7212526 ]
[ 0.82177072 0.94246304 1. 0.87247418]
[ 0.92487807 0.7212526 0.87247418 1. ]]
Desired output:
[[ 1. 0. 0.82177072 0.92487807]
[ 0. 1. 0.94246304 0.7212526 ]
[ 0. 0.94246304 1. 0.87247418]
[ 0.92487807 0. 0.87247418 1. ]]
Not a considerable improvement, but to avoid the flatten and reshape you can use np.put
:
# Generate the sorted indices
ss = np.argsort(s.view(np.ndarray), axis=1)[:,::-1]
ss += (np.arange(ss.shape[0])*ss.shape[1])[:,None] #Add in place, probably trivial improvement
k=3
np.put(s,ss[:,k:],0) #or s.flat[ss[:,k:]]=0
print s
[[ 1. 0. 0.82177072 0.92487807]
[ 0. 1. 0.94246304 0.7212526 ]
[ 0. 0.94246304 1. 0.87247418]
[ 0.92487807 0. 0.87247418 1. ]]
If you find yourself generating long lists of indices into an array, there is a good chance that it can be solved in more elegant way using boolean matrices. In your case:
a = np.random.rand(5, 5)
a = a + a.T # make it symmetrical
sort_idx = np.argsort(np.argsort(a, axis=1), axis=1)
k = 3 # values to keep
# if you want a copy of the original
mask = (sort_idx >= a.shape[1] - k) # positions we want to keep
b = np.zeros_like(a)
b[mask] = a[mask]
# if you wantrd to do the operation in-place
# mask = (sort_idx < a.shape[1] - k) # positions we want to zero
# a[mask] = 0
>>> a
array([[ 1.87816548, 0.86562424, 1.94171234, 0.96565312, 0.53451029],
[ 0.86562424, 1.13762348, 1.48565754, 0.78031763, 0.51448499],
[ 1.94171234, 1.48565754, 1.39960519, 0.57456214, 1.32608456],
[ 0.96565312, 0.78031763, 0.57456214, 1.56469221, 0.74632264],
[ 0.53451029, 0.51448499, 1.32608456, 0.74632264, 0.55378676]])
>>> b
array([[ 1.87816548, 0. , 1.94171234, 0.96565312, 0. ],
[ 0.86562424, 1.13762348, 1.48565754, 0. , 0. ],
[ 1.94171234, 1.48565754, 1.39960519, 0. , 0. ],
[ 0.96565312, 0.78031763, 0. , 1.56469221, 0. ],
[ 0. , 0. , 1.32608456, 0.74632264, 0.55378676]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.