Consider the following code that generates the following dst
matrix.
tmp = pd.DataFrame()
tmp['a'] = np.random.randint(1, 10, 5)
tmp['b'] = np.random.randint(1, 10, 5)
dst = pairwise_distances(tmp, tmp, metric='l2')
dst
which looks like the following
array([[0. , 5.38516481, 5. , 4.12310563, 2. ],
[5.38516481, 0. , 1.41421356, 3.16227766, 5. ],
[5. , 1.41421356, 0. , 4. , 4.12310563],
[4.12310563, 3.16227766, 4. , 0. , 5. ],
[2. , 5. , 4.12310563, 5. , 0. ]])
Now, I want to somehow get 4
as an output column, because for row=0 and col=4
lies the minimum distance of row0
to another row apart from itself. I'm trying to use the following code to do the job! but np.nonzeros()
is messing up the game. np.argmin(dst[0, np.nonzero(dst[0,:])])
I'm getting 3
as an output, where I should be getting 4
. I understand that np.nonzero()
return another set of dimensions [1,2,3,4]
of which argmin
picks 3rd
column which is actual 4th
column of the dst
matrix. Need help! Thanks in advance!!
Instead of argmin
, use np.min
and compare the result to dst[0,:]
. Finally, pass it to np.flatnonzero
or np.nonzero
np.flatnonzero(np.min(dst[0,np.nonzero(dst[0,:])]) == dst[0,:])
Out[150]: array([4], dtype=int64)
Or
np.nonzero(np.min(dst[0,np.nonzero(dst[0,:])]) == dst[0,:])[0]
Out[151]: array([4], dtype=int64)
If you want to return an integer index, you may use np.argmax
at the last step
np.argmax(np.min(dst[0,np.nonzero(dst[0,:])]) == dst[0,:])
Out[157]: 4
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.