Skip to content

Commit 8d97a02

Browse files
author
Jouni Helske
committed
improved numerical stability with exp
1 parent 0cd4685 commit 8d97a02

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

hdbscan/_prediction_utils.pyx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ cpdef np.ndarray[np.float64_t, ndim=1] outlier_membership_vector(neighbor,
225225
if softmax:
226226
result = per_cluster_scores(neighbor, lambda_, clusters, tree,
227227
max_lambda_dict, cluster_tree)
228-
result = np.exp(result)
229-
result[~np.isfinite(result)] = np.finfo(np.double).max
228+
# Scale for numerical stability, mathematically equivalent with old
229+
# version due to the scaling with the sum in below.
230+
result = np.exp(result - np.nanmax(result))
231+
#result[~np.isfinite(result)] = np.finfo(np.double).max
230232
else:
231233
result = per_cluster_scores(neighbor, lambda_, clusters, tree,
232234
max_lambda_dict, cluster_tree)
@@ -310,8 +312,10 @@ cpdef np.ndarray[np.float64_t, ndim=2] all_points_outlier_membership_vector(
310312
max_lambda_dict,
311313
cluster_tree)
312314
if softmax:
313-
result = np.exp(per_cluster_scores)
314-
result[~np.isfinite(result)] = np.finfo(np.double).max
315+
# Scale for numerical stability, mathematically equivalent with old
316+
# version due to the scaling with the sum in below.
317+
result = np.exp(per_cluster_scores - np.nanmax(per_cluster_scores))
318+
#result[~np.isfinite(result)] = np.finfo(np.double).max
315319
else:
316320
result = per_cluster_scores
317321

@@ -354,4 +358,3 @@ cpdef all_points_prob_in_some_cluster(
354358
result[point] = (heights.max() / max_lambda)
355359

356360
return result
357-

0 commit comments

Comments
 (0)