Skip to content Skip to sidebar Skip to footer

Iterate Over Numpy Array Columnwise

np.nditer automatically iterates of the elements of an array row-wise. Is there a way to iterate of elements of an array columnwise? x = np.array([[1,3],[2,4]]) for i in np.nditer

Solution 1:

For completeness, you don't necessarily have to transpose the matrix before iterating through the elements. With np.nditer you can specify the order of how to iterate through the matrix. The default is usually row-major or C-like order. You can override this behaviour and choose column-major, or FORTRAN-like order which is what you desire. Simply specify an additional argument order and set this flag to 'F' when using np.nditer:

In [16]: x = np.array([[1,3],[2,4]])

In [17]: for i in np.nditer(x,order='F'):
   ....:     print i
   ....:     
1234

You can read more about how to control the order of iteration here: http://docs.scipy.org/doc/numpy-1.10.0/reference/arrays.nditer.html#controlling-iteration-order

Solution 2:

You could use the shape and slice each column

>>> [x[:, i] for i in range(x.shape[1])]
[array([1, 2]), array([3, 4])]

Solution 3:

You could transpose it?

>>> x = np.array([[1,3],[2,4]])
>>> [y for y in x.T]
[array([1, 2]), array([3, 4])]

Or less elegantly:

>>> [np.array([x[j,i] for j in range(x.shape[0])]) for i in range(x.shape[1])]
[array([1, 2]), array([3, 4])]

Solution 4:

nditer is not the best iteration tool for this case. It is useful when working toward a compiled (cython) solution, but not in pure Python coding.

Look at some regular iteration strategies:

In [832]: x=np.array([[1,3],[2,4]])

In [833]: x
Out[833]: 
array([[1, 3],
       [2, 4]])

In [834]: for i in x:print i   # print each row
[13]
[24]

In [835]: for i in x.T:print i   # print each column
[12]
[34]

In [836]: for i in x.ravel():print i   # print values in order
1324

In [837]: for i in x.T.ravel():print i  # print values in column order
1234

You comment: I need to fill values into an array based on the index of each cell in the array

What do you mean by index?

A crude 2d iteration with indexing:

In [838]: for i in range(2):
   .....:     for j in range(2):
   .....:         print (i,j),x[i,j]
(0, 0) 1
(0, 1) 3
(1, 0) 2
(1, 1) 4

ndindex uses nditer to generate similar indexes

In[841]: fori,jinnp.ndindex(x.shape):
   .....:     print (i,j),x[i,j]
   .....:     
(0, 0) 1
(0, 1) 3
(1, 0) 2
(1, 1) 4

enumerate is a good Python way of getting both values and indexes:

In[847]: fori,vinenumerate(x):printi,v0[1 3]1[2 4]

Or you can use meshgrid to generate all the indexes, as arrays

In [843]: I,J=np.meshgrid(range(2),range(2))

In [844]: I
Out[844]: 
array([[0, 1],
       [0, 1]])

In [845]: J
Out[845]: 
array([[0, 0],
       [1, 1]])

In [846]: x[I,J]
Out[846]: 
array([[1, 2],
       [3, 4]])

Note that most of these iterative methods just treat your array as a list of lists. They don't take advantage of the array nature, and will be slow compared to methods that work with the whole x.

Post a Comment for "Iterate Over Numpy Array Columnwise"