带有tqdm的python进度条

Not long after I began working on machine learning projects in Python, I ran into computationally-intensive tasks that just took a long time to run. Usually this was associated with some kind of iterable process. A couple that immediately come to mind are (1) running a grid search on p, d, and q orders to fit ARIMA models on large data sets, and (2) grid searching hyperparameters while training machine learning algorithms. In both cases, you can potentially spend hours (or more!) waiting for your code to finish running. Desperate for some kind of indicator to show the progress of these tasks, I found tqdm.

在开始使用Python进行机器学习项目后不久,我遇到了需要大量时间才能运行的计算密集型任务。 通常,这与某种可迭代的过程相关。 马上想到的一对夫妇是(1)在p,d和q阶上运行网格搜索以适合大型数据集上的ARIMA模型,以及(2)在训练机器学习算法时进行网格搜索超参数。 在这两种情况下,您都可能花费数小时(或更多!)等待代码完成运行。 迫切需要某种指示器来显示这些任务的进度,我找到了tqdm

什么是tqdm? (What is tqdm?)

tqdm is a Python library that allows you to output a smart progress bar by wrapping around any iterable. A tqdm progress bar not only shows you how much time has elapsed, but also shows the estimated time remaining for the iterable.

tqdm是一个Python库,通过包装可迭代的对象,您可以输出智能进度栏。 一个tqdm进度栏不仅显示您已经花费了多少时间,还显示了可迭代对象的估计剩余时间。

安装和导入tqdm (Installing and importing tqdm)

Since tqdm is part of the Python Package Index (PyPI), it can be installed using the pip install tqdm command.

由于tqdm是Python软件包索引( PyPI )的一部分,因此可以使用pip install tqdm命令进行安装。

I tend to work often in IPython/Jupyter notebooks, and tqdm provides excellent support for this. To begin playing with tqdm in a notebook, you can import the following:

我倾向于经常在IPython / Jupyter笔记本上工作,而tqdm为此提供了出色的支持。 要开始在笔记本中使用tqdm播放,可以导入以下内容:

例子 (Examples)

For the sake of clarity, I won’t get into a computationally-intensive grid search in this post — instead I’ll use a few simple examples to demonstrate the use of tqdm.

为了清楚起见,本文不会涉及计算密集型的网格搜索,而是将使用一些简单的示例来演示tqdm的用法

For-loop progress

循环进度

Let’s say we wanted to simulate flipping a fair coin 100,000,000 times while tracking the results, and we also wanted to see how long these iterations will take to run in Python. We can wrap the tqdm function around the iterable (range(100000000)), which will generate a progress bar while our for-loop is running. We can also assign a name to the progress bar using the desc keyword argument.

假设我们要在跟踪结果的同时模拟翻转一枚公平硬币100,000,000次,并且还希望了解这些迭代在Python中运行需要多长时间。 我们可以将tqdm函数包装在可迭代(range(100000000))周围,当for循环运行时,它将生成进度条。 我们还可以使用desc关键字参数为进度栏分配名称。

Image for post

The resulting tqdm progress bar gives us information that includes the task completion percentage, number of iterations complete, time elapsed, estimated time remaining, and the iterations completed per second.

生成的tqdm进度栏为我们提供了以下信息:任务完成百分比,完成的迭代次数,经过的时间,估计的剩余时间以及每秒完成的迭代。

In this case, tqdm allows for further optimization by using trange(100000000) in place of the tqdm(range(100000000)).

在这种情况下, tqdm可以通过使用trange(100000000)代替tqdm(range(100000000))进行进一步优化。

Image for post

Nested for-loop progress

嵌套的循环进度

If you have a situation that calls for a nested for-loop, tqdm allows you to track the progress of these loops at multiple levels. For example, let’s take our coin-flip example, but this time we want to play three separate “games” of 10,000,000 flips each while tracking the results. We can create a tqdm progress bar for “Overall Progress”, as well as progress bars for each of the three games.

如果您遇到需要嵌套的for循环的情况,则tqdm允许您在多个级别上跟踪这些循环的进度。 例如,让我们以硬币翻转为例,但是这次我们想在跟踪结果的同时玩三个单独的“游戏”,每次10,000,000次翻转。 我们可以为“总体进度”创建一个tqdm进度栏,以及三个游戏中每个游戏的进度栏。

带有tqdm的python进度条_第1张图片

熊猫整合 (Pandas Integration)

A slightly different implementation of tqdm involves integration with pandas. tqdm can provide additional functionality for the .apply() method of a pandas dataframe. The pandas .progress_apply() method must first be ‘registered’ with tqdm using the code below. Then, the .progress_apply() method is used instead of the traditional .apply() method — the difference is, we now have a smart progress bar included in the method’s output.

tqdm的实现略有不同,涉及与熊猫集成。 tqdm可以为熊猫数据框的.apply()方法提供其他功能。 必须首先使用以下代码在tqdm中“注册” pandas .p​​rogress_apply()方法。 然后,使用.progress_apply()方法代替传统的.apply()方法-区别在于,我们现在在该方法的输出中包含一个智能进度栏。

Processing Dataframe: 100%|██████████| 1000/1000 [00:02<00:00, 336.21it/s]
带有tqdm的python进度条_第2张图片

其他tqdm集成 (Additional tqdm integrations)

In addition to being integrated with IPython/Jupyter and pandas, tqdm offers integration with Keras and experimental modules for itertools, concurrent, Discord, and Telegram. This post only scratches the surface of the capabilities of tqdm, so be sure to check out the documentation to learn more about how to include smart progress bars in your Python code!

除了与IPython的/ Jupyter熊猫,使用tqdm提供一体化集成Keras和实验模块itertools同时纷争,和电报。 这篇文章仅涉及tqdm功能的表面,因此请务必查看文档以了解更多有关如何在Python代码中包括智能进度条的信息!

翻译自: https://towardsdatascience.com/progress-bars-for-python-with-tqdm-4dba0d4cb4c

你可能感兴趣的:(python)