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]
Answer from Saullo G. P. Castro on Stack OverflowYou 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)
Iterating through a numpy array
How do I iterate over rows and columns? Can't find a tutorial on it.
python - Iterate over a numpy Matrix rows - Stack Overflow
Looping through each item in a numpy array? - Stack Overflow
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?
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!!!!
The problem is you are using np.matrix. Use np.array instead and simply iterate without indexing:
result = np.array([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
for p in result:
print(p)
[11 12 13]
[21 22 23]
[31 32 33]
Explanation
What you are seeing is the effect of numpy.matrix requiring each row to have 2 dimensions. This is unnecessary and anti-pattern for NumPy.
There is a history behind numpy.matrix. It was used initial for convenience of matrix multiplication operators. But this is no longer an issue since @ is possible (Python 3.5+) instead of nested dot calls. Therefore, by default, use numpy.array.
There are two ways (both essentially boils down to same logic)
method-1:
Use result.A
Return self as an ndarray object.
Equivalent to np.asarray(self).
In [16]: for row in result.A:
...: print(row)
...:
[11 12 13]
[21 22 23]
[31 32 33]
method-2:
Use result.getA()
Return self as an ndarray object.
Equivalent to np.asarray(self).
In [17]: for row in result.getA():
...: print(row)
...:
[11 12 13]
[21 22 23]
[31 32 33]
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.