Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating standard deviation on list ignoring zeros using numpy

I am having a list pct_change. I need to calculate std deviation on the list ignoring the zeros. I tried below code, but it is not working as expected.

import numpy as np
m = np.ma.masked_equal(pct_change, 0)
value = m.mask.std()

Input value: pct_change

0          0.00
1          0.00
2          0.00
3      18523.94
4      15501.94
5      14437.03
6      13402.43
7      18986.14

Code has to ignore 3 zero values and then calculate standard deviation.

like image 274
Arvinth Kumar Avatar asked May 11 '26 19:05

Arvinth Kumar


1 Answers

Filter for values unequal to zero first:

>>> a
array([     0.  ,      0.  ,      0.  ,  18523.94,  15501.94,  14437.03,
        13402.43,  18986.14])
>>> a[a!=0].std()
2217.2329816471693
like image 124
Mike Müller Avatar answered May 13 '26 08:05

Mike Müller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!