Numpy: Divide each row by a vector element
As has been mentioned, slicing with None
or with np.newaxes
is a great way to do this.Another alternative is to use transposes and broadcasting, as in
(data.T - vector).T
and
(data.T / vector).T
For higher dimensional arrays you may want to use the swapaxes
method of NumPy arrays or the NumPy rollaxis
function.There really are a lot of ways to do this.
For a fuller explanation of broadcasting, seehttp://docs.scipy.org/doc/numpy/user/basics.broadcasting.html