rsokl / Learning_Python

Source material for Python Like You Mean it
https://www.pythonlikeyoumeanit.com/
Other
163 stars 54 forks source link

Add clipping to pairwise-distt #164

Closed rsokl closed 3 years ago

rsokl commented 3 years ago

Computing pairwise_dists(x, x) can lead to nans because floating point may produce negative square-distance values.

def pairwise_dists(x, y):
    """ Computing pairwise distances using memory-efficient
        vectorization.

        Parameters
        ----------
        x : numpy.ndarray, shape=(M, D)
        y : numpy.ndarray, shape=(N, D)

        Returns
        -------
        numpy.ndarray, shape=(M, N)
            The Euclidean distance between each pair of
            rows between `x` and `y`."""
    dists = -2 * np.matmul(x, y.T)
    dists +=  np.sum(x**2, axis=1)[:, np.newaxis]
    dists += np.sum(y**2, axis=1)
    return  np.sqrt(np.clip(dists, 0, None))
rsokl commented 3 years ago

Closed by #165