How to apply a function to each row of a NumPy array in Python

A function applied to each row of a NumPy array uses the row as input and computes a result for each row. For example, computing the absolute sum of each row in the array [[1, 2, -3], [4, -5, 6], [-7, 8, 9]] results in [6, 15, 24].

Solution for How to apply a function to each row of a NumPy array in Python : You can use numpy.apply_along_axis() to apply a function to each row of a NumPy array Call numpy.apply_along_axis(func1d, axis, arr) to apply func1d to arr along axis and return the results. Set axis=1 to apply func1d along the rows of arr.


how-to-apply-a-function-to-each-row-of-a-numpy-array-in-python