How to access a specific item on the most similar row with euclidean distance?

Viewed 41

I have a list L of arrays:

L = ( array1,
      array2,
      array3,
      array4... )

This list is created starting from a Pandas data frame, thus, every single row of the old data frame is now an array inside a list.

Using Euclidean Distance I want to:

  1. Iterate through all the arrays
  2. Select only arrays in which the 2nd item is included between 10 and 100
  3. Given an array arr1, select the most similar one (let's say arr4)
  4. Replace the 2nd item in arr4 with 2nd item in arr1

To visualize it:

  1. Iterate through all the arrays

     L = ([87, 30,  45,  99],
         [11,  21,  31,  41],
         [560, 47,  85,  328],
         [167, 32,  98,  379] )
    
  2. Select only arrays in which the 2nd item is included between 10 and 100

    L = ([87,  30,  45,  99],
         [11,  21,  31,  41],
         [560, 47,  85,  328],
         [85,  33,  43,  97] )
    
  3. Given an array arr1, select the most similar one (let's say arr4)

    array 1 = ( [87, 30,  45,  99] )
    
    array 4 = ( [85, 32,  43,  97] )
    

    Now let's call for simplicity:

    P = 30 (array 1, 2nd element)

    X = 32 (array 4, 2nd element)

  4. Replace the 2nd item in arr4(P) inside arr1 2nd position (X)

    array 1 = ([87, 32, 45, 99])
    

Many thank and advance for whatever hint that can be useful!

1 Answers
import numpy as np

L = np.array([
  [87, 30,  45,  99],
  [11,  21,  31,  41],
  [560, 47,  85,  328],
  [167, 32,  98,  379]
])

For point 2 you can use np.where which return an array of condition.nonzero() that indicates where condition is True. Then you can put this array as index.


result = L[np.where((L[:,1]>10)&(L[:,1]<100))]

To given array e.g arr1, we can use following method (I don't know if it's best, but it works).

L2 = np.array([
  [87,  30,  45,  99],
  [11,  21,  31,  41],
  [560, 47,  85,  328],
  [85,  33,  43,  97]
])

def dist(a):
  return np.linalg.norm(L2[0]-a)

distances = np.apply_along_axis(dist, 1, L2)
index = np.ma.MaskedArray(result, result==0).argmin() #remove first element which dist is min (0)
print(L2[index])
Related