pythonnumpy# How to find the threshold that will yield the desired number of array elements

Given an array of numbers and a target count, I want to find the threshold such that the number of element that are above it will be equal the target (or as close as possible).

For example.

```
arr = np.random.rand(100)
target = 80
for i in range(100):
t = i * 0.01
if (arr > t).sum() < target: break
print(t)
```

However this is not efficient and it is not very precise, and perhaps someone has already solved this problem.

**EDIT:**

In the end I found `scipy.optimize.bisect`

(link) which works perfectly.

Solution

I am using `scipy.optimize.bisect`

```
import scipy.optimize
import numpy as np
def compute_cutoff(arr:np.ndarray, volume:float) -> float:
"""
Compute the cutoff to attain desired volume.
Returns the cutoff, such that (arr > cutoff).sum() == volume
or as close as possible.
:param np.ndarray arr: an array with values (0 .. 1)
:param float volume: desired volume in number of voxels
"""
tolerance = max(1,volume*.01)
gross_diff = lambda y: y - volume if abs(y - volume) > tolerance else 0
err_fn = lambda x : gross_diff((arr > x).sum())
# probably this [-2,2] could be tighter [0,1], but just to be safe.
return scipy.optimize.bisect(err_fn, -2, 2, maxiter=50, disp=False)
arr = np.random.rand(100)
t = compute_cutoff(arr, 80)
print(t)
```

This prints a value close to 0.2

- Python Jinja2 LaTeX Table
- Getting attributes of a class
- How can I print many significant figures in Python?
- How to allow list append() method to return the new list
- Calculate Last Friday of Month in Pandas
- Python type hint for Iterable[str] that isn't str
- How to iterate over a list in chunks
- How to exit the entire application from a Python thread?
- Running shell command and capturing the output
- How do I pass a variable by reference?
- Convert range(r) to list of strings of length 2 in python
- How can I get the start and end dates for each week?
- how to use send_message() in python-telegram-bot
- Python conditional replacement based on element type
- How can I count the number of items in an arbitrary iterable (such as a generator)?
- Find longest consecutive range of numbers in list
- Insert text in braces with asyncpg
- How does one put a link / url to the web-site's home page in Django?
- How to determine if a path is a subdirectory of another?
- Custom Keybindings for Ipython terminal
- FastAPI asynchronous background tasks blocks other requests?
- How to make sure that information from one file is duplicated into several text documents, without specific lines
- Installing a Python environment with Anaconda
- sklearn pipeline model predicting same results for all input
- Brew command not found after installing Anaconda Python
- How to get an XPath from selenium webelement or from lxml?
- Pipe PuTTY console to Python script
- How to align the axes of a figure in matplotlib?
- Persist ParentDocumentRetriever of langchain
- How to reset index in a pandas dataframe?