я пробую более одного кода.Лучше всего выглядит вторая версия:
import numpy as np
a = np.array([[1,2,9], [5,2,4], [1,2,1]])
print(a)
%timeit temp = a[np.sum(a, axis=1) > 5]
temp = a[np.sum(a, axis=1) > 5]
print(temp)
%timeit temp = [n for n, curr in enumerate(a) if sum(curr) > 5 ]
temp = [n for n, curr in enumerate(a) if sum(curr) > 5 ]
print(temp)
%timeit temp = np.argwhere(np.sum(a, axis=1) > 5)
temp = np.argwhere(np.sum(a, axis=1) > 5)
print(temp)
%timeit temp = np.flatnonzero(a.sum(1) > 10)
temp = np.flatnonzero(a.sum(1) > 10)
print(temp)
Результаты:
[[1 2 9]
[5 2 4]
[1 2 1]]
The slowest run took 12.37 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.47 µs per loop
[[1 2 9]
[5 2 4]]
100000 loops, best of 3: 5.09 µs per loop
[0, 1]
The slowest run took 9.83 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 13.3 µs per loop
[[0]
[1]]
The slowest run took 6.78 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 6.8 µs per loop
[0 1]