How to apply callback to each row of multi‑dimensional NumPy array in Python

1 Answer

0 votes
# Using np.apply_along_axis() for multi‑dimensional arrays

import numpy as np

# Callback function that sums a row
def row_sum(row):
    return np.sum(row)

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# Apply callback to each row (axis=1)
result = np.apply_along_axis(row_sum, axis=1, arr=arr)

print(result)  



'''
run:
 
[ 6 15]
 
'''

 



answered Mar 19 by avibootz
...