0

I have this ndarray (not matrix):

mx = np.array([[10,25,33],[3,1,5],[50,50,52]])

[[10 25 33]
 [ 3  1  5]
 [50 50 52]]

and I want to get a ndarray of shares by dividing every element by the sum of the column. So the result of this operation:

[[10/63 25/76 33/90]
 [ 3/63  1/76  5/90]
 [50/63 50/76 52/90]]

I can do

np.true_divide(mx,mx.sum(axis=0))

Are ther some build-in functions to calculate shares or stuff like that?

3
  • In what way it doesn't work? I tried the code and it does exactly what you wanted in Python3. Commented Feb 13, 2018 at 15:03
  • It returns all 0. Just discovered the np.true_divide(), so it's solved. Commented Feb 13, 2018 at 15:05
  • Just use mx / mx.sum(axis=0). If that does the wrong division, use from __future__ import division at the top of your file Commented Feb 13, 2018 at 21:24

1 Answer 1

1

The problem is related to how int behaves in division between python2 and python3. Should you start with float array it would work fine. There is also np.true_divide() that you mention in the comment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.