python - Get only those elements of array that match all elements of test array? -
How do I get only elements of the array that match all the elements of the test array? For example, If I have:
& gt; & Gt; & Gt; Np & gt; & Gt; & Gt; Arr = np.array ([[0, 0, 1], [1, 0, 1], [1, 0, 1]]) gt; & Gt; & Gt; Arr == [0,0,1] array ([[True, True], [false, true], [false, true, true]], dtype = bool
< Solution Index for code> arr == [0,0,1] 0
You must check the status of the row using a axis
argument:
& gt; & Gt; & Gt; All (axis = 1) array ([right, wrong, wrong], dtype = bool]
if you Index wants:
>> Np.where ((arr == np.array ([0,0,1]). All (axis = 1) ) (Array ([0]),)
We do not want to stop calling from np.all
to np.reduce
You can also fast (faster):
>> np.array ([0,0,1])> & gt; & gt; & Gt; Dt = np.dtype ((np.void, arr.dtype.itemsize * arr.shape [1])) >>> (ARR Scene (DT) == B..Weu ( New form (-1) array ([true, false, false], dtype = bool)
some time:
arr = Np.r Andom.randint (0,2, (1E2,3))% timeit (arr.view (dt) == b.view (dt)) Reshape (- 1) 100000 loop, best 3: 7.76 μs per loop% With all the arrays: The time (arr == b) all (axis = 1) 100000 loops, the best 3: 13.5 μs per loop Arr = np.random.randint (0,2, (1e 5,3))% timeit (arr.view (dt) == b.view (dt)) (axis 1 = 1) 1 loops, best 3 : 315 ms per loop
Comments
Post a Comment