Just iterate over the transposed of your array:
for column in array.T:
some_function(column)
Answer from tillsten on Stack OverflowJust iterate over the transposed of your array:
for column in array.T:
some_function(column)
This should give you a start
>>> for col in range(arr.shape[1]):
some_function(arr[:,col])
[1 2 3 4]
[99 14 12 43]
[2 5 7 1]
I know this is a dumb question, but I've scoured Google for the documentation, Stack Overflow, etc. but I can't figure out how to do this. I'm really used to C so I might be approaching this wrong.
Basically, I have a multidimensional array; specifically, it's n x 2. Another way of looking at it is that it's an array of tuples.
So suppose there are the points (1, 9), (2, 8), and (3, 7).
Now, my assumption is that [[1, 2, 3], [9, 8, 7]] is an nx2 array, and that [[1,9], [2, 8], [3, 7]] is a 2xn array, correct?
So my question is, in each case (given an nx2 and a 2xn array), how would I iterate through each ordered pair?
Hey,
I'm fairly new to Python and Numpy, but I have a reoccuring problem: I have a transformation matrix (as a numpy array) with a shape of (2,2) and a numpy array (shape(2,i)) with a lot of points I want to transform. In order to transform I need to get every column and multiply the transformation matrix with the column and then save the result in numpy array. I'm able to iterate over every column, however that doesn't look like the pythonic way to do it. Is there a short command or pythonic way?
The problem I want to solve is this- I want to find a certain item in a list that is formatted like a chart, something like this:
list=[1,2,3,4,5
2,4,6,8,10
3,6,9,12,16
4,8,12,16,20
5,10,15,20,25]So I want to be able to call values within this makeshift chart. For instance, if I wanted to call a certain value within this list like--- Row 2, column 3.... this would return 6.
I think this can be done with iterations???? I saw a video of it somewhere but I can't find anything on google about this, I think I just don't know how to explain it. How do I do what I want to do in python 3+, without using panda?
Would really appreciate some code, but if you can just tell me what I should look up to learn how to do this, it would help just as much! Thanks!!!!
Try np.ndenumerate:
>>> a =numpy.array([[1,2],[3,4]])
>>> for (i,j), value in np.ndenumerate(a):
... print(i, j, value)
...
0 0 1
0 1 2
1 0 3
1 1 4
Make a small 2d array, and a nested list from it:
In [241]: A=np.arange(6).reshape(2,3)
In [242]: alist= A.tolist()
In [243]: alist
Out[243]: [[0, 1, 2], [3, 4, 5]]
One way of iterating on the list:
In [244]: for row in alist:
...: for item in row:
...: print(item)
...:
0
1
2
3
4
5
works just same for the array
In [245]: for row in A:
...: for item in row:
...: print(item)
...:
0
1
2
3
4
5
Now neither is good if you want to modify elements. But for crude iteration over all elements this works.
WIth the array I can easily treat it was a 1d
In [246]: [i for i in A.flat]
Out[246]: [0, 1, 2, 3, 4, 5]
I could also iterate with nested indices
In [247]: [A[i,j] for i in range(A.shape[0]) for j in range(A.shape[1])]
Out[247]: [0, 1, 2, 3, 4, 5]
In general it is better to work with arrays without iteration. I give these iteration examples to clearup some confusion.
You can use numpy.apply_along_axis(). Assuming that your array is 2D, you can use it like:
import numpy as np
myarray = np.array([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
def myfunction(x):
return x[0] + x[1]**2 + x[2]**3
print(np.apply_along_axis(myfunction, axis=1, arr=myarray))
#[ 2352 12672 36992]
While you should certainly provide more information, if you are trying to go through each row, you can just iterate with a for loop:
import numpy
m = numpy.ones((3,5),dtype='int')
for row in m:
print str(row)