Jupyter Notebook is a popular tool for writing Python code for data science.  For new users that haven’t used notebooks before, though, it can be a little tricky to get started with.  In this tutorial, I’ll go over how to install Jupyter Notebook, what it’s best used for, and how to run code with it.

I use Jupyter Notebooks all the time for cleaning data, and presenting data visualizations along with easy to read documentation.  In this video, I’ll be going through the basics of setting up and using Jupyter Notebooks.  I’ve included timestamps in the description if you want to jump to a specific part of the video.  If you want to see more software demos and tutorials, hit the subscribe button below to make sure you never miss a video. 

Table of Contents

  1. Why Jupyter Notebooks?
  2. Jupyter Installation with Anaconda
  3. Creating a Notebook
  4. Writing Code
  5. Running your code
  6. Documentation

Why Jupyter Notebooks?

Jupyter Notebook is a web based application allowing users to create documents with markup text and code that can be run in document, piece by piece. Jupyter is built for data cleaning, visualization, and presentation.  The notebooks support a variety of languages, including R, Ruby and more, but it was initially built for Python, so that is the most well supported language by the software.

Notebooks are similar to IDE’s insofar as you write code in them, but using notebooks you can make changes to code and see the results on the fly, in contrast to an IDE where you would have to run an application to see the results of the code.  Additionally, Notebooks allow you to include markup language, so you can document your code both through code comments, and out of line text that is fully customizable in terms of size, color, and more.

All in all, Jupyter Notebooks are a great choice when you want to see changes in code output as you edit the code, or when you want to present your code to others with thorough and readable documentation.

Installation

Jupyter has two primary methods of installation.  Using command line, you can install Jupyter via a series of pip commands listed on Jupyter’s website.  Alternatively, Jupyter Notebooks can be installed using Anaconda, which will be easier for many users that are less comfortable with the command line, and includes a number of other data science tools such as Spyder and Rstudio.  I’ll be going over how to install Jupyter Notebooks using Anaconda.

First, head over to anaconda.com and click get started, then select install Anaconda individual edition.  Note Anaconda does offer business versions, so if your organization purchased Anaconda already, check to see what your installation process is.  Afterwords, hit download, and open the installer once it’s finished downloading.  All default installation settings should be fine for most users, so you can just click through the installer until it’s finished.  Once it’s done, you can open Anaconda Navigator from your computer, and select launch under Jupyter Notebook.

Creating a Notebook

Jupyter Notebook opens in a browser and shows you the files on your computer.  From here you can navigate to where you want to create your notebook.  I’ll be making mine in the documents folder, though you can make your notebook wherever you prefer. I’m going to set mine to be a python document.

Once you’ve selected your file type, a notebook will open, with sections for writing code and a variety of tools.

Writing Code

To start with, you’ll just have one input section where you can write either Python code or markdown.  To select which you want to write, click the dropdown at the top and select code or markdown.

Notably, using code you can also run command line scripts, including pip installations, so you can install packages you need to use like you would in the command line. 

Markdown is what you’ll use to create documentation for your code.  Anything you type as markdown can be customized.  So you can create headers using hashes, and customize regular text using html.  If you add text without html tags or styling, it will just appear as regular text.  You can use markdown to explain each block of code.  To add a new block, you can hold shift and hit enter, or select insert, and choose insert cell below. 

If you choose code, you can write code right into the editing block. So if we write a simple line of code, like print hello world, we can run the code and see the output with shift and enter.  You can also run the code by clicking the cell and selecting run cells.  To run the cells and create a new cell, select run cells, and insert below.

One of the neat things about Jupyter Notebooks is that each cell will simply return variables you include in your code.  As a result, if we were to remove our print statement, but leave the hello world string, the output will still contain hello world.

Running the Code

Once you have a complete notebook, there are a few ways you can run your code cells. First, select the cell you want to run by clicking into its cell.  After that, you can use shift and enter to run each cell in order, or click the run button at the top of the screen for the same functionality.  Next to this run button is the stop button, which you can use to stop code from running.  This function is useful if you accidentally write a function that runs infinitely and you want to stop it. 

In the cell menu, you can also choose to run all cells, run all cells above the cell you are in, or run all cells below that cell. 

Running cells individually is one of the most valuable features of Jupyter Notebooks.  In this notebook, I’ve divided all my cells by the visualizations they create.  As a result, I can take these histograms on movie genres, and change the genres they report on, then run this block of code without running any of the other cells.  If I had built this script in an IDE, I would have to run the entire script.  When you are creating complicated scripts, this can be a huge time saver. 

Documentation

I’ve gone over the basics of how to write and run code in Jupyter Notebooks, but if you are interested in some more advanced features you can click on help, where you can access a wealth of documentation. Notebook help will bring up Jupyter Notebooks’s documentation and tutorials, while markdown will provide syntax shortcuts for creating headings of different sizes, italics bolding and more.

Below the documentation for notebooks and markdown, Jupyter Notebook also provides links to the documentation for Python as well as a series of popular Python libraries, like pandas and NumPy.  You can access documentation for any of these tools with ease while you are coding. 

Conclusion

You should now understand enough to install and set up Jupyter Notebooks, as well as use them to write and run code.  If you still aren’t sure you want to go with Jupyter Notebooks, you can check out other data science tools with verified reviews from real users at Trustradius.com.  Alternatively, if you do use Jupyter Notebooks, Trustadius would love to hear your thoughts on the software at the link in the description. 

If you’re interested in more software demos or tutorials hit the subscribe button to make sure you see our new releases, and consider checking out some of our previous videos.

Source link

Need Any Technology Assistance? Call Pursho @ 0731-6725516