2

I am trying to replicate the behaviour of zip(a, b) in order to be able to loop simultaneously along two NumPy arrays. In particular, I have two arrays a and b:

a.shape=(n,m)
b.shape=(m,) 

I would like to get for every loop a column of a and an element of b.

So far, I have tried the following:

for a_column, b_element in np.nditer([a, b]):
    print(a_column)

However, I get printed the element a[0,0] rather than the column a[0,:], which I want.

How can I solve this?

0

3 Answers 3

1

You can still use zip on numpy arrays, because they are iterables.

In your case, you'd need to transpose a first, to make it an array of shape (m,n), i.e. an iterable of length m:

for a_column, b_element in zip(a.T, b):
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

This is the most pythonic answer. np.nditer is potentially more powerful but a whole lot uglier. For instance, you have to recall the difference between C- and Fortran-ordering.
1

Adapting my answer in shallow iteration with nditer, nditer and ndindex can be used to iterate over rows or columns by generating indexes.

In [19]: n,m=3,4
In [20]: a=np.arange(n*m).reshape(n,m)
In [21]: b=np.arange(m)

In [22]: it=np.nditer(b)
In [23]: for i in it: print a[:,i],b[i]
[0 4 8] 0
[1 5 9] 1
[ 2  6 10] 2
[ 3  7 11] 3

In [24]: for i in np.ndindex(m):print a[:,i],b[i]
[[0]
 [4]
 [8]] 0
[[1]
 [5]
 [9]] 1
[[ 2]
 [ 6]
 [10]] 2
[[ 3]
 [ 7]
 [11]] 3
In [25]: 

ndindex uses an iterator like: it = np.nditer(b, flags=['multi_index'].

For iteration over a single dimension like this, for i in range(m): works just as well.

Also from the other thread, here's a trick using order to iterate without the indexes:

In [28]: for i,j in np.nditer([a,b],order='F',flags=['external_loop']):
    print i,j
[0 4 8] [0 0 0]
[1 5 9] [1 1 1]
[ 2  6 10] [2 2 2]
[ 3  7 11] [3 3 3]

Comments

0

Usually, because of NumPy's ability to broadcast arrays, it is not necessary to iterate over the columns of an array one-by-one. For example, if a has shape (n,m) and b has shape (m,) then you can add a+b and b will broadcast itself to shape (n, m) automatically.

Moreover, your calculation will complete much faster if it can be expressed through operations on the whole array, a, rather than through operations on pieces of a (such as on columns) using a Python for-loop.

Having said that, the easiest way to loop through the columns of a is to iterate over the index:

for i in np.arange(b.shape[0]):
    a_column, b_element = a[:, i], b[i]
    print(a_column)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.