Iterating over Numpy matrix rows to apply a function each?

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]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)