tqdm库
原创
©著作权归作者所有:来自51CTO博客作者wx636b527159a1e的原创作品,请联系作者获取转载授权,否则将追究法律责任
tqdm库
文章目录
- tqdm库
- 如何安装
- 如何使用
- 1.基于迭代的进度条
- 如何在Pandas中使用进度条
- 如何在keras中使用进度条
- 如何使用Notebook优化的进度条和层级进度条
- 如何为文件存储设置进度条
简要介绍:
tqdm是一个进度条可视化库,可以帮助我们监测程序运行的进度,估计运行的时长,甚至可以协助debug。
如何安装
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]