gpt4 book ai didi

python - tqdm 和 numpy 向量化

转载 作者:行者123 更新时间:2023-12-03 13:40:51 25 4
gpt4 key购买 nike

我正在使用 np.vectorize -ed 函数,并希望通过 tqdm 查看函数的进度.但是,我一直无法弄清楚如何做到这一点。

我发现的所有建议都与将计算转换为 for 循环或 pd.DataFrame 相关。

最佳答案

据我所知,tqdm不包裹numpy.vectorize .

要显示 numpy 数组的进度条,numpy.ndenumerate可以使用。

给定输入和函数:

import numpy as np
from tqdm import tqdm

a = np.array([1, 2, 3, 4])
b = 2
def myfunc(a, b):
"Return a-b if a>b, otherwise return a+b"
if a > b:
return a - b
else:
return a + b

替换下面这个矢量化部分

# using numpy.vectorize
vfunc = np.vectorize(myfunc)
vfunc(a, b)

有了这个

# using numpy.ndenumerate instead
[myfunc(x,b) for index, x in tqdm(np.ndenumerate(a))]

tqdm进步。

关于python - tqdm 和 numpy 向量化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57797533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com