2016-07-10 13 views
0

sklearn에서 몇 가지 기성품 분류기를 만들었습니다. 분류기가 잘못 수행되고 올바르게 예측되지 않을 것으로 예상되는 몇 가지 예상 시나리오가 있습니다. sklearn.svm 패키지는 오류없이 실행되지만 다음 경고가 발생합니다.Python - 경고를 간단한 메시지로 바꾸기

~/anaconda/lib/python3.5/site-packages/sklearn/metrics/classification.py:1074: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples. 
    'precision', 'predicted', average, warn_for) 

나는이 경고를 억제하고 대신 stdout에 메시지로 교체, 예를 들어 "poor classifier performance" 말을하고 싶습니다.

일반적으로 warnings을 억제하는 방법이 있습니까? 모든 경고를 억제

답변

2

는 (당신의 경고 유형을 무시)

warnings 모듈은 필터를 사용하여 약간의 미세한 조정을 할 수있다 (warning flag docs 참조) -Wignore 용이합니다.

(를 조정할 수있는 모듈의 일부 API가없는 가정) 단지 당신의 경고를 캡처하고 "Testing Warnings"에서 적응 warnings.catch_warnings context manager 및 코드를 사용하여 수행 할 수있는 뭔가 특별한 일 :

import warnings 

class MyWarning(Warning): 
    pass 

def something(): 
    warnings.warn("magic warning", MyWarning) 

with warnings.catch_warnings(record=True) as w: 
    # Trigger a warning. 
    something() 
    # Verify some things 
    if ((len(w) == 1) 
      and issubclass(w[0].category, MyWarning) 
      and "magic" in str(w[-1].message)): 
     print('something magical') 
+0

했다. 스 니펫이 생성하고 일반 메시지로보고 할 수있는 모든 경고를 캡처하기로 결정했습니다. self.clfObj.fit (self.train_x, self.train_y) self.preds = 목록 (self.clfObj.predict (self.test_x)) 자기 : warnings.catch_warnings w로 (기록 = 참)와 ' .predProbabs = self.clfObj.predict_proba (self.test_x) [:, 1] self.valClassifierPerf ( ) len (w)> = 1 : print ("Poor Classifer detected" –