1

I have a numpy.array with several integer values.

For all values from 0 to the maximal value in the array, I want to count how many elements are equal or greater.

This is my current code:

import numpy as np
from random import randint

arr = np.array([randint(0, 10) for _ in range(20)])
val_range = np.arange(arr.max() + 1)
count_array = np.array([(arr >= v).sum() for v in val_range])

Is there a better way implementing this with numpy?

I want to implement this with numpy and later integrate the code in a function compiled with numba.

2 Answers 2

1

You can use ECDF.

If you use the first definition of vals you'll get what you want. But I also give another option - this is a bit different than what you ask, because it only computes the "necessary" numbers while your code computes all numbers between 0 and the maximal value, but many of them are unncessary (especially if your array is smaller than the largest number).

Anyways, if you need to sample in other values than what you have, you can easilly use the ecdf function as it is.

from statsmodels.distributions.empirical_distribution import ECDF

# Generate array
arr = np.array([randint(0, 10) for _ in range(20)])

# Compute ECDF
ecdf = ECDF(arr, side='left')

# what you do in your code
vals = np.arange(np.max(arr) + 1)

# a more efficient way (if relevant) - get points to sample from ECDF
vals = np.unique(arr)
vals.sort()

# Get number of elements equal or greated than from each element
(1-ecdf(vals)) * arr.shape[0]
Sign up to request clarification or add additional context in comments.

1 Comment

This works great, but not with numba, it has a problem compiling ECDF: Untyped global name 'ECDF': Cannot determine Numba type of <class 'type'>
0

I guess this is what you are looking for? This is compatible with numba njit

[np.count_nonzero(arr >= v) for v in val_range]
Out[24]: [20, 17, 15, 15, 14, 12, 8, 6, 4, 2, 2] ## As in my random list

1 Comment

It works and compatible, but it still uses list comprehension, which might be less efficient than a vectorized operation

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.