avatarTimo Böhm

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1589

Abstract

"gist-iframe" src="/gist/timo-boehm/73d9d4c62228dc915a3dafdaedba0537.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><h2 id="4bdc">%matplotlib</h2><p id="2296">If you did an online course before, you probably recognize this magic command in combination with the <b>inline </b>parameter. Using this command ensures that Jupyter Notebooks show your plots. Probably the most critical magic command for every report based on a notebook.</p> <figure id="81b2"> <div> <div>

            <iframe class="gist-iframe" src="/gist/timo-boehm/aee3acf5568dbbb56ced40c1e45111d4.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="4996">%load_ext autoreload</h2><p id="fb65">This magic command allows you to load the most important extension: <b>autoreload</b>. Without it, you have to reload the kernel every time you change something in your referenced code. The code snippet gives you a brief example of how to use it:</p>
    <figure id="8240">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/timo-boehm/9a9e59bf4f68da3e2e468f7fb6d0183a.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="cb9f">%system</h2><p id="7a06">If y

Options

ou want to use the shell, this magic command does the trick for you. It’s very nice for fast check of the current directory and similar things. It doesn’t look like much, but it is a good tool to have in your belt.</p> <figure id="f6d1"> <div> <div>

            <iframe class="gist-iframe" src="/gist/timo-boehm/1a6a9b9adc0e739ba440e17d48c3fc26.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="3651">%who_ls</h2><p id="91e7">This magic command does one thing very well: showing you the list of variables in your environment. You can also add a parameter to define what type of variables, e.g. functions, you want to see.</p>
    <figure id="510e">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/timo-boehm/fc64c348167bbc5427608d99c87d916a.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="6f3f">Of course, there are way more magic commands than I covered in this post. See <a href="https://ipython.readthedocs.io/en/stable/interactive/magics.html">here </a>for the official documentation. What magic commands do you use and why? Let me know in the comments or on <a href="https://twitter.com/TimoBohm">Twitter</a>. <b>Thanks for reading, leave some 👏🏻 if it helped you and let’s keep on learning!</b></p></article></body>
Photo by Jez Timms on Unsplash

The Top 5 Magic Commands for Jupyter Notebooks

Jupyter Notebooks are a web-based and interactive tool that the machine learning and data science community uses a lot. They are used for quick testing, as a reporting tool or even as highly sophisticated learning materials in online courses. One of the features I like most about them is called magic commands. These are shortcuts that significantly extend a notebook’s capabilities. Here is a list of the five spells you should learn first to become a better data wizard. I included code snippets to illustrate their use, but there is also a complete notebook available here.

%time, %timeit and %%time

Do you want to know how much time your code needs to run? Unsurprisingly, the magic command you need to use for that is time and its variations. I use the cell magic version (%%time) a lot. It is a fast way to benchmark your code and to indicate to other people how much time they would need to rerun your results.

%matplotlib

If you did an online course before, you probably recognize this magic command in combination with the inline parameter. Using this command ensures that Jupyter Notebooks show your plots. Probably the most critical magic command for every report based on a notebook.

%load_ext autoreload

This magic command allows you to load the most important extension: autoreload. Without it, you have to reload the kernel every time you change something in your referenced code. The code snippet gives you a brief example of how to use it:

%system

If you want to use the shell, this magic command does the trick for you. It’s very nice for fast check of the current directory and similar things. It doesn’t look like much, but it is a good tool to have in your belt.

%who_ls

This magic command does one thing very well: showing you the list of variables in your environment. You can also add a parameter to define what type of variables, e.g. functions, you want to see.

Of course, there are way more magic commands than I covered in this post. See here for the official documentation. What magic commands do you use and why? Let me know in the comments or on Twitter. Thanks for reading, leave some 👏🏻 if it helped you and let’s keep on learning!

Data Science
Machine Learning
Jupyter Notebook
Recommended from ReadMedium