首页 > 解决方案 > 将 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()

并尝试了作者所说的这个猴子补丁,但也只是保存了“平行”行。

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

有没有办法保存所有输出?

标签: pythonloggingscikit-learngridsearchcvconsole-output

解决方案


我不知道你是否可以使用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

然后,您将同时拥有“Parallel”和“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语句(Source)生成。这被写入标准输出。

并且“平行”线由记录器(Source)产生。这被写入标准错误。

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


推荐阅读