Skip to content

Commit 6e768d6

Browse files
committed
Fix bug with 0 distances
1 parent 53b8fca commit 6e768d6

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

sica/mutualknn.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ def compute_distance(
100100

101101
if metric in ["pearson", "spearman", "kendall"]:
102102
corr = pd.concat([X, Y], keys=["X", "Y"]).T.corr(method=metric)
103-
return 1 - np.abs((corr.loc["X", "Y"]).values)
103+
distance = 1 - np.abs((corr.loc["X", "Y"]).values)
104104
else:
105-
return cdist(X, Y, metric=metric)
105+
distance = cdist(X, Y, metric=metric)
106+
# we add a small term to deal with 0 distances
107+
return np.where(distance == 0, 10 ** (-5), distance)
106108

107109
def adjacency_matrix(self, weighted: bool) -> Union[np.ndarray, None]:
108110
"""Compute the undirected adjacency matrix with the Mutual Nearest Neighbors method (``k`` neighbors)

0 commit comments

Comments
 (0)