How Can I Calculate The Ratio Of A Value In Array To The Sum Of Array In Python?
I have an array such as this: array =[[1,2,3], [5,3,4], [6,7,2]] and for each member I would like to calculate the ratio of them to the sum of the row. Therefore,
Solution 1:
You can specify keepdim=True
while doing the sum, and then you have a 2d array as result while each row stands for the row sum:
array = np.array([[1,2,3],
[5,3,4],
[6,7,2.]])
array/array.sum(1, keepdims=True)
#array([[ 0.16666667, 0.33333333, 0.5 ],
# [ 0.41666667, 0.25 , 0.33333333],
# [ 0.4 , 0.46666667, 0.13333333]])
Post a Comment for "How Can I Calculate The Ratio Of A Value In Array To The Sum Of Array In Python?"