How to use numpy with 'None' value in Python?

You are looking for masked arrays. Here's an example.

import numpy.ma as ma
a = ma.array([1, 2, None], mask = [0, 0, 1])
print "average =", ma.average(a)

From the numpy docs linked above, "The numpy.ma module provides a nearly work-alike replacement for numpy that supports data arrays with masks."


haven't used numpy, but in standard python you can filter out None using list comprehensions or the filter function

>>> [i for i in [1, 2, None] if i != None]
[1, 2]
>>> filter(lambda x: x != None, [1, 2, None])
[1, 2]

and then average the result to ignore the None


You can use scipy for that:

import scipy.stats.stats as st
m=st.nanmean(vec)

Tags:

Python

Numpy

Mean