부동 소수점 (& # 39, nan& # 39;) '결과' Nan (not a number). 그러나 how do I check for it? 하지만 난 제공하십시오 매우 쉽게 찾을 수 없습니다.
[마테오아난 (x)] [1]
>. 만약 x 는 복귀하십시오 '진정한' 와 '거짓' NaN (not a number), 그렇지 않으면.
>>> import math
>>> x = float('nan')
>>> math.isnan(x)
True
[1]: http://docs.python.org/library/math.html # 마테오아난
사실 그냥 실행했음 를 이 있지만, 난, - inf, 또는 그냥 전송되었기 가져다줄래요 확인하는 데 inf.
if float('-inf') < float(num) < float('inf'):
난, 대한, 그리고 모두 마찬가지입니다 숫자임, inf) 와 같은 것들을 대한 예외를 제기할 것이라고 거짓값 문장열 또는 다른 유형 (가능성이 있는 것은 좋은). 또한 이 모든 라이브러리보다는 임포트하지 필요로 하지 않는 등 수학 또는 누마피 (누마피 젠장 너무 큰 it 크기를 두 배로 어떤 컴파일된 응용 프로그램).
답을 슬라이드에서는 작업:
이것은:
import numpy as np
def is_nan(x):
return (x is np.nan or x != x)
그리고 일부 (예:
values = [float('nan'), np.nan, 55, "string", lambda x : x]
for value in values:
print "{:<8} : {}".format(repr(value), is_nan(value))
출력:
nan : True
nan : True
55 : False
'string' : False
<function <lambda> at 0x000000000927BF28> : False
import pandas as pd
import numpy as np
import math
#For single variable all three libraries return single boolean
x1 = float("nan")
print(f"It's pd.isna : {pd.isna(x1)}")
print(f"It's np.isnan : {np.isnan(x1)}")
print(f"It's math.isnan : {math.isnan(x1)}")
"'
[마테오아난 ()] [1]
또는 비교할 수 있다. 난 항상! = 난, 그렇지 않을 경우 (예를 들어, 는 경우 여러) 비교 /dev/raw/raw200 성공했다.
[1]: http://docs.python.org/library/math.html # 마테오아난
Nan 또는 가변입니다 경우 모든 방법을 알 수 없음.
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()
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
나는 '난' 으로 받은 데이터를 전송하는 '& # 39, 웹 서비스 문자열으로 Nan& # 39,'. 하지만 내 데이터 뿐만 아니라 간단한 문자열 신규인지 있을 수 있기 때문에 다른 부동 소수점 (가치) '' 예외를 발생시킬 수 있다. 난 다음 사용되는) 의 변형 수락됨 대답:
def isnan(value):
try:
import math
return math.isnan(float(value))
except:
return False
요구 사항:
isnan('hello') == False
isnan('NaN') == True
isnan(100) == False
isnan(float('nan')) = True
여기 있는 경우, 다른 유형을 함께 사용하지 않는 누마피 이트레이블 솔루션이다.
from math import isnan
Z = ['a','b', float('NaN'), 'd', float('1.1024')]
[x for x in Z if not (
type(x) == float # let's drop all float values…
and isnan(x) # … but only if they are nan
)]
pre> <; [& # 39, a& # 39, & # 39, & # 39 d& # 39 b& # 39,,, 1.1024] < /pre>;
단락, 즉 '평가' 에서 유형의 & # 39 라는 값을 isnan 않을 수 없는 것처럼 허위 · (.) ',', float& # 39 로 평가되는 '거짓' 필요 없이 신속하게 평가하십시오 오른쪽.
에 대해 그 팬더곰이요 시행하십시오 문장열 파트리아누얼:
if not pd.isnull(atext):
for word in nltk.word_tokenize(atext):
이 함수는 누르타크 대한 기능과 추출
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