除了单元格的原始输出之外,我还想获取单元格执行所花费的时间。
为此,我尝试了 %%timeit -r1 -n1
但它没有公开单元格中定义的变量。
%%time
适用于仅包含 1 条语句的单元格。
In[1]: %%time
1
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 5.96 µs
Out[1]: 1
In[2]: %%time
# Notice there is no out result in this case.
x = 1
x
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs
最好的方法是什么?
我一直在使用Execute Time in Nbextension现在已经有一段时间了。太棒了。
截至目前,this是正确答案。从本质上讲,%%time
和 %%timeit
现在都可以正常工作了。
最佳答案
我发现解决这个问题的唯一方法是使用 print 执行最后一条语句。
Do not forget that单元格魔法以 %%
开头,线条魔法以 %
开头。
%%time
clf = tree.DecisionTreeRegressor().fit(X_train, y_train)
res = clf.predict(X_test)
print(res)
请注意,在单元格内执行的任何更改都不会在下一个单元格中考虑,这在存在管道时是反直觉的:
https://stackoverflow.com/questions/32565829/