在阅读从SQL数据库到pandas数据帧的大关系时,有一个进度条会很好,因为元组的数量是静态已知的,并且可以估计I/O速率.看起来该tqdm模块具有一个函数tqdm_pandas,该函数将报告映射函数在列上的进度,但默认情况下调用它不会像这样报告I/O上的进度.是否可以tqdm在通话中使用进度条pd.read_sql?
我可以找到一些解释如何使用包的文档tqdm,但从中我无法弄清楚在线下载数据时如何生成进度表。
下面是我从 ResidentMario 复制的用于下载数据的示例代码
def download_file(url, filename):
"""
Helper method handling downloading large files from `url` to `filename`. Returns a pointer to `filename`.
"""
r = requests.get(url, stream=True)
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
return filename
dat = download_file("https://data.cityofnewyork.us/api/views/h9gi-nx95/rows.csv?accessType=DOWNLOAD",
"NYPD Motor Vehicle Collisions.csv")
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何在这里使用 tqdm 包来显示下载进度?
谢谢
我使用 GitPython 在我的程序中克隆一个存储库。我想出了如何使用 clone_from 命令显示克隆的状态,但我希望状态看起来更像 tqdm 进度条。我尝试使用 requests 库来获取文件的大小,但我仍然不确定如何实现它。尝试做下面类似的事情,但它不起作用。任何帮助表示赞赏,谢谢。
url = 'git@github.com:somegithubrepo/repo.git'
r = requests.get(url, stream=True)
total_length = r.headers.get('content-length')
for i in tqdm(range(len(total_length??))):
git.Git(pathName).clone(url)
Run Code Online (Sandbox Code Playgroud) 我正在使用我的 lightgbm 模型进行交叉验证,如下所示。
我想tqdm在 for 循环中使用,以便我可以检查过程。
folds = KFold(n_splits=num_folds, random_state=2319)
oof = np.zeros(len(train))
getVal = np.zeros(len(train))
predictions = np.zeros(len(target))
feature_importance_df = pd.DataFrame()
print('Light GBM Model')
for fold_, (trn_idx, val_idx) in enumerate(folds.split(train.values, target.values)):
X_train, y_train = train.iloc[trn_idx][features], target.iloc[trn_idx]
X_valid, y_valid = train.iloc[val_idx][features], target.iloc[val_idx]
print("Fold idx:{}".format(fold_ + 1))
trn_data = lgb.Dataset(X_train, label=y_train, categorical_feature=categorical_features)
val_data = lgb.Dataset(X_valid, label=y_valid, categorical_feature=categorical_features)
clf = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=5000, early_stopping_rounds = 4000)
oof[val_idx] = clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)
getVal[val_idx]+= clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration) / folds.n_splits …Run Code Online (Sandbox Code Playgroud) 我不确定为什么当我使用该bar_format选项添加颜色时,我的 TQDM 进度条会分成多行。这似乎也与迭代次数有关,因为当我仅运行 10 次迭代(而不是 1389 次迭代)的相同代码时,它不会分裂(参见图片)。另外,当我运行相同的代码而不修改条形颜色时,它工作得很好。
from tqdm import tqdm
from colorama import Fore
dnames = [...] # List of directories
cmap = [ # List of colors, same length as `dnames`
'\x1b[38;5;231m',
'\x1b[38;5;194m',
'\x1b[38;5;151m',
'\x1b[38;5;114m',
'\x1b[38;5;71m',
'\x1b[38;5;29m',
'\x1b[38;5;22m',
'\x1b[38;5;22m',
'\x1b[38;5;22m',
'\x1b[38;5;22m'
# ...may include many more colors
]
# Initialize progress bar and color variable
pbar = tqdm(dnames, unit='dir')
current_color = None
for i, dname in enumerate(dnames):
# Update color of pbar if different from last …Run Code Online (Sandbox Code Playgroud) 我正在尝试迭代包含近一百万个条目的 Pandas 数据框。我正在使用 for 循环来迭代它们。以下面的代码为例
import pandas as pd
import os
from requests_html import HTMLSession
from tqdm import tqdm
import time
df = pd.read_csv(os.getcwd()+'/test-urls.csv')
df = df.drop('Unnamed: 0', axis=1 )
new_df = pd.DataFrame(columns = ['pid', 'orig_url', 'hosted_url'])
refused_df = pd.DataFrame(columns = ['pid', 'refused_url'])
tic = time.time()
for idx, row in df.iterrows():
img_id = row['pid']
url = row['image_url']
#Let's do scrapping
session = HTMLSession()
r = session.get(url)
r.html.render(sleep=1, keep_page=True, scrolldown=1)
count = 0
link_vals = r.html.find('.zoomable')
if len(link_vals) != 0 : …Run Code Online (Sandbox Code Playgroud) 如何将 tqdm 用于 data_loader ?
这是正确的方法吗?
for i,j in enumerate(data_loader,total = 100):
pass
Run Code Online (Sandbox Code Playgroud) 我在脚本中使用 tqdm 两次,第一次工作正常,但第二次它仅在 14 次迭代后更新。如果我删除所有其他打印语句也是一样的。知道可能出了什么问题吗?
\n程序:
\nfor name in tqdm(final_urls):\n print(f"Downloading {name} files...")\n url_list = final_urls[name]\n class_dir = os.path.join("data", name)\n\n if not os.path.isdir(class_dir):\n os.mkdir(class_dir)\n\n for idx, url in enumerate(url_list):\n filepath = os.path.join(class_dir, f"audio{idx}.wav")\n if not os.path.isfile(filepath):\n r = requests.get(url)\n with open(filepath, "wb+") as f:\n f.write(r.content)\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nDownload files...\n 0%| | 0/32 [00:00<?, ?it/s]Downloading Bearded Seal files...\nDownloading Bottlenose Dolphin files...\nDownloading Rough-Toothed Dolphin files...\nDownloading Common Dolphin files...\nDownloading Striped Dolphin files...\nDownloading Fin, Finback Whale files...\nDownloading Melon Headed Whale files...\nDownloading …Run Code Online (Sandbox Code Playgroud) 我该怎么做:
from tqdm.notebook import tqdm
from matplotlib import pyplot as plt
from IPython import display
import time
import numpy as np
xx = list()
for i in tqdm(range(500)):
xx.append(i * 0.1)
yy = np.sin(xx)
if i % 10 == 0:
display.clear_output(wait=True)
plt.plot(xx, yy)
time.sleep(0.1)
Run Code Online (Sandbox Code Playgroud)
但是tqdm当我更新情节时防止进度条消失?
我有以下代码create_data()引用了我之前已经定义的函数。
%%time
from tqdm import tqdm
from multiprocessing import Pool
import pandas as pd
import os
with Pool(processes=os.cpu_count()) as pool:
results = pool.map(create_data, date)
data = [ent for sublist in results for ent in sublist]
data = pd.DataFrame(data, columns = cols)
data.to_csv("%s"%str(date), index=False)
Run Code Online (Sandbox Code Playgroud)
我基本上想create_data()在传递日期参数的同时打电话。然后获得的所有结果将被收集到results变量中。然后我会将它们全部合并到一个列表中并将其转换为数据框。该函数create_data计算量较大,计算时间较长。这就是为什么我需要进度条来查看进程。
我尝试将该行更改为以下内容。
results = list(tqdm(pool.map(create_od, date), total = os.cpu_count()))
Run Code Online (Sandbox Code Playgroud)
但它似乎不起作用。我已经等了很长一段时间了,没有进度条出现。我在这里该怎么办?
python ×10
tqdm ×10
progress-bar ×3
pandas ×2
matplotlib ×1
printing ×1
python-3.x ×1
pytorch ×1