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

后端 未结 7 2358
甜味超标
甜味超标 2020-12-18 18:14

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 valu

7条回答
  •  粉色の甜心
    2020-12-18 18:39

    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
    

提交回复
热议问题