@@ -225,8 +225,10 @@ cpdef np.ndarray[np.float64_t, ndim=1] outlier_membership_vector(neighbor,
225
225
if softmax:
226
226
result = per_cluster_scores(neighbor, lambda_, clusters, tree,
227
227
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
230
232
else:
231
233
result = per_cluster_scores(neighbor, lambda_, clusters, tree,
232
234
max_lambda_dict, cluster_tree)
@@ -310,8 +312,10 @@ cpdef np.ndarray[np.float64_t, ndim=2] all_points_outlier_membership_vector(
310
312
max_lambda_dict,
311
313
cluster_tree)
312
314
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
315
319
else:
316
320
result = per_cluster_scores
317
321
@@ -354,4 +358,3 @@ cpdef all_points_prob_in_some_cluster(
354
358
result[point] = (heights.max() / max_lambda)
355
359
356
360
return result
357
-
0 commit comments