Numpy: Find Index of the Elements Within Range

Numpy: find index of the elements within range

You can use np.where to get indices and np.logical_and to set two conditions:

import numpy as np
a = np.array([1, 3, 5, 6, 9, 10, 14, 15, 56])

np.where(np.logical_and(a>=6, a<=10))
# returns (array([3, 4, 5]),)

How to find the index of a list element in a numpy array using ranges

I would do it like this. Initialize output to the same shape. Then just do the comparison on those elements.

out = np.full(arr.shape,False)
out[:,:,0] = arr[:,:,0] >= 5
out[:,:,1] = arr[:,:,1] >= 8

Output:

array([[[False, False],
[ True, False]],

[[ True, True],
[ True, False]]])

EDIT: After our edit I think you just need a final np.all along the last axis:

np.all(out, axis=-1)

Returns:

array([[False, False],
[ True, False]])

Numpy Find Index of Maximum Value inside small range of array

We can do this by first masking out the values where the condition does not hold, and then use argmax, to calculate the index where the second column is the maximum.

So we mask with:

data_masked = np.ma.masked_where((data[:,0] < 5) | (data[:,0] > 9), data[:,1])

So here the condition is the opposite of the filter condition: all rows for which data[:0] < 5 or data[:0] > 9 are masked out. Note that we already make a projection to the second column. The intermediate result is then:

>>> np.ma.masked_where((data[:,0] < 5) | (data[:,0] > 9), data[:,1])
masked_array(data=[--, --, --, --, --, 0.05736691, 0.54063927, 0.3045981,
0.13873822, --, --],
mask=[ True, True, True, True, True, False, False, False,
False, True, True],
fill_value=1e+20)

and then we calculate the index with:

index = np.argmax(b)

Is there a NumPy function to return the first index of something in an array?

Yes, given an array, array, and a value, item to search for, you can use np.where as:

itemindex = numpy.where(array == item)

The result is a tuple with first all the row indices, then all the column indices.

For example, if an array is two dimensions and it contained your item at two locations then

array[itemindex[0][0]][itemindex[1][0]]

would be equal to your item and so would be:

array[itemindex[0][1]][itemindex[1][1]]

How to extract specific part of a numpy array?

You can find the index of the nearest value by creating a new array of the differences between the values in the original array and the target, then find the index of the minimum value in the new array.

For example, starting with an array of random values in the range 5.0 - 10.0:

import numpy as np

x = np.random.uniform(low=5.0, high=10.0, size=(20,))
print(x)

Find the index of the value closest to 8 using:

target = 8

diff_array = np.absolute(x - target)
print(diff_array)

index = diff_array.argmin()
print(index, x[index])

Output:

[7.74605146 8.31130556 7.39744138 7.98543982 7.63140243 8.0526093
7.36218916 6.62080638 6.18071939 6.54172198 5.76584536 8.69961399
5.83097522 9.93261906 8.21888006 7.63466418 6.9092988 9.2193369
5.41356164 5.93828971]

[0.25394854 0.31130556 0.60255862 0.01456018 0.36859757 0.0526093
0.63781084 1.37919362 1.81928061 1.45827802 2.23415464 0.69961399
2.16902478 1.93261906 0.21888006 0.36533582 1.0907012 1.2193369
2.58643836 2.06171029]

3 7.985439815743841

How do I select a range of two numpy indices?

You can simply do this using the traditional start:stop:step convention without any modulo by reshaping your array, indexing, and then flattening it back. Try this -

  1. By reshaping it to (-1,2) you create bi-gram sequence
  2. Then you simply start from 1 and step 2 times
  3. Last you flatten it back.
x.reshape(-1,2)[1::2].flatten()
array([ 2,  3,  6,  7, 10, 11, 14, 15, 18, 19])

This should be significantly faster than approaches where mathematical operations are being used to check each value since this is just reshaping and indexing.

Python Help -- How to extract a specific range of values from a 1D array?

One of the methods is to use a list comprehension
Note: Inclusive of 2, the lower bound but excluding 10, the upper bound

x = [1,0,35,9,1,23,10,2,4,8,3]
y = [c for c in x if c >=2 and c<10]

Since you are using numpy

Method 1:

import numpy as np
y = np.where(np.logical_and(x>=2, x<10))

Method 2:

import numpy as np
x = np.array([1,0,35,9,1,23,10,2,4,8,3])
y = x[(x>=2) * (x<10)]

How to return indices of values between two numbers in numpy array

Since > < = return masked arrays, you can multiply them together to get the effect you are looking for (essentially the logical AND):

>>> import numpy as np
>>> A = 2*np.arange(10)
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])

>>> idx = (A>2)*(A<8)
>>> np.where(idx)
array([2, 3])

numpy get elements by array of ranges

I am not sure why you have a 'null' in one of your intervals. But you can do this using a list comprehension:

import numpy as np
A=np.array([1, 161, 51, 105, 143, 2, 118, 127 , 37, 19, 4 , 29 , 13, 136, 129, 128, 129])

I=[[ 0, 2],
[ 2 ,5],
[ 5 ,8],
[ 8, 14]]

res = [A[i[0]:i[1]] for i in I]

Output:

[array([  1, 161]),
array([ 51, 105, 143]),
array([ 2, 118, 127]),
array([ 37, 19, 4, 29, 13, 136])]


Related Topics



Leave a reply



Submit