我可以使用warnings
库通过scikit-learn通过几个选项关闭警告:
# After the imports
warnings.filterwarnings(action='ignore')
# Or in the code
with warnings.catch_warnings():
warnings.simplefilter("ignore")
# do stuff
但是,一旦n_jobs参数大于1(由于要进行多处理?),则对分类器不起作用。以下代码示例对此进行了说明:
import numpy as np
from sklearn.multiclass import OneVsRestClassifier
from sklearn.linear_model import LogisticRegression
import warnings
import logging
logger = logging.getLogger()
for n_job in [1, 2]:
print("START")
print("n_jobs =", n_job)
clf = OneVsRestClassifier(LogisticRegression(solver="liblinear", multi_class="ovr"), n_jobs=n_job)
x_train = np.array([[1,1], [0,1], [0,0], [1,5], [2,1], [3,1]])
y_train = np.array([[False, False, True], [False, False, True], [True, False, False], [True, False, False], [True, False, True], [True, False, False]])
with warnings.catch_warnings():
warnings.simplefilter("ignore")
clf.fit(x_train, y_train) # "UserWarning: Label not 1 is present in all training examples."
print("END")
print()
输出:
START
n_jobs = 1
END
START
n_jobs = 2
UserWarning: Label not 1 is present in all training examples.
END
即使n_jobs> 1,如何禁用警告?
编辑:由于可能与multiprocessing
相关,所以我可能要补充一点,我在Linux上使用python 3.6运行了该脚本。