Ipython Magic Executing Previous Line Again

Towards becoming a Jupyter Notebook power-user

When using Jupyter Notebook with IPython kernel, IPython magic commands come in handy. These magic commands get in easier to consummate certain tasks. You lot can remember of them equally an extra set of helpful syntax in add-on to Python syntax. In this post, nosotros will get familiar with a few useful magic commands that you could use in Jupyter Notebook.

Photo by RamΓ³n Salinero on Unsplash

IPython magic commands πŸ’«

If you aren't familiar with magic commands, it'southward very likely that you may have been using some unknowingly. Does this syntax: %matplotlib inline look familiar to you? Probably, yeah? %matplotlib is a IPython magic command. You lot see how this control start with %? This is a mutual characteristic of magic commands: they start with %. At that place are two kinds of magic commands:

  • line magic commands (start with %)
  • jail cell magic commands (start with %%)

For a line magic control, inputs are provided following the command in the same line. For a cell magic command, contents in the unabridged cell become its inputs. If you are not as well sure what we mean by this, an example in section 3 will hopefully clarify.

At present we know a petty bit about them, it'south time to explore a handful of useful magic commands and familiarise with their case usage! ✨

πŸ“ one. Import code with %load

Nosotros can load code from an external source into a jail cell in Jupyter Notebook using this magic control. Hither's one useful application of this:

For well-nigh data science projects, you may discover yourself importing the same set of libraries over and over over again across different notebooks. To make this process quicker, nosotros can set up a standard setup script for a Jupyter Notebook and import this script at the outset of each notebook to reduce the repetitive typing. Let'south expect at an example to illustrate what we mean past this.

Imagine that we are working in magic_commands.ipynb that is located in project1 folder and setup.py contained the post-obit setup script:

          # Contents in setup.py
# Data manipulation
import numpy as np
import pandas every bit pd
pd.options.brandish.max_columns=None
pd.options.display.float_format='{:.2f}'.format
# Visualisation
import matplotlib.pyplot as plt
import seaborn equally sns
sns.ready(style='whitegrid', context='talk', palette='rainbow')

We could import the contents in setup.py with the post-obit one liner without leaving the notebook:

          %load setup.py        

Printing Ctrl + Shift to run cell

Every bit we can see from this example, when nosotros run the command, it inserts the lawmaking from setup.py and comments itself. Since standard imports can exist used across most projects, you may adopt to save the script in Desktop (the parent directory) and have a projection specific setup in the projection binder only when needed (for case, NLP projects volition need boosted set of imports).

If nosotros wanted to load setup.py from the parent binder in the same notebook, we can update the file path to reflect the change:

          %load ..\setup.py        

Although this example case may seem trivial, it is a small change yous could kickoff practicing and information technology will hopefully inspire other applications.

Earlier we move on to the next control, it'due south worth mentioning that while importing lawmaking from .py file is mutual, you can also import content from other files such as .txt and .dr.. In add-on, yous can also import code from URL like this:

          %load https://gist.githubusercontent.com/zluvsand/74a6d88e401c4e3f76c2ae783a18689b/raw/5c9fd80a7bed839ba555bf4636e47572bd5c7e6d/pickle.py        

πŸ“ 2. Save code with %%writefile

This command lets u.s.a. do the contrary of the previous command. We tin relieve code to an external source from a cell in Jupyter Notebook using this magic command. If nosotros imagine ourselves yet being inside magic_commands.ipynb, this is how we would create setup.py to Desktop without leaving the notebook:

          %%writefile ..\setup.py
# Data manipulation
import numpy as np
import pandas every bit pd
pd.options.display.max_columns=None
pd.options.display.float_format='{:.2f}'.format
# Visualisation
import matplotlib.pyplot every bit plt
import seaborn as sns
sns.set(fashion='whitegrid', context='talk', palette='rainbow')

This will create a setup.py file if doesn't exist. Otherwise, information technology volition overwrite the contents in the existing file.

πŸ“ 3. Time code with %timeit or %%timeit

There are often multiple ways to accomplish the aforementioned job. One of import consideration when choosing between the options is speed. Or sometimes you just desire to time your lawmaking to understand its functioning. Whatever your use example might be, it's useful to know how to time your code. Fortunately, timing code is easy with %[%]timeit.

Firstly, we will fix some dummy information:

          import numpy as np
np.random.seed(seed=123)
numbers = np.random.randint(100, size=million)

Let'southward imagine nosotros wanted to time this code: mean = np.mean(numbers). We tin do so with the following one liner:

          %timeit mean = np.mean(numbers)        

Output shows hateful and standard deviation of the speed across multiple runs & loops. This is more rigorous way to fourth dimension your code compared to timing based on a unmarried run.

