GridSearchCV:每次函数完成循环时打印一些表达式 [英] GridSearchCV: print some expression each time a function completes a loop

查看:531
本文介绍了GridSearchCV:每次函数完成循环时打印一些表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在Python中有一些通过循环工作的函数function:例如,它可以是计算某个数学表达式的函数,例如x**2,用于数组中的所有元素,例如([1, 2, ..., 100])(显然,这是一个玩具示例).是否可以编写这样的代码,使得每次function循环并获得结果时,都会执行一些代码,例如print("Loop %s has been executed" % i)?因此,在我们的示例中,计算出x**1时,程序将打印Loop 1 has been executed,然后计算出x**2时,它将打印Loop 2 has been executed,依此类推.

Assume you have some function function in Python that works by looping: for example it could be a function that evaluates a certain mathematical expression, e.g. x**2, for all elements from an array, e.g. ([1, 2, ..., 100]) (obviously this is a toy example). Would it be possible to write a code such that, each time function goes through a loop and obtains a result, some code is executed, e.g. print("Loop %s has been executed" % i)? So, in our example, when x**1 has been computed, the program prints Loop 1 has been executed, then when x**2 has been computed, it prints Loop 2 has been executed, and so on.

请注意,困难来自于我没有对该函数进行编程的事实,它是某些软件包中预先存在的函数(更具体地说,我感兴趣的函数将是软件包scikit learn中的GridSearchCV). /p>

Note that the difficulty comes from the fact that I do not program the function, it is a preexisting function from some package (more specifically, the function I am interested in would be GridSearchCV from package scikit learn).

推荐答案

最简单的方法是将函数的代码复制到自己的函数中,对其进行调整,然后再使用它.对于您的情况,您必须继承GridSearchCV的子类并覆盖_fit方法.这种方法的问题在于它可能无法在软件包升级中幸免.

The easiest way to do this would be to just copy the function's code into your own function, tweak it, and then use it. In your case, you would have to subclass GridSearchCV and override the _fit method. The problem with this approach is that it may not survive a package upgrade.

对于您而言,这不是必需的.您可以在创建对象时指定详细级别:

In your case, that's not necessary. You can just specify a verbosity level when creating the object:

GridSearchCV(verbose=100)

我不太确定详细程度数字本身是什么意思.这是来自用于打印的内部包的文档:

I'm not entirely sure what the verbosity number itself means. Here's the documentation from the package used internally that does the printing:

详细级别:如果不为零,则显示进度消息.高于50时,输出将发送到stdout.消息的频率随着详细程度而增加.如果大于10,则报告所有迭代.

The verbosity level: if non zero, progress messages are printed. Above 50, the output is sent to stdout. The frequency of the messages increases with the verbosity level. If it more than 10, all iterations are reported.

您可以在源代码中查找 如果您真的想知道verbosity号的作用.我不知道.

You can look at the source code if you really want to know what the verbosity number does. I can't tell.

这篇关于GridSearchCV:每次函数完成循环时打印一些表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