Given a values array and a valid indices array, I would like to get all the other indices.
Looking for a pythonic way to do it, but here is an example for a solution and also clarify what is it I'm trying to accomplish:
A = np.array(['a', 'b', 'c', 'd', 'e', 'f', 'g']) # Array of values. Shape: (7,)
B = np.array([0,3,5]) # Array of indices.
# Looking for a more elegant way to do this following line
C = np.array([i for i in range(len(A)) if i not in B]) # Array indices not in B
# Expected Output: C = [1, 2, 4, 6]
Edit : Benchmarking the solutions
A = np.ones(10000)
B = np.random.random_integers(low=0, high=len(A) - 1, size=8000)
t1 = time()
mask = np.ones(len(A), dtype=bool)
mask[B] = False
C = np.arange(len(A))[mask]
t1 = time() - t1
t2 = time()
C = np.delete(np.arange(A.size), B)
t2 = time() - t2
t3 = time()
C = np.array([i for i in range(len(A)) if i not in B])
t3 = time() - t3
t4 = time()
C = set(np.arange(len(A))).difference(B)
t4 = time() - t4
print("T1: %.5f" % np.round(t1, 5))
print("T2: %.5f" % np.round(t2, 5))
print("T3: %.5f" % np.round(t3, 5))
print("T4: %.5f" % np.round(t4, 5))
Results (Values varied when number of indices in B changes, but the fastest always remained T1:
T1: 0.00011 <<< Ran the above script multiple times, this always was the fastest. Second approach was always just a little behind.
T2: 0.00017
T3: 0.05746 << List comprehension took the most time. Even after removing the np.array.
T4: 0.00158
- Conclusion:
I will be using the second approach above (T2) just because it is a one liner and takes (almost) the same time as the fastest approach.