float(nan')表示nan(不是数字)。但我该如何检查呢?


当前回答

另一种方法是,如果你坚持低于2.6,你没有numpy,也没有IEEE 754支持:

def isNaN(x):
    return str(x) == str(1e400*0)

其他回答

另一种方法是,如果你坚持低于2.6,你没有numpy,也没有IEEE 754支持:

def isNaN(x):
    return str(x) == str(1e400*0)

numpy.isnan(数字)告诉你它是不是NaN。

判断变量是NaN还是None的所有方法:

无类型

In [1]: from numpy import math

In [2]: a = None
In [3]: not a
Out[3]: True

In [4]: len(a or ()) == 0
Out[4]: True

In [5]: a == None
Out[5]: True

In [6]: a is None
Out[6]: True

In [7]: a != a
Out[7]: False

In [9]: math.isnan(a)
Traceback (most recent call last):
  File "<ipython-input-9-6d4d8c26d370>", line 1, in <module>
    math.isnan(a)
TypeError: a float is required

In [10]: len(a) == 0
Traceback (most recent call last):
  File "<ipython-input-10-65b72372873e>", line 1, in <module>
    len(a) == 0
TypeError: object of type 'NoneType' has no len()

NaN类型

In [11]: b = float('nan')
In [12]: b
Out[12]: nan

In [13]: not b
Out[13]: False

In [14]: b != b
Out[14]: True

In [15]: math.isnan(b)
Out[15]: True

对于panda中的字符串,请使用pd.isnull:

if not pd.isnull(atext):
  for word in nltk.word_tokenize(atext):

NLTK的特征提取功能

def act_features(atext):
features = {}
if not pd.isnull(atext):
  for word in nltk.word_tokenize(atext):
    if word not in default_stopwords:
      features['cont({})'.format(word.lower())]=True
return features

math.isnan()

或将数字与自身进行比较。NaN总是!=NaN,否则(例如,如果是数字),比较应成功。