Is there a way to work with iterators instead of (for example) numpy.ndarray in numpy?
For example, imagine I have a 2D-array and I want to know if there is a row that only contain even numbers:
import numpy as np
x = np.array([[1, 2], [2, 4], [3, 6]])
np.any(np.all(x % 2 == 0, axis=1))
Is there a way to do this kind of things without instantiating the intermediate objects in memory? (or maybe it is already the case and I just don't know it) In this example, that would mean having an iterator over [False True False] instead of an array. In other words, can we do something that would be equivalent to:
has_an_even_row = False
for row in x:
if np.all(row % 2 == 0):
has_an_even_row = True
break
My question doesn't only concern all and any but all function/methods in numpy. If it isn't possible I wonder if there is a practical reason for not having this in numpy. (Maybe everyone thinks it's useless, that would be a good reason)
forloop. But be ware the action will usually, but not always, be slower.numexpr. But, as hpaulj is saying, if you want an iterator, use a for-loop.numbawhich is a JIT compiler that will just-in-time-compile functions that use simple loops over numpy data structures into native code. In my experience, it is quite effective.numpyis like a Lego set. It is fast and easy to use when you stick with the given building blocks. It does not include a custom block molding machine - you have to get that from some other source.