我希望能够在运行时将 GridSearchCV 输出保存到文件中。
GridSearchCV(XGBClassifier(), tuned_parameters, cv=cv, n_jobs=-1, verbose=10)
这是一个输出示例:
Fitting 1 folds for each of 200 candidates, totalling 200 fits
[Parallel(n_jobs=-1)]: Using backend with 4 concurrent workers.
[CV] colsample_bytree=0.7, learning_rate=0.05, max_depth=4, n_estimators=300, subsample=0.7
[CV] colsample_bytree=0.7, learning_rate=0.05, max_depth=4, n_estimators=300, subsample=0.7
score=0.645, total= 6.3min
[Parallel(n_jobs=-1)]: Done 1 tasks | elapsed: 6.3min
我设法保存了第一行和平行线,但无论我尝试什么,我都无法保存以 [CV] 开头的行。我想保存这些行,所以如果程序失败,我至少可以看到部分结果。
我从这里尝试了解决方案
sys.stdout = open('file', 'w')
和:
with open('help.txt', 'w') as f:
with redirect_stdout(f):
print('it now prints to `help.text`')
class Tee(object):
def __init__(self, *files):
self.files = files
def write(self, obj):
for f in self.files:
f.write(obj)
f.flush() # If you want the output to be visible immediately
def flush(self) :
for f in self.files:
f.flush()
并尝试了作者所说的这个猴子补丁,但也只是保存了“平行”行。
(只是强调一下,上面的代码只是提出的解决方案的一瞥,当我尝试它们时,我拿走了所有相关的代码)。
有没有办法保存所有输出?