scikit-learn/scikit-learn — pull request #2485 by larsmans (2013-09-30 13:11:33+00:00)
Title: [MRG] sparse matrix support in pairwise_distances_argmin_min + optimizations

Fixes #2417. Additionally, I've refactored the code and optimized the dense Euclidean distance computations for NumPy >= 1.6 using its great new tool [`einsum`](http://docs.scipy.org/doc/numpy-1.7.0/reference/generated/numpy.einsum.html):

```
>>> X = np.random.randn(900, 800)
>>> %timeit (X * X).sum(axis=1)
1000 loops, best of 3: 1.75 ms per loop
>>> %timeit np.einsum('ij,ij->i', X, X)
1000 loops, best of 3: 341 us per loop
```

For larger arrays, the difference gets more extreme. `einsum` also takes much less memory as it doesn't have to allocate a large temporary of shape `X.shape`.