I have an array:
> a
array([[1, 2, 3], [2, 3, 4], [3, 4, 5]])
I want to change those values to the avarage of those values, so the output will be:
> a
array([[2, 2, 2], [3, 3, 3], [4, 4, 4]])
Is there a fast way to do this without using a for loop?
If you are able to use
numpymodule the broadcasting functions are useful for this (Though they do this by highly optimized loops as well).a.mean(axis=1)gets the row mean andnp.broadcast_tobroadcasts the result to the desired shape..Tjust returns transpose of the resultOutput: