tqdm库

tqdm库


简要介绍:
tqdm是一个进度条可视化库,可以帮助我们监测程序运行的进度,估计运行的时长,甚至可以协助debug。

img

如何安装

CMD安装:

pip install tqdm
或pip install "git+https://github.com/tqdm/tqdm.git@devel#egg=tqdm"
anaconda prompt安装:
conda install -c conda-forge tqdm

如何使用

tqdm用途广泛,可以以多种方式使用。下面给出三个主要的。

1.基于迭代的进度条

In [2]:

from tqdm import tqdm
from time import sleep

text = ""
for char in tqdm(["a", "b", "c", "d"]):
    sleep(0.25)
    text = text + char
100%|██████████| 4/4 [00:01<00:00,  3.98it/s]

也可以是range对象

In [3]:

from tqdm import trange

for i in tqdm(range(100)):
    sleep(0.01)
100%|██████████| 100/100 [00:01<00:00, 98.62it/s]

trange(i)是针对tqdm(range(i))的优化写法,大家要多用!

In [4]:

from tqdm import trange

for i in trange(100):
    sleep(0.01)
100%|██████████| 100/100 [00:01<00:00, 98.59it/s]

也可以把tqdm写在循环外,手动控制进度条显示内容。
仔细看,进度条左边有文字描述,并且会随着循环的元素变化而变化!

In [6]:

pbar = tqdm(["a", "b", "c", "d"])
for char in pbar:
    sleep(0.25)
    pbar.set_description("Processing %s" %char)
Processing d: 100%|██████████| 4/4 [00:01<00:00,  3.97it/s]

2.手动设置进度条

手动设置看起来麻烦,但可以控制进度条的间隔;
下例的进度条是每10%跳动一次!

In [10]:

with tqdm(total=100) as pbar:
    for i in range(10):
        sleep(0.1)
        pbar.update(10)
100%|██████████| 100/100 [00:01<00:00, 99.17it/s]

也可以不适用with方法来构建pbar,但前往不要忘记在结束时del或close()

In [12]:

pbar = tqdm(total=100)
for i in range(10):
    sleep(0.1)
    pbar.update(10)
pbar.close()
100%|██████████| 100/100 [00:01<00:00, 99.14it/s]

3.CMD进度条

notebook不方便演示,不做赘述

如何在Pandas中使用进度条

In [29]:

import pandas as pd
import numpy as np
from tqdm import tqdm

df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))

tqdm.pandas(desc="my bar!")

new_df = df.progress_apply(lambda x: x**2)
#goupby之后也可以
# df.groupby(0).progress_apply(lambda x: x**2)
my bar!: 100%|██████████| 6/6 [00:00<00:00, 330.33it/s]

如何在keras中使用进度条

由于训练数据暂无,且训练时间长,所以不作展示;请需要keras进度条的童鞋脑补。

In [ ]:

from tqdm.keras import TqdmCallback

...

model.fit(..., verbose=0, callbacks=[TqdmCallback()])

如何使用Notebook优化的进度条和层级进度条

In [30]:

from tqdm.notebook import trange, tqdm
from time import sleep

for i in trange(3, desc='1st loop'):
    for j in tqdm(range(100), desc='2nd loop'):
        sleep(0.01)
HBox(children=(FloatProgress(value=0.0, description='1st loop', max=3.0, style=ProgressStyle(description_width…
HBox(children=(FloatProgress(value=0.0, description='2nd loop', style=ProgressStyle(description_width='initial…

HBox(children=(FloatProgress(value=0.0, description='2nd loop', style=ProgressStyle(description_width='initial…

HBox(children=(FloatProgress(value=0.0, description='2nd loop', style=ProgressStyle(description_width='initial…

如何为文件存储设置进度条

In [23]:

import urllib, os
from tqdm import tqdm

eg_link = "https://caspersci.uk.to/matryoshka.zip"
response = getattr(urllib, 'request', urllib).urlopen(eg_link)
with tqdm.wrapattr(open(os.devnull, "wb"), "write",
                   miniters=1, desc=eg_link.split('/')[-1],
                   total=getattr(response, 'length', None)) as fout:
    for chunk in response:
        fout.write(chunk)
matryoshka.zip: 100%|██████████| 254k/254k [00:01<00:00, 156kB/s]  
  • 7
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deng872347348

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值