Now let's sympathize the deviation between %timeit and %%timeit (the following guideline is truthful for nearly line and cell magic commands):
◼ ️️To utilize %timeit, a line magic command, the code you want to time should consist of a single line and be written in the same line following the magic command. Although this is a adept general rule, multiple lines is possible with tweaks according to the documentation (see documentation for details).
◼ To use %%timeit, a cell magic command, the code you want to time can consist of whatever number of lines and written in the next line(s) following the magic command.

Here's the equivalent of the previous code using %%timeit:

          %%timeit
mean = np.hateful(numbers)

Information technology's likely that the lawmaking you want to time will consist of multiple lines, in which case %%timeit volition come in handy.

Here'southward a quick quiz to test your understanding. What do y'all think is the departure between the outputs of the following two cells? Try to think of the answer before proceeding. πŸ’­

          ##### Jail cell A first #####
%timeit mean = np.mean(numbers)
np.hateful(numbers)
##### Cell A stop #####
##### Cell B kickoff #####
%%timeit
hateful = np.mean(numbers)
np.hateful(numbers)
##### Cell B start #####

Here comes the answer. In prison cell A, beginning we time the first line of lawmaking: mean = np.hateful(numbers) then nosotros discover the average whereas in cell B, nosotros time two lines of code:

Y'all can meet that cell B'south mean speed is about twice as prison cell A's. This makes sense because we are essentially timing the aforementioned code twice (one with assignment and one without assignment) in cell B.

%[%]timeit automatically adjusts the number of loops depending on how long information technology takes to execute the code. This means that the longer the runtime, the less number of repetitions and vice versa and so that it will ever take nigh the aforementioned corporeality of time to time regardless of the complexity of the code. Still, you can command the number of runs and loops by tweaking the optional arguments. Here'southward an instance:

          %timeit -n500 -r5 np.mean(numbers)        

Here, nosotros take specified 5 runs and 500 loops.

πŸ“ 4. Check session history with %history, %notebook, %recall

These sets of commands are very useful if yous experimented with a bunch of things and it'southward already starting to go messy so information technology'due south difficult to call up exactly what you lot did. We can bank check the history of commands we ran in the electric current session with %history. Of note, %hist can be used instead of %history.

Let's imagine we started a new session in section iii. We can come across session history with:

          %history        

This is great but a piddling hard to see where i command ends and the other starts. Hither's how to cheque the history with each command numbered:

          %history -n        

This is easier to work with. At present, let's acquire how to consign the history. If we desire to write the history to a file named history.py in the aforementioned directory equally the notebook, so we could use:

          %history -f history.py        

If we want to write the history to a Jupyter Notebook called history.ipynb in the same directory as the current notebook, then we apply %notebook:

          %notebook history.ipynb        

This volition insert each command into a separate prison cell. Quite convenient isn't it?

Sometimes, we may want to think a department of commands from the history to tweak it or rerun it. In this example, nosotros tin employ %recall. When using %recall, we need to laissez passer the corresponding numbers for the department of commands from history like this example:

          %call back one-2        

The code above inserts first ii commands from history into the next cell.

πŸ“ five. Other magic commands

We accept simply covered a small-scale subset of commands in this mail service. So in this section, I want to provide a simple guide on how to explore more magic commands on your ain.
◼️ To run into all available magic commands, run %lsmagic.
◼️ To access documentation for all commands, either cheque the documentation folio or run %magic.
◼️ To access documentation of a magic command, you can run the magic command followed by ?. For example: %load?.

Lastly, attempt running the following 1 liner in your Jupyter Notebook:

          %config MagicsManager        

If you go the aforementioned output, even if nosotros don't write % or %% at the beginning of a magic control, information technology will notwithstanding be recognised. For instance, if y'all attempt running the following syntax, you will encounter the same output as before:

          config MagicsManager        

While I think it is a convenient feature, writing the prefix makes the code more than readable as it'southward easy to tell it'southward a magic command by the prefix.

Voila❕ We have covered a few useful magic commands and you are equipped to explore more on your own.✨ Every bit Stephen R Covey once said "to learn and not to practice is really not to learn. To know and not to do is really not to know", I promise yous will practise these magic commands to consolidate what nosotros accept learned today.

Photo past Patrick Hendry on Unsplash

Would you similar to access more than content like this? Medium members get unlimited access to any articles on Medium. If you go a member using my referral link, a portion of your membership fee will direct become to support me.

phillipshishand.blogspot.com

Source: https://towardsdatascience.com/useful-ipython-magic-commands-245e6c024711

0 Response to "Ipython Magic Executing Previous Line Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel