Code
Fill the distance to the nearest True position in a column.
from scipy.spatial import KDTree
array = df.to_numpy()
bmp = array.astype(np.uint8)
distance = []
for points in bmp.T:
all_points = np.argwhere(points!=2)
true_points = np.argwhere(points==1)
tree = KDTree(true_points)
dist = tree.query(all_points, k=1, p=2)[0]
distance.append(dist)
distance = np.array(distance).astype(int).T
df[df.columns + "_dist"] = distance
Output
Var1 Var2 Var3 Var1_dist Var2_dist Var3_dist
idx
0 True False False 0 1 2
1 False True False 1 0 1
2 True False True 0 1 0
3 False False False 1 2 1
4 True False True 0 3 0
Fill the distance to the nearest True position in the whole table.
from scipy.spatial import KDTree
array = df.to_numpy()
bmp = array.astype(np.uint8)
all_points = np.argwhere(bmp!=2)
true_points = np.argwhere(bmp==1)
tree = KDTree(true_points)
distance = tree.query(all_points, k=1, p=1)[0]
distance.resize(array.shape)
df[df.columns + "_dist"] = distance.astype(int)
Output
Var1 Var2 Var3 Var1_dist Var2_dist Var3_dist
idx
0 True False False 0 1 2
1 False True False 1 0 1
2 True False True 0 1 0
3 False False False 1 2 1
4 True False True 0 1 0
Explain
- Using
np.array to make 0,1 data
array([[1, 0, 0],
[0, 1, 0],
[1, 0, 1],
[0, 0, 0],
[1, 0, 1]], dtype=uint8)
argwhere will return the position coordinate for eligible points.
KDTree is a classical algorithm to find the nearest point.
arg k means the top n nearest points
arg p=1 means "Manhattan" distance
Which Minkowski p-norm to use.
1 is the sum-of-absolute-values distance ("Manhattan" distance).
2 is the usual Euclidean distance.
Reference
scipy.KDTree