Hi Reader,
Do you believe in magic? You will after you read this weekās tip! āØ
But first: Iām including a ālink of the weekā in each issue to share something that I think is worth checking out!
š Link of the week
āAn Introduction to Statistical Learningā
Nine years after first reading it, this remains my top recommended text for learning the foundational principles of Machine Learning. Although the bookās code was originally written in R, the authors released a Python version which uses libraries like scikit-learn, statsmodels, NumPy, Matplotlib, and PyTorch.
The labs from the book can be accessed as Jupyter Notebooks on GitHub or as a searchable Jupyter Book.
š Tip #24: Use IPython magic commands for easier coding
Iām going to show you some āmagicā tricks that will improve your coding experience in Jupyter. But first, a quick history lesson⦠š
In the early days of the Jupyter Notebook, it used to be called the IPython Notebook. Thatās because it was initially built for Python only, whereas now the Notebook supports many other programming languages.
But why was it called the āIPython Notebookā, not the āPython Notebookā?
Thatās because the IPython Notebook was built on top of IPython, which is an Interactive Python shell. IPython is basically a better version of the standard Python shell.
One of the neat features of IPython is āmagic commandsā, which Iāll demonstrate below. And because the IPython Notebook (and thus the Jupyter Notebook) was built on top of IPython, you can use IPython magic commands from within Jupyter! šŖ
Line magics vs Cell magics
There are two types of IPython magic commands:
- Line magics apply to one line of code, and they start with
%
. - Cell magics apply to an entire cell, and they start with
%%
.
For example, you can use the line magic %lsmagic
to list all of the magic commands:
You can use another line magic, %quickref
, to open a āquick reference cardā that briefly describes each of the commands. (Try it out!)
Below are some of my favorite magic commands... š
Line magics: %time
and %timeit
%time
runs a line of code once, times how long it took to run, and displays the output of the code:
%timeit
runs a line of code many times and averages the timing results (for greater accuracy), but it does not display the output of the code:
I use %time
for long-running processes (like a scikit-learn grid search) in which I want to know how long it took to run but I donāt actually want to watch it run!
I use %timeit
when I need to accurately compare the performance of two different lines of code.
Cell magics: %%time
and %%timeit
The use cases for %%time
and %%timeit
are the same as the line magics above, except that these cell magics time the entire cell:
Line magics: %who
and %whos
%who
shows you all of the variables youāve defined in the current session:
%whos
is similar, but it prints some extra information about each variable:
Both %who
and %whos
can be filtered by data type:
Line magics: %history
and %pastebin
%history
shows your input history from the current session:
There are many useful options for %history, which you can learn about by adding a question mark after the command:
(The question mark allows you to get help with any object in Jupyter, not just magic commands!)
One useful option is to add -n
for line numbers:
If you really want to blow your mind, use the -g
(global) option to see your entire history, meaning every command you've ever typed into Jupyter š¤Æ
That may overflow Jupyter, so you can include the -f
option to save it to a text file:
(The last line in my text file is ā5230/15ā, which means that Iāve started 5230 Jupyter sessions on this computer š )
A more practical use of %history -g
is to search for a particular line of code that youāve written in the past. For example, I can filter my history to only show input that included ādfā:
Another line magic that pairs well with %history
is %pastebin
, which makes it easy to share your code with someone else. For example, this code uploads lines 1 through 6 of your current sessionās input history to a pastebin website:
You can then share the unique URL with anyone you like, and hereās what they would see:
Going further
There are many more magic commands, which you can read about in the IPython documentation.
There are also other IPython features worth learning about, such as the ability to run shell commands from within Jupyter!
If you enjoyed this weekās tip, please forward it to a friend! Takes only a few seconds, and it really helps me grow the newsletter! š
See you next Tuesday!
- Kevin
P.S. Officer: pop the trunkā
Did someone awesome forward you this email? Sign up here to receive data science tips every week!