Arvinth Kumar
Arvinth Kumar

Reputation: 1014

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.

Upvotes: 3

Views: 5968

Answers (2)

Mike Müller
Mike Müller

Reputation: 85462

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

Upvotes: 4

Divakar
Divakar

Reputation: 221574

One approach would be to convert the zeros to NaNs and then use np.nanstd that would ignore the NaNs for the standard deviation computation -

np.nanstd(np.where(np.isclose(a,0), np.nan, a))

Sample run -

In [296]: a
Out[296]: [0.0, 0.0, 0.0, 18523.94, 15501.94, 14437.03, 13402.43, 18986.14]

In [297]: np.nanstd(np.where(np.isclose(a,0), np.nan, a))
Out[297]: 2217.2329816471693

Note that we are using np.isclose(a,0) because we are dealing with floating-pt numbers here and it's not a good idea to simply compare against zeros to detect those in a float dtype array.

Upvotes: 3

Related Questions