Skip to content

WIP: Refactor euclidean_distance, add optional argument for preallocated output array. #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed

WIP: Refactor euclidean_distance, add optional argument for preallocated output array. #483

wants to merge 9 commits into from

Conversation

dwf
Copy link
Member

@dwf dwf commented Dec 19, 2011

This pull request adds

  • Several optimized Cython routines for (squared) Euclidean distance, now used in the dense-dense case by euclidean_distance.
  • An explicit check that euclidean_distance is operating on floating point arrays (general consensus in the sprint room is that it doesn't usually make much sense in the integer case and if you really want that, you can cast).
  • An optional out argument for euclidean_distance (only in the dense-dense case; raises an error otherwise).

----------
X : ndarray, float32, shape = [n_samples_a, n_features]

Y : ndarray, float32, shape = [n_sample_b, n_features]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: n_sampleS_b

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed.

@dwf
Copy link
Member Author

dwf commented Dec 19, 2011

It seems that under certain circumstances, using BLAS might be faster, but loses precision in certain cases. @jakevdp has done some benchmarking.

In an ideal world, these Cython functions should try and guess whether the BLAS call is worth it and do it.

@GaelVaroquaux
Copy link
Member

Code to bench (create a '.ipy' file and run it in IPython):

import timeit

from sklearn.metrics.pairwise import euclidean_distances

import numpy as np
np.random.seed(0)

for n_features in (10, 100, 1000):
    for n_samples in (10, 100, 1000):
    Y = np.random.random(size=(n_features, n_samples))
    X = np.random.random(size=(n_features, n_samples))

    print 'n_features: % 2i, n_samples % 2i' % (n_features, n_samples)
    %timeit euclidean_distances(X, Y)

@fabianp
Copy link
Member

fabianp commented Dec 20, 2011

another implementation I found in milk: https://github.com/luispedro/milk/blob/master/milk/unsupervised/pdist.py

@dwf dwf closed this Jul 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants