我在Python中有一个进度条:
import os
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
import progressbar
from time import sleep
bar = progressbar.ProgressBar(maxval=file_len(os.path.basename(__file__)), \
widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
for i in range(file_len(os.path.basename(__file__))):
bar.update(i+1)
sleep(0.1)
bar.finish()
进度条在视觉上按预期工作:它显示条形图中的进度及其背后的百分比。但是,进度条的计算不正确。
它首先显示百分比增加的栏,但当栏为100%时,程序开始运行。我在我的代码中放了几个'print'定义来查看当时代码的位置,但现在我首先看到百分比栏增加到100%,然后是代码中的第一个'print'。在此打印之后,代码开始计算。
有人知道我在这里做错了吗?
问候,Ganesh
因此,当栏位为100%时,训练部分开始。
答案 0 :(得分:0)
Etene,我有我在这里的代码。我删除了我试过的进度条,所以也许你可以尝试一下。
import os
import numpy as np
from sklearn import cross_validation
import matplotlib.pyplot as plt
import seaborn as sb
import pylab as pl
from termcolor import colored
from sklearn import datasets
""" ===================================== Create New Folders ===================================== """
def create_folder(path):
if not os.path.exists(path):
os.makedirs(path)
return path
current_dir = os.getcwd()
path_graph = create_folder(current_dir + "\\Graphs")
""" ==================================== Function Information ==================================== """
def graph_heatmap(Plot, Filename, Annot, Mask_half):
print (colored('---- Creating Heatmap: ' + Filename, 'blue'))
plt.gcf().clear()
plt.subplots(figsize=(40,40))
mask = np.zeros_like(Plot)
mask[np.triu_indices_from(mask)] = Mask_half
sb.heatmap(Plot, annot=Annot, mask=mask, cmap="Blues", linewidths=0.5, linecolor='black', vmin=0, vmax=1)
pl.savefig(path_graph + '\\' + Filename + '.png', bbox_inches='tight', dpi=600)
plt.close()
""" ================================= Importing & Filtering Files ================================ """
diabetes = datasets.load_diabetes()
""" ======================================== Data Mining ======================================== """
#Training
print (colored('---- Training', 'blue'))
X = diabetes.data[:, np.newaxis, 2]
y = diabetes.target
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size = 0.3, random_state=25)
这个比我的小,但结构是一样的。我希望进度条在导入模数后直接启动,并在最后一行结束(当一切都完成时)。