将GridSearchCV(或任何其他Sklearn对象)输出重定向到文件



我希望能够在运行时将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()

并尝试了作者所说的猴子补丁,但也只是保存了"平行的";线

(只是强调一下,上面的代码只是所提出的解决方案的一部分,当我尝试它们时,我采用了所有相关的代码(。

是否有保存ALL输出的方法?

我不知道您是否可以使用sys库或其他库来实现这一点。相反,我建议使用以下方法来正确重定向stdout和stderr。

假设你有一个这样的脚本:

测试.py

import numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
params = {"C": [0.001, 0.01, 0.1, 1, 2, 3]}
grid = GridSearchCV(model, params, n_jobs=-1, verbose=10)
X = np.random.randn(100, 10)
y = np.random.randint(0, 2, 100)
grid.fit(X, y)

然后用运行

python test.py > logfile.txt 2>&1

那么你将同时拥有";平行的";以及";CV";logfile.txt:中的行

Fitting 5 folds for each of 6 candidates, totalling 30 fits
[Parallel(n_jobs=-1)]: Using backend LokyBackend with 12 concurrent workers.
[Parallel(n_jobs=-1)]: Done   1 tasks      | elapsed:    1.6s
[Parallel(n_jobs=-1)]: Done  11 out of  30 | elapsed:    1.7s remaining:    2.9s
[Parallel(n_jobs=-1)]: Done  15 out of  30 | elapsed:    1.7s remaining:    1.7s
[Parallel(n_jobs=-1)]: Done  19 out of  30 | elapsed:    1.7s remaining:    1.0s
[Parallel(n_jobs=-1)]: Done  23 out of  30 | elapsed:    1.7s remaining:    0.5s
[Parallel(n_jobs=-1)]: Done  27 out of  30 | elapsed:    1.7s remaining:    0.2s
[Parallel(n_jobs=-1)]: Done  30 out of  30 | elapsed:    1.7s finished
[CV] C=0.001 .........................................................
[CV] ............................. C=0.001, score=0.500, total=   0.0s
[CV] C=0.1 ...........................................................
[CV] ............................... C=0.1, score=0.450, total=   0.0s
[CV] C=0.1 ...........................................................
[CV] ............................... C=0.1, score=0.550, total=   0.0s
[CV] C=1 .............................................................
[CV] ................................. C=1, score=0.550, total=   0.0s
[CV] C=1 .............................................................
[CV] ................................. C=1, score=0.500, total=   0.0s
[CV] C=2 .............................................................
...

详细信息

";[CV]";行由print语句生成(来源(。这是写入stdout的。

并且";平行的";行由记录器(Source(生成。这是写入stderr的。

> logfile.txt 2>&1是将stdout和stderr重定向到同一个文件的技巧(相关问题(。因此,两条消息都被写入同一个文件。

最新更新