我在大熊貓系列上做的比較沉重。有沒有什麼辦法可以找回一些打印反饋信息,說明在每次調用函數時,在函數內部打印多少內容?大理大熊貓適用
Q
大理大熊貓適用
1
A
回答
3
你可以用跟蹤包裝你的功能。下面有兩個例子,一個基於完成的迭代次數,另一個基於總工作的百分比。
from pandas import Series
def progress_coroutine(print_on = 10000):
print "Starting progress monitor"
iterations = 0
while True:
yield
iterations += 1
if (iterations % print_on == 0):
print "{} iterations done".format(iterations)
def percentage_coroutine(to_process, print_on_percent = 0.10):
print "Starting progress percentage monitor"
processed = 0
count = 0
print_count = to_process*print_on_percent
while True:
yield
processed += 1
count += 1
if (count >= print_count):
count = 0
pct = (float(processed)/float(to_process))*100
print "{}% finished".format(pct)
def trace_progress(func, progress = None):
def callf(*args, **kwargs):
if (progress is not None):
progress.send(None)
return func(*args, **kwargs)
return callf
def my_func(i):
return i * 2
data_series = Series(xrange(100000))
co1 = progress_coroutine()
co1.next()
co2 = percentage_coroutine(len(data_series))
co2.next()
data_series.apply(trace_progress(my_func, progress = co1))
data_series.apply(trace_progress(my_func, progress = co2))
Starting progress monitor
Starting progress percentage monitor
10000 iterations done
20000 iterations done
30000 iterations done
40000 iterations done
50000 iterations done
60000 iterations done
70000 iterations done
80000 iterations done
90000 iterations done
100000 iterations done
10.0% finished
20.0% finished
30.0% finished
40.0% finished
50.0% finished
60.0% finished
70.0% finished
80.0% finished
90.0% finished
100.0% finished
+0
完美,謝謝:) –
+0
不客氣。很高興我能幫上忙。 – Clockw3rk
0
我對上述Python 3的代碼進行了更改,以防萬一任何人想使用。謝謝@ Clockw3rk爲上述答案
from pandas import Series
def progress_coroutine(print_on = 10):
print ("Starting progress monitor")
iterations = 0
while True:
yield
iterations += 1
if (iterations % print_on == 0):
print ("{} iterations done".format(iterations))
def percentage_coroutine(to_process, print_on_percent = 0.10):
print ("Starting progress percentage monitor")
processed = 0
count = 0
print_count = to_process*print_on_percent
while True:
yield
processed += 1
count += 1
if (count >= print_count):
count = 0
pct = (float(processed)/float(to_process))*100
print ("{}% finished".format(pct))
def trace_progress(func, progress = None):
def callf(*args, **kwargs):
if (progress is not None):
progress.send(None)
return func(*args, **kwargs)
return callf
def my_func(i):
return i ** 2
data_series = Series(list(range(100)))
co1 = progress_coroutine()
co2 = percentage_coroutine(len(data_series))
data_series.apply(trace_progress(my_func, progress = co1))
data_series.apply(trace_progress(my_func, progress = co2))
Starting progress monitor
10 iterations done
20 iterations done
30 iterations done
40 iterations done
50 iterations done
60 iterations done
70 iterations done
80 iterations done
90 iterations done
Starting progress percentage monitor
10.0% finished
20.0% finished
30.0% finished
40.0% finished
50.0% finished
60.0% finished
70.0% finished
80.0% finished
90.0% finished
Out[12]:
0 0
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
11 121
12 144
13 169
14 196
15 225
16 256
17 289
18 324
19 361
20 400
21 441
22 484
23 529
24 576
25 625
26 676
27 729
28 784
29 841
...
70 4900
71 5041
72 5184
73 5329
74 5476
75 5625
76 5776
77 5929
78 6084
79 6241
80 6400
81 6561
82 6724
83 6889
84 7056
85 7225
86 7396
87 7569
88 7744
89 7921
90 8100
91 8281
92 8464
93 8649
94 8836
95 9025
96 9216
97 9409
98 9604
99 9801
Length: 100, dtype: int64
相關問題
- 1. 大熊貓適用於螺紋
- 2. 熊貓適用於大型csv文件
- 3. 大熊貓列表理解
- 4. 大熊貓與熊貓
- 5. 在大熊貓
- 6. 熊貓大CSV
- 7. 與大熊貓
- 8. 在大熊貓
- 9. 在大熊貓
- 10. 在大熊貓
- 11. 大熊貓
- 12. 在大熊貓
- 13. 在大熊貓
- 14. 在大熊貓
- 15. 在大熊貓
- 16. 在大熊貓
- 17. 與大熊貓
- 18. 在大熊貓
- 19. 大熊貓
- 20. 大熊貓由
- 21. 在大熊貓
- 22. 在大熊貓
- 23. 對大熊貓
- 24. 與大熊貓
- 25. 與大熊貓
- 26. 在大熊貓
- 27. 異常處理的大熊貓。適用()函數
- 28. 大熊貓 - 在大塊
- 29. 大熊貓 - 最大value_counts
- 30. Python的大熊貓:
我不這麼認爲,不。 – BrenBarn
這是不幸的,謝謝 –