1
from numba import jit

@jit
def interim_mk(x, unique_x):
    """

    :param x:
    :param unique_x:
    :return:
    """
    tp = np.zeros(unique_x.shape)

    for i in range(len(unique_x)):
        tp[i] = sum(x == unique_x[i])

    return tp

In the function above, I used jit to try and speeden it up. However, it does not seem to help. Both x and unique_x are numpy arrays, is there a way to speeden up this calculation (without using cython)

3
  • Is unique_x = np.unique(x)? Commented Dec 11, 2017 at 0:13
  • @Divakar, yes it is Commented Dec 11, 2017 at 0:13
  • For more meaningful answers, it would be helpful to clarify the intent of your function. Commented Dec 11, 2017 at 0:21

1 Answer 1

2

For case with positive numbers

You can use np.bincount -

count = np.bincount(x)
out = count[count!=0]

Using unique_x -

out = np.bincount(x)[unique_x]

For generic case

out = np.bincount(np.searchsorted(unique_x, x))

Of course, we could have gotten the counts directly from np.unique call, if that's how we had unique_x -

out = np.unique(x, return_counts=1)[1]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.