python - How to divide matrix elements by corresponding matrix row sum in numpy? -
this question has answer here:
- numpy divide row row sum 2 answers
for example,
m= [[1,2], [7,8]] then want
[[1/3, 2/3], [7/15, 8/15]] i'm trying vectorized. 1 idea have write s = np.sum(m, axis=1); gives corresponding row sums. maybe transpose s, , copy along columns, elementwise division of m/s, seems hacky. what's right way?
use tile repeat along dimension on sum operated.
m / np.tile(np.sum(m, 1), (1, m.shape[1]))
Comments
Post a Comment