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!!