My favorite IPython Magic commands
Jupyter Notebook is a great tool for data science prototyping, visualization and sharing. The first code cell of every notebook I work on contains the following commands:
%matplotlib inline %load_ext autoreload %autoreload 2
These are some of the IPython Magic commands – handy enhancements added on top of the standard Python syntax to solve various tasks specific to interactive computing. The first line in the snippet above configures the notebook session to visualize and store all the Matplotlib figures inside the notebook. An alternative can be to add interactivity to the Matplotlib figures, which can be done with the following configuration:
autoreload extension makes it possible to automatically reload imported Python modules on each invocation of code relying on these modules. This is specifically useful when doing prototyping in a notebook and eventually decomposing good code into separate modules.
Another magic command I often use is
%timeit. It allows to gage execution duration of one-line Python expressions. For example, to measure how fast a function
f is, run the following in a separate Jupyter cell:
To read more about IPython Magic commands, refer to Jake VanderPlas’ data science handbook and the Dataquest blog.