[PYTHON] Find a position above the threshold with NumPy

Find index with value> = threshold for ascending data with NumPy

Method

  1. argmin of "np.array <th" --Searchsorted for "np.array> = th" --Length of "np.array <th" --Sum of "np.array <th" --Where of "np.array> = th" --Search by for loop --Length of "takewhile"

Confirm that the result is correct in 7 ways

python


import numpy as np
from more_itertools import ilen
from itertools import takewhile

def find_index(a, ths):
    for i,v in enumerate(a):
        if v >= th:
            break
    return i

n = 10000000
x = np.arange(n)
th = n / 10
print((x < th).argmin())
print(np.searchsorted(x, th))
print(len(x[x < th]))
print((x < th).sum())
print(np.where(x>=th)[0][0])
print(find_index(x, th))
print(ilen(takewhile(lambda i: i < th, x)))
>>>
1000000
1000000
1000000
1000000
1000000
1000000
1000000

measurement

python


%timeit np.searchsorted(x, th)
%timeit (x < th).argmin()
%timeit len(x[x < th])
%timeit (x < th).sum()
%timeit np.where(x >= th)[0][0]
%timeit find_index(x, th)
%timeit ilen(takewhile(lambda i: i < th, x))
>>>
3.84 µs ± 347 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
10.2 ms ± 215 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
12.5 ms ± 62.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
21.2 ms ± 411 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
43.1 ms ± 635 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
3.36 µs ± 19.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
5.74 µs ± 75.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

python


th = 10
%timeit np.searchsorted(x, th)
%timeit (x < th).argmin()
%timeit len(x[x < th])
%timeit (x < th).sum()
%timeit np.where(x >= th)[0][0]
%timeit find_index(x, th)
%timeit ilen(takewhile(lambda i: i < th, x))
>>>
3.31 µs ± 24.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
9.86 ms ± 28.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
13.1 ms ± 530 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
21.7 ms ± 761 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
46.9 ms ± 1.8 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
3.76 µs ± 310 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
6.01 µs ± 231 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Consideration

--search sorted is good --If it's an argmin and a for loop, it doesn't have to be sorted

that's all

Recommended Posts

Find a position above the threshold with NumPy
Find the smallest index that meets the cumulative sum threshold with numpy
Draw a beautiful circle with numpy
Find the Levenshtein Distance with python
Find the optimal value of a function with a genetic algorithm (Part 2)
Tweet the weather forecast with a bot
Find the inertial spindle and moment of inertia from the inertial tensor with NumPy
Find the dates for a jarring tournament
Find the SHA256 value with R (with bonus)
Search the maze with the python A * algorithm
Display markers above the border with matplotlib
I made a life game with Numpy
Read a character data file with numpy
A model that identifies the guitar with fast.ai
[Python] Get the files in a folder with Python
Tweet the weather forecast with a bot Part 2
Find the mood value with python (Rike Koi)
Follow the AR marker with a 2-axis servo
Find the number of days in a month
Save the object to a file with pickle
Find out the day of the week with datetime
[Python] Find the transposed matrix in a comprehension
I made a random number graph with Numpy
Find the shortest path with the Python Dijkstra's algorithm
Create a translation tool with the Translate Toolkit
Draw a graph with PyQtGraph Part 5-Increase the Y-axis
Let's transpose the matrix with numpy and multiply the matrices.
Find the sum of unique values with pandas crosstab
How to create a submenu with the [Blender] plugin
Extract elements other than a specific index with Numpy
Take a screenshot of the LCD with Python-LEGO Mindstorms
Create a Todo app with the Django REST framework
Make a breakpoint on the c layer with python
A story that struggled with the common set HTTP_PROXY = ~
Find out the location of packages installed with pip
Fill the background with a single color with OpenCV2 + Python
Visualize the characteristic vocabulary of a document with D3.js
Create a tweet heatmap with the Google Maps API
A memo that I touched the Datastore with python
Transit to the update screen with the Django a tag
Divide the dataset (ndarray) into arbitrary proportions with NumPy
Calculate the product of matrices with a character expression?
Solved the world's smallest hint Sudoku with numpy & numba
Numpy creates a matrix with only the columns whose total values of the columns of the matrix are the top X
Create a 2D array by adding a row to the end of an empty array with numpy