Skip to main content
Improved my previous code, (Distance defined here will be the absolute distance for each element in X and Y)
Source Link
DIY-DS
  • 263
  • 1
  • 4
  • 16

May be you could try this using pure NP and self defined function:

import numpy as np #Your Example xDA=np.array([[1.22, 5.64],[2.31, 7.63],[4.94, 4.15]]15],[6.1,6.2]]) yDA=np.array([[1.23, 5.63],[6.31, 10.63],[2.32, 7.65]]65],[3.1,9.2]]) ###Try this large sample### x=xDA#xDA=np.sumround(axis=1np.random.uniform(1,2, size=(5000, 2)),2) y=yDA#yDA=np.sumround(axis=1np.random.uniform(1,2, size=(5000, 2)),2) print(xDA) print(yDA) #Match yx to x y def np_matrix(myx,myy,calp=0.2): Xxx = np.transpose(np.repeat(myx[:, np.newaxis], myy.size, axis=1)) Yyy = np.repeat(myy[:, np.newaxis], myx.size, axis=1)   # define a caliper matches = {} dist = np.abs(Xxx - Yyy) print(dist) for m in range(0, myx.size): if (np.min(dist[:, m]) <= calp) or not calp: matches[m] = np.argmin(dist[:, m]) return matches mymatch=np_matrix alwd_dist=0.1 xc1=xDA[:,1] yc1=yDA[:,1] m1=np_matrix(yxc1,xyc1,0alwd_dist) xc0=xDA[:,0] yc0=yDA[:,0] m0=np_matrix(xc0,yc0,alwd_dist) shared_items = set(m1.04items()) & set(m0.items()) if (int(len(shared_items))==0): print("No Matched Items based on given allowed distance:",alwd_dist) else:  print("Matched:")  for ke in mymatch.keys()shared_items:   print(xDA[ke]xDA[ke[0]],yDA[mymatch[ke]]yDA[ke[1]]) 

May be you could try this using pure NP and self defined function:

import numpy as np xDA=np.array([[1.22, 5.64],[2.31, 7.63],[4.94, 4.15]]) yDA=np.array([[1.23, 5.63],[6.31, 10.63],[2.32, 7.65]]) x=xDA.sum(axis=1) y=yDA.sum(axis=1) #Match y to x  def np_matrix(myx,myy,calp=0.2): Xxx = np.transpose(np.repeat(myx[:, np.newaxis], myy.size, axis=1)) Yyy = np.repeat(myy[:, np.newaxis], myx.size, axis=1) matches = {} dist = np.abs(Xxx - Yyy) print(dist) for m in range(0, myx.size): if (np.min(dist[:, m]) <= calp) or not calp: matches[m] = np.argmin(dist[:, m]) return matches mymatch=np_matrix(y,x,0.04) for ke in mymatch.keys(): print(xDA[ke],yDA[mymatch[ke]]) 

May be you could try this using pure NP and self defined function:

import numpy as np #Your Example xDA=np.array([[1.22, 5.64],[2.31, 7.63],[4.94, 4.15],[6.1,6.2]]) yDA=np.array([[1.23, 5.63],[6.31, 10.63],[2.32, 7.65],[3.1,9.2]]) ###Try this large sample### #xDA=np.round(np.random.uniform(1,2, size=(5000, 2)),2) #yDA=np.round(np.random.uniform(1,2, size=(5000, 2)),2) print(xDA) print(yDA) #Match x to y def np_matrix(myx,myy,calp=0.2): Xxx = np.transpose(np.repeat(myx[:, np.newaxis], myy.size, axis=1)) Yyy = np.repeat(myy[:, np.newaxis], myx.size, axis=1)   # define a caliper matches = {} dist = np.abs(Xxx - Yyy) for m in range(0, myx.size): if (np.min(dist[:, m]) <= calp) or not calp: matches[m] = np.argmin(dist[:, m]) return matches  alwd_dist=0.1 xc1=xDA[:,1] yc1=yDA[:,1] m1=np_matrix(xc1,yc1,alwd_dist) xc0=xDA[:,0] yc0=yDA[:,0] m0=np_matrix(xc0,yc0,alwd_dist) shared_items = set(m1.items()) & set(m0.items()) if (int(len(shared_items))==0): print("No Matched Items based on given allowed distance:",alwd_dist) else:  print("Matched:")  for ke in shared_items:   print(xDA[ke[0]],yDA[ke[1]]) 
Source Link
DIY-DS
  • 263
  • 1
  • 4
  • 16

May be you could try this using pure NP and self defined function:

import numpy as np xDA=np.array([[1.22, 5.64],[2.31, 7.63],[4.94, 4.15]]) yDA=np.array([[1.23, 5.63],[6.31, 10.63],[2.32, 7.65]]) x=xDA.sum(axis=1) y=yDA.sum(axis=1) #Match y to x def np_matrix(myx,myy,calp=0.2): Xxx = np.transpose(np.repeat(myx[:, np.newaxis], myy.size, axis=1)) Yyy = np.repeat(myy[:, np.newaxis], myx.size, axis=1) matches = {} dist = np.abs(Xxx - Yyy) print(dist) for m in range(0, myx.size): if (np.min(dist[:, m]) <= calp) or not calp: matches[m] = np.argmin(dist[:, m]) return matches mymatch=np_matrix(y,x,0.04) for ke in mymatch.keys(): print(xDA[ke],yDA[mymatch[ke]])