我正在使用一个np.vectorize-艾德函数,并希望看到该函数与tqdm的进展。但是,我一直没能弄清楚如何做到这一点。我找到的所有建议都与将计算转换为for循环或pd. DataFrame有关。
np.vectorize
tqdm
wgx48brx1#
我终于找到了一个方法,它可以使用np.vectorize函数来更新tqdm进度条。我使用with Package 了vectorize函数
with tqdm(total=len(my_inputs)) as pbar: my_output = np.vectorize(my_function)(my_inputs)
with tqdm(total=len(my_inputs)) as pbar:
my_output = np.vectorize(my_function)(my_inputs)
字符串在my_function()中添加以下行
global pbarpbar.update(1)
global pbar
pbar.update(1)
型瞧!我现在有了一个随每次迭代更新的进度条。我的代码只有轻微的性能下降。注意:当你示例化函数时,它可能会抱怨pbar还没有定义。在示例化之前只需输入一个pbar =,然后该函数将调用由with定义的pbar希望对大家阅读这里有所帮助。
p8h8hvxi2#
根据@Carl Kirstein的回答,我提出了以下解决方案。我将pbar元素作为参数添加到my_function中,并在函数中对其进行了更新。
my_function
with tqdm(total=len(my_inputs)) as pbar: my_output = np.vectorize(my_function)(my_inputs, pbar)
my_output = np.vectorize(my_function)(my_inputs, pbar)
字符串在my_function中的某个地方添加了pbar.update(1)。
def my_function(args, pbar): ... pbar.update(1) ...
def my_function(args, pbar):
...
型
zc0qhyus3#
据我所知,tqdm不会覆盖numpy.vectorize。要显示numpy数组的进度条,可以使用numpy.ndenumerate。给定输入和函数:
numpy.vectorize
numpy.ndenumerate
import numpy as npfrom tqdm import tqdma = np.array([1, 2, 3, 4])b = 2def myfunc(a, b): "Return a-b if a>b, otherwise return a+b" if a > b: return a - b else: return a + b
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.vectorizevfunc = np.vectorize(myfunc)vfunc(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))]
# using numpy.ndenumerate instead
[myfunc(x,b) for index, x in tqdm(np.ndenumerate(a))]
型查看tqdm的进度。
3条答案
按热度按时间wgx48brx1#
我终于找到了一个方法,它可以使用np.vectorize函数来更新tqdm进度条。我使用with Package 了vectorize函数
字符串
在my_function()中添加以下行
型
瞧!我现在有了一个随每次迭代更新的进度条。我的代码只有轻微的性能下降。
注意:当你示例化函数时,它可能会抱怨pbar还没有定义。在示例化之前只需输入一个pbar =,然后该函数将调用由with定义的pbar
希望对大家阅读这里有所帮助。
p8h8hvxi2#
根据@Carl Kirstein的回答,我提出了以下解决方案。我将pbar元素作为参数添加到
my_function
中,并在函数中对其进行了更新。字符串
在
my_function
中的某个地方添加了pbar.update(1)
。型
zc0qhyus3#
据我所知,
tqdm
不会覆盖numpy.vectorize
。要显示numpy数组的进度条,可以使用
numpy.ndenumerate
。给定输入和函数:
字符串
替换下面的矢量化部分
型
用这个
型
查看
tqdm
的进度。