0

I have two numpy arrays with 0s and 1s in them. How can I find the indexes with 1 in the first array and 0 in the second?

I tried np.logical_and

But got error message (builtin_function_or_method' object is not subscriptable)

  • Does this answer your question? [numpy get index where value is true](https://stackoverflow.com/questions/16094563/numpy-get-index-where-value-is-true) – Mushroomator Mar 22 '22 at 14:58

5 Answers5

3

Use np.where(arr1==1) and np.where(arr2==0)

Julien
  • 13,986
  • 5
  • 29
  • 53
  • Just a quick addition: Given an array `arr` you could also use `arr.nonzero()` for the ones. As a general rule `np.where()` is just a shorthand for `np.asarray(condition).nonzero()` and the latter should be preferred as it behaves correctly for subclasses. See [numpy docs for `where()`](https://numpy.org/doc/stable/reference/generated/numpy.where.html). – Mushroomator Mar 22 '22 at 14:49
0
import numpy as np


array1 = np.array([0,0,0,1,1,0,1])
array2 = np.array([0,1,0,0,1,0,1])


ones = np.where(array1 == 1)
zeroes = np.where(array2 == 0)
    
print("array 1 has 1 at",ones)
print("array 2 has 0 at",zeroes)

returns:

array 1 has 1 at (array([3, 4, 6]),)
array 2 has 0 at (array([0, 2, 3, 5]),)
White_Sirilo
  • 264
  • 1
  • 11
0

I'm not sure if theres some built-in numpy function that will do this for you, since it's a fairly specific problem. EDIT: there is, see bottom

Nonetheless, if one were to exist, it would have to be a linear time algorithm if you're passing in a bare numpy array, so writing your own isn't difficult.

If I have any numpy array (or python array) myarray, and I want a collection of indices where some object myobject appears, we can do this in one line using a list comprehension:

indices = [i for i in range(len(myarray)) if myarray[i] == myobject]

So what's going on here?

A list comprehension works in the following format:

[<output> for <input> in <iterable> if <condition>]

In our case, <input> and <output> are the indices of myarray, and the <condition> block checks if the value at the current index is equal to that of our desired value.

Edit: as White_Sirilo helpfully pointed out, numpy.where does the same thing, I stand corrected

Dharman
  • 30,962
  • 25
  • 85
  • 135
Hanson
  • 193
  • 2
  • 10
  • This is a *very* common problem and numpy of course has a function to do it efficiently. Using native python loops here will be unnecessarily complicated and inefficient... – Julien Mar 22 '22 at 14:58
0

Let's say your arrays are called j and k. The following code returns all indices where j[index] = 1 and k[index] = 0 if both arrays are 1-dimensional. It also works if j and k are different sizes.

idx_1 = np.where(j == 1)[0]
idx_2 = np.where(k == 0)[0]

final_indices = np.intersect1d(idx_1, idx_2, return_indices=False)

If your array is 2-dimensional, you can use the above code in a function and then go row-by-row. There are almost definitely better ways to do this, but this works in a pinch.

AJH
  • 799
  • 1
  • 3
  • 16
0

tow numpy array given in problem. array1 and array2

just use one_index=np.where(array1==1) and zero_index=np.where(array2==0)

Luicfer Ai
  • 56
  • 2