Python 关闭scikit学习';使用n_作业时的警告>;1.
我可以使用scikit learn通过几个选项关闭警告,使用Python 关闭scikit学习';使用n_作业时的警告>;1.,python,scikit-learn,suppress-warnings,Python,Scikit Learn,Suppress Warnings,我可以使用scikit learn通过几个选项关闭警告,使用warnings库: # After the imports warnings.filterwarnings(action='ignore') # Or in the code with warnings.catch_warnings(): warnings.simplefilter("ignore") # do stuff 但是,当n_jobs参数大于1时(由于多处理?),这在分类器上不起作用。以下代码示例说明了这
warnings
库:
# 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_作业>1,如何禁用警告
编辑:由于它可能与
多处理相关
,我可以补充一点,我在linux上用python 3.6运行了此脚本。我无法重现您的警告,请您交叉检查。我仔细检查了一下,您是否使用linux和python 3.6?windows n py 3.6当然,为什么您没有得到相同的输出,windows上的多处理与linux无关。