I would like to run an operation (e.g. subtracting the median) on rows of a numpy array.
One way to do that is using comprehension lists:
import numpy as np
from statistics import median
x = np.array([[1, 2, 3, 4], [5, 6, 7 ,8], [9, 10, 11, 12]])
xm = np.vstack(([x[i,:] - median(x[i,:]) for i in range(x.shape[0])]))
Each row is processed, then stacked vertically as numpy array.
Is there a more efficient/elegant way to do that?