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]]
Answer from Alex on Stack OverflowYes, 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]]
If you need the index of the first occurrence of only one value, you can use nonzero (or where, which amounts to the same thing in this case):
>>> t = array([1, 1, 1, 2, 2, 3, 8, 3, 8, 8])
>>> nonzero(t == 8)
(array([6, 8, 9]),)
>>> nonzero(t == 8)[0][0]
6
If you need the first index of each of many values, you could obviously do the same as above repeatedly, but there is a trick that may be faster. The following finds the indices of the first element of each subsequence:
>>> nonzero(r_[1, diff(t)[:-1]])
(array([0, 3, 5, 6, 7, 8]),)
Notice that it finds the beginning of both subsequence of 3s and both subsequences of 8s:
[1, 1, 1, 2, 2, 3, 8, 3, 8, 8]
So it's slightly different than finding the first occurrence of each value. In your program, you may be able to work with a sorted version of t to get what you want:
>>> st = sorted(t)
>>> nonzero(r_[1, diff(st)[:-1]])
(array([0, 3, 5, 7]),)
python - Index of element in NumPy array - Stack Overflow
efficiently finding the index of a value in a numpy array
How to find index of value in NumPy array? - Data Exploration & Visualization - Data Science Dojo Discussions
python - get the index of element in NumPy array - Stack Overflow
Use np.where to get the indices where a given condition is True.
Examples:
For a 2D np.ndarray called a:
i, j = np.where(a == value) # when comparing arrays of integers
i, j = np.where(np.isclose(a, value)) # when comparing floating-point arrays
For a 1D array:
i, = np.where(a == value) # integers
i, = np.where(np.isclose(a, value)) # floating-point
Note that this also works for conditions like >=, <=, != and so forth...
You can also create a subclass of np.ndarray with an index() method:
class myarray(np.ndarray):
def __new__(cls, *args, **kwargs):
return np.array(*args, **kwargs).view(myarray)
def index(self, value):
return np.where(self == value)
Testing:
a = myarray([1,2,3,4,4,4,5,6,4,4,4])
a.index(4)
#(array([ 3, 4, 5, 8, 9, 10]),)
You can convert a numpy array to list and get its index .
for example:
tmp = [1,2,3,4,5] #python list
a = numpy.array(tmp) #numpy array
i = list(a).index(2) # i will return index of 2, which is 1
this is just what you wanted.
I have a numpy array that has unique values and is static, and I routinely want to find some index of a value. Is it a good idea to repeatedly use where for this? Is numpy sorting the values and storing a mapping of them to the indices behind the scene, or otherwise doing something smart to quickly find the index? If not, what would be a good way to implement finding the index of a value in a numpy array?
You can do it quite easily, using Pandas.
First convert your array to a pandasonic Series:
s = pd.Series(a)
Then:
- Group it by its value.
- Apply to each group a function, which:
- for groups of size 4 or smaller returns just this group,
- for groups with more members, returns a random sample of 4 elements from them.
- Drop the 0-th level of the resulting index (added during grouping).
- Sort by the (original) index, to bring back the original order (without the dropped elements, for now we have original values with their corresponding indices).
- Return the index of the above result, as a Numpy array.
The code to do it is:
s.groupby(s).apply(lambda grp: grp if grp.size <= 4 else grp.sample(4))\
.reset_index(level=0, drop=True).sort_index().index.values
For a sample array containg:
array([2, 2, 1, 0, 1, 0, 2, 2, 2, 3, 0, 2, 1, 0, 0, 3, 3, 0, 2, 4])
the result is:
array([ 0, 2, 4, 5, 7, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19])
To show that this result is correct, I repeated the source array, with "x" marks below the elements at the returned indices.
array([2, 2, 1, 0, 1, 0, 2, 2, 2, 3, 0, 2, 1, 0, 0, 3, 3, 0, 2, 4])
x x x x x x x x x x x x x x x
Yes, you can do this using NumPy by:
a = np.random.randint(0,10,20)
print(a)
num = 4
if str(np.where(a<num)[0].shape) != '(0,)': # Condition 1
ans = np.where(a<num)[0]
print(ans)
if str(np.where(a>=num)[0].shape) != '(0,)': # Condition 2
ans = np.random.choice(a[np.where(a>=num)[0]], 4)
print(ans)
'''Output:
[9 9 8 1 0 7 7 4 6 2 8 2 1 2 9 5 5 1 4 1]
[ 3 4 9 11 12 13 17 19]
[4 9 8 7]
'''
I have done only for the cases you have mentioned. There can be many other cases such as if both conditions are true, or if there are less than 4 numbers in second case.