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

女生的网名这么多〃 提交于 2020-01-10 03:27:30

问题


I'd like to calculate the mean of an array in Python in this form:

Matrice = [1, 2, None]

I'd just like to have my None value ignored by the numpy.mean calculation but I can't figure out how to do it.


回答1:


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

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

Unfortunately, masked arrays aren't thoroughly supported in numpy, so you've got to look around to see what can and can't be done with them.




回答2:


You can use scipy for that:

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



回答3:


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




回答4:


You might also be able to kludge with values like NaN or Inf.

In [1]: array([1, 2, None])
Out[1]: array([1, 2, None], dtype=object)

In [2]: array([1, 2, NaN])
Out[2]: array([  1.,   2.,  NaN])

Actually, it might not even be a kludge. Wikipedia says:

NaNs may be used to represent missing values in computations.

Actually, this doesn't work for the mean() function, though, so nevermind. :)

In [20]: mean([1, 2, NaN])
Out[20]: nan



回答5:


You can also use filter, pass None to it, it will filter non True objects, also 0, :D So, use it when you dont need 0 too.

>>> filter(None,[1, 2, None])
[1, 2]



回答6:


You can 'upcast' the array to numpy's float64 dtype and then use numpy's nanmean method as in the following example:

import numpy as np

arr = [1,2,3, None]
arr2 = np.array(arr, dtype=np.float64)
print(arr2) # [ 1.  2.  3. nan]
print(np.nanmean(arr2)) # 2.0



回答7:


np.mean(Matrice[Matrice != None])



来源:https://stackoverflow.com/questions/962343/how-to-use-numpy-with-none-value-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!