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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Andre_k
  • 1,680
  • 3
  • 18
  • 41
  • `np.sum(clf_prob, axis=1)` (or try axis=0 if you don't like that!). Sometimes the `keepdims` parameter is handy - try it. – hpaulj May 30 '17 at 06:13
  • What is the desired output? Maybe use a smaller array so you can show all the data and the expected output. – Daniel F May 30 '17 at 06:31
  • clf_prob=array([[2,3],[3,1],[4,3]]) ............ desired output ([5],[4],[7]) – Andre_k May 30 '17 at 06:34

3 Answers3

2

You can do

clf_prob.sum(axis=1)

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

Mathias711
  • 6,568
  • 4
  • 41
  • 58
0

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

Isdj
  • 1,835
  • 1
  • 18
  • 36
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.

Code Pope
  • 5,075
  • 8
  • 26
  • 68