For python dict
, I could use iteritems()
to loop through key and value at the same time. But I cannot find such functionality for NumPy array. I have to manually track idx
like this:
idx = 0 for j in theta: some_function(idx,j,theta) idx += 1
Is there a better way to do this?
The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])
NumPy: repeat() function The repeat() function is used to repeat elements of an array. Input array. The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.
Iterating Arrays Iterating means going through elements one by one. As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python. If we iterate on a 1-D array it will go through each element one by one.
There are a few alternatives. The below assumes you are iterating over a 1d NumPy array.
range
for j in range(theta.shape[0]): # or range(len(theta)) some_function(j, theta[j], theta)
Note this is the only of the 3 solutions which will work with numba
. This is noteworthy since iterating over a NumPy array explicitly is usually only efficient when combined with numba
or another means of pre-compilation.
enumerate
for idx, j in enumerate(theta): some_function(idx, j, theta)
The most efficient of the 3 solutions for 1d arrays. See benchmarking below.
np.ndenumerate
for idx, j in np.ndenumerate(theta): some_function(idx[0], j, theta)
Notice the additional indexing step in idx[0]
. This is necessary since the index (like shape
) of a 1d NumPy array is given as a singleton tuple. For a 1d array, np.ndenumerate
is inefficient; its benefits only show for multi-dimensional arrays.
# Python 3.7, NumPy 1.14.3 np.random.seed(0) arr = np.random.random(10**6) def enumerater(arr): for index, value in enumerate(arr): index, value pass def ranger(arr): for index in range(len(arr)): index, arr[index] pass def ndenumerater(arr): for index, value in np.ndenumerate(arr): index[0], value pass %timeit enumerater(arr) # 131 ms %timeit ranger(arr) # 171 ms %timeit ndenumerater(arr) # 579 ms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With