pip install line_profiler
You can find more information about
line_profiler in its GitHub repo.
Sencondly, run this command (which is called magic) in a Jupyter notebook cell:
This line is used to “import” line_profiler
Thirdly, run this line to profile function A when running function B:
%lprun -f A B(parameters)
For example, if I have a function called
sleep and it’s defined as following:
def sleep(count): for i in range(count): time.sleep(1) print(i)
Then if I want to profile this function, I just need to run
%lprun -f sleep sleep(5).
The full example are shown in this image:
In default mode, Jupyter will show the result of the last line if it’s not be assigned to a variable.
To show all such results, you can add these two lines into the begining of your notebook.
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all"
Using Latex Math
You can only use Latex in markdown cell. Then things are the same with when using it in markdown. There are two ways to use it:
- independent mode: put your equationships into the middle of
$$x^2+y^2=1$$, then the output should be an equationship in a new line:
- inline mode: put your equationship into the middle of
$x^2+y^2=1$. Then the output should be \(x^2+y^2=1\). Notice it’s inline mode
Sometimes there may be some warnings and you decide just to ignore them. To make it looks better you could add these two lines into the begining of you code:
import warnings warnings.filterwarnings('ignore')
Basically, what you need is just install them with the following commands.
pip install yapf jupyter nbextension install https://github.com/jfbercher/yapf_ext/archive/master.zip --user jupyter nbextension enable yapf_ext-master/yapf_ext
By saying interactive plot, we mean the tool provided by matplotlib which allows us to drag, zoom or ajust the figure we plot. To use it in Jupyter, you need to add and excute this magic command before you run the plot code:
That’s it! Enjoy the interactive plot now.
Show data in table
Sometimes we may need to show the data in a table to make it vivid and clear for readers. Basically, there are two ways to do that.
- Use a package named tabulate. It allows you to transform the two dimention list into a HTML table. And then call Jupyter’s
displayfunction to show it.
Suppose our data is a two dimension list
data=[[1,2],[3,4],[5,6]]. Then we can show it by
- Use the dataframe of the package pandas. Pandas is a famous package to handle table data. When we convert our data to pandas dataframe, we can just print it. Jupyter provides us with the proper way to show it.
So using pandas, wen can convert our data by:
df = pd.DataFrame(data,columns=["X", "Y"])
Draw for retina screen
Sometimes you may want to make the figure more clear in your retina screen. What you need to do is only add one line of magic code:
%config InlineBackend.figure_format = 'retina'
Shortcuts can accelerate our coding speed. Some basical shortcuts are shown in the following:
|Ctrl+Enter||Execute this cell|
|Enter||Edit this cell, or called edit mode|
|Esc||Back to command mode|
|Shift+Enter||Execute this cell and move to next cell|
|b||Insert cell below|
|a||Insert cell above|
|c||Copy this cell|
|x||Cut this cell|
|v||Paste cell below|
|dd||Delete cell. Note that this will not copy this cell like Vim|