0

I have an numpy array

>>> clf_prob.dtype()

array([[ 0.05811791,  0.06526527,  0.06024136, ...,  0.06972481],
       [ 0.06093686,  0.06357167,  0.06462331, ...,  0.06999094],
       [ 0.08188396,  0.08504034,  0.0820972 , ...,  0.08487802],
       [ 0.05197106,  0.0786195 ,  0.15669477, ...,  0.0893244]])

I'm trying to add elements of these arrays such that my output would be:

[[0.05811791 + 0.06526527 + 0.06024136 +...+ 0.06972481],
[0.06093686 + 0.06357167 + 0.06462331 +...+0.06999094],
[0.08188396 + 0.08504034 + 0.0820972 + ...+  0.08487802],
[0.05197106 + 0.0786195  + 0.15669477+ ...+ 0.0893244]]

I tried doing

sum(map(sum, clf_prob))

This doesn't gives me desired output. Any suggestion?

3
  • np.sum(clf_prob, axis=1) (or try axis=0 if you don't like that!). Sometimes the keepdims parameter is handy - try it. Commented May 30, 2017 at 6:13
  • What is the desired output? Maybe use a smaller array so you can show all the data and the expected output. Commented May 30, 2017 at 6:31
  • clf_prob=array([[2,3],[3,1],[4,3]]) ............ desired output ([5],[4],[7]) Commented May 30, 2017 at 6:34

3 Answers 3

2

You can do

clf_prob.sum(axis=1)

This will take the sum over a certain axis, in this case rows.

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

Comments

0

numpy.sum() over your intended axis (1) should do the job in your case

2 Comments

this will result in one number which is sum of all elements,specifying axis=1 gives a columns wise sums,axis=0 row wise
@Eliethesaiyan I meant he should look at the way numpy sum works.. sorry
0

Another probability is to use ufunc of numpy:
np.sum.reduce(clf_prob)
which will give the sum over the first axis. You can also use the axis parameter to sum over another axis.

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.