Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101358 > unrolled thread

SUM only of positive numbers from array

Started byDavorin Bajic <davorinbajic@gmail.com>
First post2016-01-07 20:12 -0800
Last post2016-01-08 04:33 -0800
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  SUM only of positive numbers from array Davorin Bajic <davorinbajic@gmail.com> - 2016-01-07 20:12 -0800
    Re: SUM only of positive numbers from array Peter Otten <__peter__@web.de> - 2016-01-08 09:17 +0100
    Re: SUM only of positive numbers from array Davorin Bajic <davorinbajic@gmail.com> - 2016-01-08 04:33 -0800

#101358 — SUM only of positive numbers from array

FromDavorin Bajic <davorinbajic@gmail.com>
Date2016-01-07 20:12 -0800
SubjectSUM only of positive numbers from array
Message-ID<50beb098-f854-4b17-b157-1be075d91f4d@googlegroups.com>
Hi All,

I should help...

I want to calculate the sum of a positive number, for each row:


x = ((mat_1 / s_1T)-(s_2 / total))
y = (np.sum(x > 0, axis=1)).reshape(-1, 1).tolist()

However, this part of the code only calculation count, I need sum.

Any ideas how to solve this problem?

thanks in advance

[toc] | [next] | [standalone]


#101363

FromPeter Otten <__peter__@web.de>
Date2016-01-08 09:17 +0100
Message-ID<mailman.59.1452241084.2305.python-list@python.org>
In reply to#101358
Davorin Bajic wrote:

> Hi All,
> 
> I should help...
> 
> I want to calculate the sum of a positive number, for each row:
> 
> 
> x = ((mat_1 / s_1T)-(s_2 / total))
> y = (np.sum(x > 0, axis=1)).reshape(-1, 1).tolist()
> 
> However, this part of the code only calculation count, I need sum.
> 
> Any ideas how to solve this problem?

Use x>0 as "index":

>>> import numpy
>>> x = numpy.array([1, -2, 3])
>>> x>0
array([ True, False,  True], dtype=bool)
>>> x[x>0]
array([1, 3])
>>> x[x>0].sum()
4

[toc] | [prev] | [next] | [standalone]


#101368

FromDavorin Bajic <davorinbajic@gmail.com>
Date2016-01-08 04:33 -0800
Message-ID<73c7888d-d16d-4f93-9214-c287c5a9e2d0@googlegroups.com>
In reply to#101358
On Friday, January 8, 2016 at 5:13:06 AM UTC+1, Davorin Bajic wrote:
> Hi All,
> 
> I should help...
> 
> I want to calculate the sum of a positive number, for each row:
> 
> 
> x = ((mat_1 / s_1T)-(s_2 / total))
> y = (np.sum(x > 0, axis=1)).reshape(-1, 1).tolist()
> 
> However, this part of the code only calculation count, I need sum.
> 
> Any ideas how to solve this problem?
> 
> thanks in advance

Tnx Peter,

I solved using mask and filled

x = ((mat_1 / s_1T)-(s_2 / total))
i_bolean = x < 0 
amasked = np.ma.array(x, mask=i_bolean) 
fill_value = 0. 
aunmasked = np.ma.filled(amasked, fill_value) 
y = (aunmasked.sum(axis=1)).reshape(-1, 1).tolist()

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web