scikit-learn/scikit-learn — pull request #2368 by emsrc (2013-08-18 20:02:36+00:00)
Title: Cosine distance metric for sparse matrices

I wanted to use Cosine as a distance metric in the NearestCentroid classifier, but the existing implementation calls scipy.spatial.distance.cosine, which cannot handle sparse matrices. The same issue started #732. Although a lot of good things resulted from this, it seems the issue that started it -- Cosine distance for sparse matrices -- was never addressed.

As there already is a Cosine similarity metric that works with sparse input (sklearn.metrics.pairwise.cosine_similarity), adding a corresponding distance metric is trivial (simply 1.0 minus Cosine sim).

I incidentally noticed that the doc string for the function 'pairwise_distances' suggests that sparse matrices are supported for all metrics in pairwise.PAIRWISE_DISTANCE_FUNCTIONS. However, all metrics calling on 'manhattan_distances' (i.e. 'cityblock', 'l1' and 'manhattan') raise an exception with sparse input. So I took the liberty of correcting that doc string too.

To sum up the changes:
- Added 'cosine_distances' function to sklearn.metrics.pairwise.
- Added 'cosine' as metric in 'pairwise_distances' function.
- Corrected doc string of same function, because all metrics based on
the 'manhattan_distances' function (i.e. 'cityblock', 'l1', and
'manhattan') do currently NOT support sparse matrices.
- Added corresponding corresponding unit test.