2
\$\begingroup\$

I have the following metric:

def d(x, y):
    """The metric."""
    return 1 - sum(min(xi, yi) for xi, yi in zip(x, y))

where x and y are numpy arrays of equal length (~3000). I know that the elements of x and the elements of y each sum up to 1 and are non-negative.

Can this be calculated faster?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Minimum calculation could be vectorized:

import numpy as np

def f(x, y):
    return 1 - np.minimum(x, y).sum()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.